github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/builtin/split_test.go (about) 1 package builtin_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/madlambda/nash/internal/sh/internal/fixture" 8 ) 9 10 func TestSplit(t *testing.T) { 11 type splitDesc struct { 12 script string 13 word string 14 sep string 15 result string 16 } 17 18 tests := map[string]splitDesc{ 19 "space": { 20 script: "./testdata/split.sh", 21 word: "a b c", 22 sep: " ", 23 result: "a\nb\nc\n", 24 }, 25 "pipes": { 26 script: "./testdata/split.sh", 27 word: "1|2|3", 28 sep: "|", 29 result: "1\n2\n3\n", 30 }, 31 "nosplit": { 32 script: "./testdata/split.sh", 33 word: "nospaces", 34 sep: " ", 35 result: "nospaces\n", 36 }, 37 "splitfunc": { 38 script: "./testdata/splitfunc.sh", 39 word: "hah", 40 sep: "a", 41 result: "h\nh\n", 42 }, 43 } 44 45 for name, desc := range tests { 46 t.Run(name, func(t *testing.T) { 47 var output bytes.Buffer 48 shell, cleanup := fixture.SetupShell(t) 49 defer cleanup() 50 51 shell.SetStdout(&output) 52 err := shell.ExecFile(desc.script, "mock cmd name", desc.word, desc.sep) 53 54 if err != nil { 55 t.Fatalf("unexpected err: %s", err) 56 } 57 58 if output.String() != desc.result { 59 t.Fatalf("got %q expected %q", output.String(), desc.result) 60 } 61 }) 62 } 63 } 64 65 func TestSplitingByFuncWrongWontPanic(t *testing.T) { 66 // Regression for: https://github.com/madlambda/nash/issues/177 67 68 badscripts := map[string]string{ 69 "noReturnValue": ` 70 fn b() { echo "oops" } 71 split("lalala", $b) 72 `, 73 "returnValueIsList": ` 74 fn b() { return () } 75 split("lalala", $b) 76 `, 77 "returnValueIsFunc": ` 78 fn b() { 79 fn x () {} 80 return $x 81 } 82 split("lalala", $b) 83 `, 84 } 85 86 for testname, badscript := range badscripts { 87 88 t.Run(testname, func(t *testing.T) { 89 shell, cleanup := fixture.SetupShell(t) 90 defer cleanup() 91 92 _, err := shell.ExecOutput("whatever", badscript) 93 if err != nil { 94 t.Fatal(err) 95 } 96 }) 97 } 98 } 99 100