github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/shell/script_test.go (about) 1 package shell 2 3 import ( 4 "testing" 5 6 . "src.elv.sh/pkg/prog/progtest" 7 ) 8 9 func TestScript_File(t *testing.T) { 10 f := Setup() 11 defer f.Cleanup() 12 MustWriteFile("a.elv", "echo hello") 13 14 Script(f.Fds(), []string{"a.elv"}, &ScriptConfig{}) 15 16 f.TestOut(t, 1, "hello\n") 17 f.TestOut(t, 2, "") 18 } 19 20 func TestScript_BadFile(t *testing.T) { 21 f := Setup() 22 defer f.Cleanup() 23 24 ret := Script(f.Fds(), []string{"a.elv"}, &ScriptConfig{}) 25 26 if ret != 2 { 27 t.Errorf("got ret %v, want 2", ret) 28 } 29 f.TestOutSnippet(t, 2, "cannot read script") 30 f.TestOut(t, 1, "") 31 } 32 33 func TestScript_Cmd(t *testing.T) { 34 f := Setup() 35 defer f.Cleanup() 36 37 Script(f.Fds(), []string{"echo hello"}, &ScriptConfig{Cmd: true}) 38 39 f.TestOut(t, 1, "hello\n") 40 f.TestOut(t, 2, "") 41 } 42 43 var scriptErrorTests = []struct { 44 name string 45 code string 46 compileOnly bool 47 json bool 48 wantExit int 49 wantOut string 50 wantErr string 51 }{ 52 {name: "parse error", 53 code: "echo [", 54 wantExit: 2, 55 wantErr: "parse error"}, 56 {name: "parse error with -compileonly and -json", 57 code: "echo [", compileOnly: true, json: true, 58 wantExit: 2, 59 wantOut: `[{"fileName":"code from -c","start":6,"end":6,"message":"should be ']'"}]` + "\n"}, 60 {name: "multiple parse errors with -compileonly and -json", 61 code: "echo [{", compileOnly: true, json: true, 62 wantExit: 2, 63 wantOut: `[{"fileName":"code from -c","start":7,"end":7,"message":"should be ',' or '}'"},{"fileName":"code from -c","start":7,"end":7,"message":"should be ']'"}]` + "\n"}, 64 {name: "compile error", 65 code: "echo $a", 66 wantExit: 2, 67 wantErr: "compilation error"}, 68 {name: "compile error with -compileonly and -json", 69 code: "echo $a", compileOnly: true, json: true, 70 wantExit: 2, 71 wantOut: `[{"fileName":"code from -c","start":5,"end":7,"message":"variable $a not found"}]` + "\n"}, 72 {name: "parse error and compile error with -compileonly and -json", 73 code: "echo [$a", compileOnly: true, json: true, 74 wantExit: 2, 75 wantOut: `[{"fileName":"code from -c","start":8,"end":8,"message":"should be ']'"},{"fileName":"code from -c","start":6,"end":8,"message":"variable $a not found"}]` + "\n"}, 76 {name: "exception", 77 code: "fail failure", 78 wantExit: 2, 79 wantOut: "", 80 wantErr: "fail failure"}, 81 {name: "exception with -compileonly", 82 code: "fail failure", compileOnly: true, 83 wantExit: 0}, 84 } 85 86 func TestScript_Error(t *testing.T) { 87 for _, test := range scriptErrorTests { 88 t.Run(test.name, func(t *testing.T) { 89 f := Setup() 90 defer f.Cleanup() 91 exit := Script(f.Fds(), []string{test.code}, &ScriptConfig{ 92 Cmd: true, CompileOnly: test.compileOnly, JSON: test.json}) 93 if exit != test.wantExit { 94 t.Errorf("got exit code %v, want 2", test.wantExit) 95 } 96 f.TestOut(t, 1, test.wantOut) 97 // When testing stderr output, we either only test that there is no 98 // output at all, or that the output contains a string; we never 99 // test it in full, as it is intended for human consumption and may 100 // change. 101 if test.wantErr == "" { 102 f.TestOut(t, 2, "") 103 } else { 104 f.TestOutSnippet(t, 2, test.wantErr) 105 } 106 }) 107 } 108 }