xgo/stdlib/os_test.go

109 lines
2.9 KiB
Go
Raw Normal View History

2019-02-05 20:45:27 +03:00
package stdlib_test
import (
"io/ioutil"
"os"
"testing"
2019-12-30 00:38:51 +03:00
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/require"
2019-02-05 20:45:27 +03:00
)
2019-02-07 00:41:46 +03:00
func TestReadFile(t *testing.T) {
content := []byte("the quick brown fox jumps over the lazy dog")
tf, err := ioutil.TempFile("", "test")
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
2019-02-08 02:18:49 +03:00
defer func() { _ = os.Remove(tf.Name()) }()
2019-02-07 00:41:46 +03:00
_, err = tf.Write(content)
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
2019-02-08 02:18:49 +03:00
_ = tf.Close()
2019-02-07 00:41:46 +03:00
2019-12-20 22:40:38 +03:00
module(t, "os").call("read_file", tf.Name()).
expect(&tengo.Bytes{Value: content})
2019-02-07 00:41:46 +03:00
}
func TestReadFileArgs(t *testing.T) {
module(t, "os").call("read_file").expectError()
}
2019-02-05 20:45:27 +03:00
func TestFileStatArgs(t *testing.T) {
module(t, "os").call("stat").expectError()
}
func TestFileStatFile(t *testing.T) {
content := []byte("the quick brown fox jumps over the lazy dog")
tf, err := ioutil.TempFile("", "test")
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
2019-02-08 02:18:49 +03:00
defer func() { _ = os.Remove(tf.Name()) }()
2019-02-05 20:45:27 +03:00
_, err = tf.Write(content)
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
2019-02-08 02:18:49 +03:00
_ = tf.Close()
2019-02-05 20:45:27 +03:00
stat, err := os.Stat(tf.Name())
if err != nil {
t.Logf("could not get tmp file stat: %s", err)
return
}
2019-12-20 22:40:38 +03:00
module(t, "os").call("stat", tf.Name()).expect(&tengo.ImmutableMap{
Value: map[string]tengo.Object{
"name": &tengo.String{Value: stat.Name()},
"mtime": &tengo.Time{Value: stat.ModTime()},
"size": &tengo.Int{Value: stat.Size()},
"mode": &tengo.Int{Value: int64(stat.Mode())},
"directory": tengo.FalseValue,
2019-02-05 20:45:27 +03:00
},
})
}
func TestFileStatDir(t *testing.T) {
td, err := ioutil.TempDir("", "test")
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
2019-02-08 02:18:49 +03:00
defer func() { _ = os.RemoveAll(td) }()
2019-02-05 20:45:27 +03:00
stat, err := os.Stat(td)
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
module(t, "os").call("stat", td).expect(&tengo.ImmutableMap{
Value: map[string]tengo.Object{
"name": &tengo.String{Value: stat.Name()},
"mtime": &tengo.Time{Value: stat.ModTime()},
"size": &tengo.Int{Value: stat.Size()},
"mode": &tengo.Int{Value: int64(stat.Mode())},
"directory": tengo.TrueValue,
2019-02-05 20:45:27 +03:00
},
})
}
func TestOSExpandEnv(t *testing.T) {
curMaxStringLen := tengo.MaxStringLen
defer func() { tengo.MaxStringLen = curMaxStringLen }()
tengo.MaxStringLen = 12
_ = os.Setenv("TENGO", "FOO BAR")
module(t, "os").call("expand_env", "$TENGO").expect("FOO BAR")
_ = os.Setenv("TENGO", "FOO")
module(t, "os").call("expand_env", "$TENGO $TENGO").expect("FOO FOO")
_ = os.Setenv("TENGO", "123456789012")
module(t, "os").call("expand_env", "$TENGO").expect("123456789012")
_ = os.Setenv("TENGO", "1234567890123")
module(t, "os").call("expand_env", "$TENGO").expectError()
_ = os.Setenv("TENGO", "123456")
module(t, "os").call("expand_env", "$TENGO$TENGO").expect("123456123456")
_ = os.Setenv("TENGO", "123456")
2019-12-20 22:40:38 +03:00
module(t, "os").call("expand_env", "${TENGO}${TENGO}").
expect("123456123456")
_ = os.Setenv("TENGO", "123456")
module(t, "os").call("expand_env", "$TENGO $TENGO").expectError()
_ = os.Setenv("TENGO", "123456")
module(t, "os").call("expand_env", "${TENGO} ${TENGO}").expectError()
}