85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package file_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"apigo.cc/gojs"
|
|
_ "apigo.cc/gojs/console"
|
|
_ "apigo.cc/gojs/file"
|
|
"github.com/ssgo/u"
|
|
)
|
|
|
|
func TestWrite(t *testing.T) {
|
|
defer os.Remove("test.txt")
|
|
defer os.Remove("test2.txt")
|
|
r, err := gojs.Run(`
|
|
import file from 'apigo.cc/gojs/file'
|
|
file.write('test.txt', 'hello world')
|
|
file.rename('test.txt', 'test2.txt')
|
|
return file.read('test2.txt')
|
|
`, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if r != "hello world" {
|
|
t.Fatal("read file error", r)
|
|
} else {
|
|
fmt.Println(u.BGreen("write test succeess"))
|
|
}
|
|
}
|
|
|
|
func TestConfig(t *testing.T) {
|
|
r, err := gojs.Run(`
|
|
import file from 'apigo.cc/gojs/file'
|
|
let t = file.loadConfig('test')
|
|
if (t.aaa != 111) return 'failed to load config, aaa != 111'
|
|
if (t.bbb.length !== 2 || t.bbb[0] !== 1 || t.bbb[1] !== 2) return 'failed to load config, bbb != [1,2]'
|
|
if (t.ccc.c1 !== 111 || t.ccc.c2 !== "222") return 'failed to load config, ccc != [111,"222"]'
|
|
return true
|
|
`, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r != true {
|
|
t.Fatal(r)
|
|
}
|
|
fmt.Println(u.BGreen("loadConfig test passed"))
|
|
}
|
|
|
|
func TestUserPath(t *testing.T) {
|
|
defer os.RemoveAll("aaa")
|
|
defer os.RemoveAll("bbb")
|
|
pwd, _ := os.Getwd()
|
|
vm := gojs.New()
|
|
vm.SetUserPath("bbb")
|
|
_, err := vm.RunCode(`
|
|
import file from 'apigo.cc/gojs/file'
|
|
import co from 'apigo.cc/gojs/console'
|
|
let aaa = ''
|
|
let bbb = ''
|
|
try {
|
|
file.write('aaa/test.txt', 'hello world1')
|
|
aaa = file.read('aaa/test.txt')
|
|
} catch (e) {
|
|
co.error(e)
|
|
}
|
|
try {
|
|
file.write('bbb/test.txt', 'hello world2')
|
|
bbb = file.read('bbb/test.txt')
|
|
} catch (e) {
|
|
co.error(e)
|
|
}
|
|
`)
|
|
aaa := u.ReadFileN(filepath.Join(pwd, "aaa", "test.txt"))
|
|
bbb := u.ReadFileN(filepath.Join(pwd, "bbb", "test.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if aaa+bbb != "hello world2" {
|
|
t.Fatal("read file error", aaa+bbb)
|
|
} else {
|
|
fmt.Println(u.BGreen("userPath test succeess"))
|
|
}
|
|
}
|