os_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package stdlib_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/d5/tengo/v2"
  7. "github.com/d5/tengo/v2/require"
  8. )
  9. func TestReadFile(t *testing.T) {
  10. content := []byte("the quick brown fox jumps over the lazy dog")
  11. tf, err := ioutil.TempFile("", "test")
  12. require.NoError(t, err)
  13. defer func() { _ = os.Remove(tf.Name()) }()
  14. _, err = tf.Write(content)
  15. require.NoError(t, err)
  16. _ = tf.Close()
  17. module(t, "os").call("read_file", tf.Name()).
  18. expect(&tengo.Bytes{Value: content})
  19. }
  20. func TestReadFileArgs(t *testing.T) {
  21. module(t, "os").call("read_file").expectError()
  22. }
  23. func TestFileStatArgs(t *testing.T) {
  24. module(t, "os").call("stat").expectError()
  25. }
  26. func TestFileStatFile(t *testing.T) {
  27. content := []byte("the quick brown fox jumps over the lazy dog")
  28. tf, err := ioutil.TempFile("", "test")
  29. require.NoError(t, err)
  30. defer func() { _ = os.Remove(tf.Name()) }()
  31. _, err = tf.Write(content)
  32. require.NoError(t, err)
  33. _ = tf.Close()
  34. stat, err := os.Stat(tf.Name())
  35. if err != nil {
  36. t.Logf("could not get tmp file stat: %s", err)
  37. return
  38. }
  39. module(t, "os").call("stat", tf.Name()).expect(&tengo.ImmutableMap{
  40. Value: map[string]tengo.Object{
  41. "name": &tengo.String{Value: stat.Name()},
  42. "mtime": &tengo.Time{Value: stat.ModTime()},
  43. "size": tengo.Int{Value: stat.Size()},
  44. "mode": tengo.Int{Value: int64(stat.Mode())},
  45. "directory": tengo.FalseValue,
  46. },
  47. })
  48. }
  49. func TestFileStatDir(t *testing.T) {
  50. td, err := ioutil.TempDir("", "test")
  51. require.NoError(t, err)
  52. defer func() { _ = os.RemoveAll(td) }()
  53. stat, err := os.Stat(td)
  54. require.NoError(t, err)
  55. module(t, "os").call("stat", td).expect(&tengo.ImmutableMap{
  56. Value: map[string]tengo.Object{
  57. "name": &tengo.String{Value: stat.Name()},
  58. "mtime": &tengo.Time{Value: stat.ModTime()},
  59. "size": tengo.Int{Value: stat.Size()},
  60. "mode": tengo.Int{Value: int64(stat.Mode())},
  61. "directory": tengo.TrueValue,
  62. },
  63. })
  64. }
  65. func TestOSExpandEnv(t *testing.T) {
  66. curMaxStringLen := tengo.MaxStringLen
  67. defer func() { tengo.MaxStringLen = curMaxStringLen }()
  68. tengo.MaxStringLen = 12
  69. _ = os.Setenv("TENGO", "FOO BAR")
  70. module(t, "os").call("expand_env", "$TENGO").expect("FOO BAR")
  71. _ = os.Setenv("TENGO", "FOO")
  72. module(t, "os").call("expand_env", "$TENGO $TENGO").expect("FOO FOO")
  73. _ = os.Setenv("TENGO", "123456789012")
  74. module(t, "os").call("expand_env", "$TENGO").expect("123456789012")
  75. _ = os.Setenv("TENGO", "1234567890123")
  76. module(t, "os").call("expand_env", "$TENGO").expectError()
  77. _ = os.Setenv("TENGO", "123456")
  78. module(t, "os").call("expand_env", "$TENGO$TENGO").expect("123456123456")
  79. _ = os.Setenv("TENGO", "123456")
  80. module(t, "os").call("expand_env", "${TENGO}${TENGO}").
  81. expect("123456123456")
  82. _ = os.Setenv("TENGO", "123456")
  83. module(t, "os").call("expand_env", "$TENGO $TENGO").expectError()
  84. _ = os.Setenv("TENGO", "123456")
  85. module(t, "os").call("expand_env", "${TENGO} ${TENGO}").expectError()
  86. }