src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/shell/script_test.go (about) 1 package shell 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/must" 7 . "src.elv.sh/pkg/prog/progtest" 8 "src.elv.sh/pkg/testutil" 9 ) 10 11 func TestScript(t *testing.T) { 12 setupCleanHomePaths(t) 13 testutil.InTempDir(t) 14 must.WriteFile("hello.elv", "echo hello") 15 must.WriteFile("invalid-utf8.elv", "\xff") 16 17 Test(t, &Program{}, 18 ThatElvish("hello.elv").WritesStdout("hello\n"), 19 ThatElvish("-c", "echo hello").WritesStdout("hello\n"), 20 21 ThatElvish("invalid-utf8.elv"). 22 ExitsWith(2). 23 WritesStderrContaining("cannot read script"), 24 ThatElvish("non-existent.elv"). 25 ExitsWith(2). 26 WritesStderrContaining("cannot read script"), 27 28 // parse error 29 ThatElvish("-c", "echo ["). 30 ExitsWith(2). 31 WritesStderrContaining("Parse error"), 32 // parse error with -compileonly 33 ThatElvish("-compileonly", "-c", "echo ["). 34 ExitsWith(2). 35 WritesStderrContaining("Parse error"), 36 // parse error with -compileonly -json 37 ThatElvish("-compileonly", "-json", "-c", "echo ["). 38 ExitsWith(2). 39 WritesStdout(`[{"fileName":"code from -c","start":6,"end":6,"message":"should be ']'"}]`+"\n"), 40 // multiple parse errors with -compileonly -json 41 ThatElvish("-compileonly", "-json", "-c", "echo [{"). 42 ExitsWith(2). 43 WritesStdout(`[{"fileName":"code from -c","start":7,"end":7,"message":"should be ',' or '}'"},{"fileName":"code from -c","start":7,"end":7,"message":"should be ']'"}]`+"\n"), 44 45 // compilation error 46 ThatElvish("-c", "echo $a"). 47 ExitsWith(2). 48 WritesStderrContaining("Compilation error"), 49 // compilation error with -compileonly 50 ThatElvish("-compileonly", "-c", "echo $a"). 51 ExitsWith(2). 52 WritesStderrContaining("Compilation error"), 53 // compilation error with -compileonly -json 54 ThatElvish("-compileonly", "-json", "-c", "echo $a"). 55 ExitsWith(2). 56 WritesStdout(`[{"fileName":"code from -c","start":5,"end":7,"message":"variable $a not found"}]`+"\n"), 57 // parse error and compilation error with -compileonly 58 ThatElvish("-compileonly", "-json", "-c", "echo [$a"). 59 ExitsWith(2). 60 WritesStdout(`[{"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"), 61 62 // exception 63 ThatElvish("-c", "fail failure"). 64 ExitsWith(2). 65 WritesStdout(""). 66 WritesStderrContaining("fail failure"), 67 // exception with -compileonly 68 ThatElvish("-compileonly", "-c", "fail failure"). 69 ExitsWith(0), 70 ) 71 }