github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/compile_effect_unix_test.go (about) 1 //go:build !windows && !plan9 2 // +build !windows,!plan9 3 4 package eval_test 5 6 import ( 7 "os" 8 "strings" 9 "testing" 10 11 "github.com/markusbkk/elvish/pkg/eval/errs" 12 . "github.com/markusbkk/elvish/pkg/eval/evaltest" 13 "github.com/markusbkk/elvish/pkg/testutil" 14 ) 15 16 func TestPipeline_ReaderGone_Unix(t *testing.T) { 17 Test(t, 18 // External commands terminated by SIGPIPE due to reader exiting early 19 // raise ReaderGone, which is then suppressed. 20 That("yes | true").DoesNothing(), 21 That( 22 "var reached = $false", 23 "{ yes; reached = $true } | true", 24 "put $reached", 25 ).Puts(false), 26 ) 27 } 28 29 func TestCommand_External(t *testing.T) { 30 d := testutil.InTempDir(t) 31 32 mustWriteScript("foo", "#!/bin/sh", "echo foo") 33 mustWriteScript("lorem/ipsum", "#!/bin/sh", "echo lorem ipsum") 34 35 testutil.Setenv(t, "PATH", d+"/bin") 36 mustWriteScript("bin/hello", "#!/bin/sh", "echo hello") 37 38 Test(t, 39 // External commands, searched and relative 40 That("hello").Prints("hello\n"), 41 That("./foo").Prints("foo\n"), 42 That("lorem/ipsum").Prints("lorem ipsum\n"), 43 // Using the explicit e: namespace. 44 That("e:hello").Prints("hello\n"), 45 That("e:./foo").Prints("foo\n"), 46 // Relative external commands may be a dynamic string. 47 That("var x = ipsum", "lorem/$x").Prints("lorem ipsum\n"), 48 // Searched external commands may not be a dynamic string. 49 That("var x = hello; $x").Throws( 50 errs.BadValue{What: "command", 51 Valid: "callable or string containing slash", Actual: "hello"}, 52 "$x"), 53 54 // Using new FD as destination in external commands. 55 // Regression test against b.elv.sh/788. 56 That("./foo 5</dev/null").Prints("foo\n"), 57 58 // Using pragma to allow or disallow implicit searched commands 59 That("pragma unknown-command = disallow", "hello").DoesNotCompile(), 60 That("pragma unknown-command = external", "hello").Prints("hello\n"), 61 // Pragma applies to subscope 62 That("pragma unknown-command = disallow", "{ hello }").DoesNotCompile(), 63 // Explicit uses with e: is always allowed 64 That("pragma unknown-command = disallow", "e:hello").Prints("hello\n"), 65 // Relative external commands are always allowed 66 That("pragma unknown-command = disallow", "./foo").Prints("foo\n"), 67 That("pragma unknown-command = disallow", "var x = ./foo", "$x").Prints("foo\n"), 68 ) 69 } 70 71 func mustWriteScript(name string, lines ...string) { 72 testutil.MustWriteFile(name, strings.Join(lines, "\n")) 73 testutil.Must(os.Chmod(name, 0700)) 74 }