github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/mods/file/file_test.go (about) 1 package file 2 3 import ( 4 "os" 5 "testing" 6 7 "src.elv.sh/pkg/eval" 8 "src.elv.sh/pkg/eval/errs" 9 . "src.elv.sh/pkg/eval/evaltest" 10 "src.elv.sh/pkg/testutil" 11 ) 12 13 // A number that exceeds the range of int64 14 const z = "100000000000000000000" 15 16 func TestFile(t *testing.T) { 17 setup := func(ev *eval.Evaler) { 18 ev.AddGlobal(eval.NsBuilder{}.AddNs("file", Ns).Ns()) 19 } 20 _, cleanup := testutil.InTestDir() 21 defer cleanup() 22 23 TestWithSetup(t, setup, 24 That(` 25 echo haha > out3 26 f = (file:open out3) 27 slurp < $f 28 file:close $f 29 `).Puts("haha\n"), 30 31 That(` 32 p = (file:pipe) 33 echo haha > $p 34 file:close $p[w] 35 slurp < $p 36 file:close $p[r] 37 `).Puts("haha\n"), 38 39 That(` 40 p = (file:pipe) 41 echo Zeppelin > $p 42 file:close $p[w] 43 echo Sabbath > $p 44 slurp < $p 45 file:close $p[r] 46 `).Puts("Zeppelin\n"), 47 48 That(` 49 p = (file:pipe) 50 echo Legolas > $p 51 file:close $p[r] 52 slurp < $p 53 `).Throws(AnyError), 54 55 That(`p = (file:pipe)`, `echo Legolas > $p`, `file:prclose $p`, 56 `slurp < $p`).Throws(AnyError), 57 58 // Side effect checked below 59 That("echo > file100", "file:truncate file100 100").DoesNothing(), 60 61 // Should also test the case where the argument doesn't fit in an int 62 // but does in a *big.Int, but this could consume too much disk 63 64 That("file:truncate bad -1").Throws(errs.OutOfRange{ 65 What: "size argument to file:truncate", 66 ValidLow: "0", ValidHigh: "2^64-1", Actual: "-1", 67 }), 68 69 That("file:truncate bad "+z).Throws(errs.OutOfRange{ 70 What: "size argument to file:truncate", 71 ValidLow: "0", ValidHigh: "2^64-1", Actual: z, 72 }), 73 74 That("file:truncate bad 1.5").Throws(errs.BadValue{ 75 What: "size argument to file:truncate", 76 Valid: "integer", Actual: "non-integer", 77 }), 78 ) 79 80 fi, err := os.Stat("file100") 81 if err != nil { 82 t.Errorf("stat file100: %v", err) 83 } 84 if size := fi.Size(); size != 100 { 85 t.Errorf("got file100 size %v, want 100", size) 86 } 87 }