0c5e80b057
* add tengo.MaxStringLen and tengo.MaxBytesLen to limit the maximum byte-length of string/bytes values * add couple more tests
119 lines
3 KiB
Go
119 lines
3 KiB
Go
package stdlib_test
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/d5/tengo"
|
|
"github.com/d5/tengo/assert"
|
|
"github.com/d5/tengo/objects"
|
|
)
|
|
|
|
func TestReadFile(t *testing.T) {
|
|
content := []byte("the quick brown fox jumps over the lazy dog")
|
|
tf, err := ioutil.TempFile("", "test")
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
defer func() { _ = os.Remove(tf.Name()) }()
|
|
|
|
_, err = tf.Write(content)
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
_ = tf.Close()
|
|
|
|
module(t, "os").call("read_file", tf.Name()).expect(&objects.Bytes{Value: content})
|
|
}
|
|
|
|
func TestReadFileArgs(t *testing.T) {
|
|
module(t, "os").call("read_file").expectError()
|
|
}
|
|
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")
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
defer func() { _ = os.Remove(tf.Name()) }()
|
|
|
|
_, err = tf.Write(content)
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
_ = tf.Close()
|
|
|
|
stat, err := os.Stat(tf.Name())
|
|
if err != nil {
|
|
t.Logf("could not get tmp file stat: %s", err)
|
|
return
|
|
}
|
|
|
|
module(t, "os").call("stat", tf.Name()).expect(&objects.ImmutableMap{
|
|
Value: map[string]objects.Object{
|
|
"name": &objects.String{Value: stat.Name()},
|
|
"mtime": &objects.Time{Value: stat.ModTime()},
|
|
"size": &objects.Int{Value: stat.Size()},
|
|
"mode": &objects.Int{Value: int64(stat.Mode())},
|
|
"directory": objects.FalseValue,
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestFileStatDir(t *testing.T) {
|
|
td, err := ioutil.TempDir("", "test")
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
defer func() { _ = os.RemoveAll(td) }()
|
|
|
|
stat, err := os.Stat(td)
|
|
if !assert.NoError(t, err) {
|
|
return
|
|
}
|
|
|
|
module(t, "os").call("stat", td).expect(&objects.ImmutableMap{
|
|
Value: map[string]objects.Object{
|
|
"name": &objects.String{Value: stat.Name()},
|
|
"mtime": &objects.Time{Value: stat.ModTime()},
|
|
"size": &objects.Int{Value: stat.Size()},
|
|
"mode": &objects.Int{Value: int64(stat.Mode())},
|
|
"directory": objects.TrueValue,
|
|
},
|
|
})
|
|
}
|
|
|
|
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")
|
|
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()
|
|
}
|