file_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package file
  2. import (
  3. "os"
  4. "testing"
  5. "src.elv.sh/pkg/eval"
  6. "src.elv.sh/pkg/eval/errs"
  7. "src.elv.sh/pkg/eval/evaltest"
  8. "src.elv.sh/pkg/testutil"
  9. )
  10. // A number that exceeds the range of int64
  11. const z = "100000000000000000000"
  12. func TestOpen(t *testing.T) {
  13. testutil.InTempDir(t)
  14. evaltest.TestWithSetup(t, setupFileModule,
  15. That(`
  16. echo haha > out3
  17. var f = (file:open out3)
  18. slurp < $f
  19. file:close $f
  20. `).Puts("haha\n"),
  21. )
  22. }
  23. func TestPipe(t *testing.T) {
  24. evaltest.TestWithSetup(t, setupFileModule,
  25. That(`
  26. var p = (file:pipe)
  27. echo haha > $p
  28. file:close $p[w]
  29. slurp < $p
  30. file:close $p[r]
  31. `).Puts("haha\n"),
  32. That(`
  33. var p = (file:pipe)
  34. echo Legolas > $p
  35. file:close $p[r]
  36. slurp < $p
  37. `).Throws(evaltest.ErrorWithType(&os.PathError{})),
  38. // Verify that input redirection from a closed pipe throws an exception. That exception is a
  39. // Go stdlib error whose stringified form looks something like "read |0: file already
  40. // closed".
  41. That(`var p = (file:pipe)`, `echo Legolas > $p`, `file:close $p[r]`,
  42. `slurp < $p`).Throws(evaltest.ErrorWithType(&os.PathError{})),
  43. )
  44. }
  45. func TestTruncate(t *testing.T) {
  46. testutil.InTempDir(t)
  47. evaltest.TestWithSetup(t, setupFileModule,
  48. // Side effect checked below
  49. That("echo > file100", "file:truncate file100 100").DoesNothing(),
  50. // Should also test the case where the argument doesn't fit in an int
  51. // but does in a *big.Int, but this could consume too much disk
  52. That("file:truncate bad -1").Throws(errs.OutOfRange{
  53. What: "size argument to file:truncate",
  54. ValidLow: "0", ValidHigh: "2^64-1", Actual: "-1",
  55. }),
  56. That("file:truncate bad "+z).Throws(errs.OutOfRange{
  57. What: "size argument to file:truncate",
  58. ValidLow: "0", ValidHigh: "2^64-1", Actual: z,
  59. }),
  60. That("file:truncate bad 1.5").Throws(errs.BadValue{
  61. What: "size argument to file:truncate",
  62. Valid: "integer", Actual: "non-integer",
  63. }),
  64. )
  65. fi, err := os.Stat("file100")
  66. if err != nil {
  67. t.Errorf("stat file100: %v", err)
  68. }
  69. if size := fi.Size(); size != 100 {
  70. t.Errorf("got file100 size %v, want 100", size)
  71. }
  72. }
  73. func TestIsTTY(t *testing.T) {
  74. evaltest.TestWithSetup(t, setupFileModule,
  75. That("file:is-tty 0").Puts(false),
  76. That("file:is-tty (num 0)").Puts(false),
  77. That(
  78. "var p = (file:pipe)",
  79. "file:is-tty $p[r]; file:is-tty $p[w]",
  80. "file:close $p[r]; file:close $p[w]").
  81. Puts(false, false),
  82. That("file:is-tty a").
  83. Throws(errs.BadValue{What: "argument to file:is-tty",
  84. Valid: "file value or numerical FD", Actual: "a"}),
  85. That("file:is-tty []").
  86. Throws(errs.BadValue{What: "argument to file:is-tty",
  87. Valid: "file value or numerical FD", Actual: "[]"}),
  88. )
  89. if canOpen("/dev/null") {
  90. evaltest.TestWithSetup(t, setupFileModule,
  91. That("file:is-tty 0 < /dev/null").Puts(false),
  92. That("file:is-tty (num 0) < /dev/null").Puts(false),
  93. )
  94. }
  95. if canOpen("/dev/tty") {
  96. evaltest.TestWithSetup(t, setupFileModule,
  97. That("file:is-tty 0 < /dev/tty").Puts(true),
  98. That("file:is-tty (num 0) < /dev/tty").Puts(true),
  99. )
  100. }
  101. // TODO: Test with PTY when https://b.elv.sh/1595 is resolved.
  102. }
  103. func canOpen(name string) bool {
  104. f, err := os.Open(name)
  105. f.Close()
  106. return err == nil
  107. }
  108. func setupFileModule(ev *eval.Evaler) {
  109. ev.ExtendGlobal(eval.BuildNs().AddNs("file", Ns))
  110. }