2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2020-01-12 08:42:18 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
2021-10-05 06:57:00 +08:00
|
|
|
"time"
|
2020-01-12 08:42:18 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
2021-03-24 12:15:27 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2021-08-24 22:56:04 +08:00
|
|
|
"src.elv.sh/pkg/mods/file"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/testutil"
|
2020-01-12 08:42:18 +08:00
|
|
|
)
|
2017-12-22 04:49:14 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestChunk(t *testing.T) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
|
|
|
// Empty chunk
|
|
|
|
That("").DoesNothing(),
|
|
|
|
// Outputs of pipelines in a chunk are concatenated
|
|
|
|
That("put x; put y; put z").Puts("x", "y", "z"),
|
|
|
|
// A failed pipeline cause the whole chunk to fail
|
2021-07-15 10:31:01 +08:00
|
|
|
That("put a; e:false; put b").Puts("a").Throws(ErrorWithType(ExternalCmdExit{})),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestPipeline(t *testing.T) {
|
2021-10-05 06:57:00 +08:00
|
|
|
Test(t,
|
2018-05-22 08:08:11 +08:00
|
|
|
// Pure byte pipeline
|
2018-07-06 08:32:42 +08:00
|
|
|
That(`echo "Albert\nAllan\nAlbraham\nBerlin" | sed s/l/1/g | grep e`).
|
|
|
|
Prints("A1bert\nBer1in\n"),
|
2021-01-10 22:48:21 +08:00
|
|
|
// Pure value pipeline
|
2021-11-28 11:24:42 +08:00
|
|
|
That(`put 233 42 19 | each {|x|+ $x 10}`).Puts(243, 52, 29),
|
2018-05-22 08:08:11 +08:00
|
|
|
// Pipeline draining.
|
|
|
|
That(`range 100 | put x`).Puts("x"),
|
2021-10-05 06:57:00 +08:00
|
|
|
// TODO: Add a useful hybrid pipeline sample
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipeline_BgJob(t *testing.T) {
|
|
|
|
setup := func(ev *Evaler) {
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.ExtendGlobal(BuildNs().AddNs("file", file.Ns))
|
2021-10-05 06:57:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
notes1 := make(chan string)
|
|
|
|
notes2 := make(chan string)
|
|
|
|
|
|
|
|
putNote := func(ch chan<- string) func(*Evaler) {
|
|
|
|
return func(ev *Evaler) {
|
|
|
|
ev.BgJobNotify = func(note string) { ch <- note }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
verifyNote := func(notes <-chan string, wantNote string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
select {
|
|
|
|
case note := <-notes:
|
|
|
|
if note != wantNote {
|
|
|
|
t.Errorf("got note %q, want %q", note, wantNote)
|
|
|
|
}
|
2021-10-05 08:02:54 +08:00
|
|
|
case <-time.After(testutil.Scaled(100 * time.Millisecond)):
|
2021-10-05 06:57:00 +08:00
|
|
|
t.Errorf("timeout waiting for notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWithSetup(t, setup,
|
2020-01-09 07:29:00 +08:00
|
|
|
That(
|
2022-01-03 08:10:40 +08:00
|
|
|
"set notify-bg-job-success = $false",
|
|
|
|
"var p = (file:pipe)",
|
2021-07-14 11:44:37 +08:00
|
|
|
"{ print foo > $p; file:close $p[w] }&",
|
2021-10-05 06:57:00 +08:00
|
|
|
"slurp < $p; file:close $p[r]").
|
|
|
|
Puts("foo"),
|
|
|
|
// Notification
|
|
|
|
That(
|
2022-01-03 08:10:40 +08:00
|
|
|
"set notify-bg-job-success = $true",
|
|
|
|
"var p = (file:pipe)",
|
2021-10-05 06:57:00 +08:00
|
|
|
"fn f { file:close $p[w] }",
|
|
|
|
"f &",
|
|
|
|
"slurp < $p; file:close $p[r]").
|
|
|
|
Puts("").
|
|
|
|
WithSetup(putNote(notes1)).
|
|
|
|
Passes(verifyNote(notes1, "job f & finished")),
|
|
|
|
// Notification, with exception
|
|
|
|
That(
|
2022-01-03 08:10:40 +08:00
|
|
|
"set notify-bg-job-success = $true",
|
|
|
|
"var p = (file:pipe)",
|
2021-10-05 06:57:00 +08:00
|
|
|
"fn f { file:close $p[w]; fail foo }",
|
|
|
|
"f &",
|
|
|
|
"slurp < $p; file:close $p[r]").
|
|
|
|
Puts("").
|
|
|
|
WithSetup(putNote(notes2)).
|
|
|
|
Passes(verifyNote(notes2, "job f & finished, errors = foo")),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2021-06-18 07:11:51 +08:00
|
|
|
func TestPipeline_ReaderGone(t *testing.T) {
|
2023-01-05 02:09:11 +08:00
|
|
|
// See Unix-only tests in compile_effect_unix_test.go.
|
2021-06-18 07:11:51 +08:00
|
|
|
Test(t,
|
|
|
|
// Internal commands writing to byte output raises ReaderGone when the
|
|
|
|
// reader is exited, which is then suppressed.
|
|
|
|
That("while $true { echo y } | nop").DoesNothing(),
|
|
|
|
That(
|
|
|
|
"var reached = $false",
|
|
|
|
"{ while $true { echo y }; reached = $true } | nop",
|
|
|
|
"put $reached",
|
|
|
|
).Puts(false),
|
|
|
|
// Similar for value output.
|
|
|
|
That("while $true { put y } | nop").DoesNothing(),
|
|
|
|
That(
|
|
|
|
"var reached = $false",
|
|
|
|
"{ while $true { put y }; reached = $true } | nop",
|
|
|
|
"put $reached",
|
|
|
|
).Puts(false),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestCommand(t *testing.T) {
|
|
|
|
Test(t,
|
2020-03-31 11:45:32 +08:00
|
|
|
That("put foo").Puts("foo"),
|
|
|
|
// Command errors when the head is not a single value.
|
2020-04-13 20:24:49 +08:00
|
|
|
That("{put put} foo").Throws(
|
2021-06-04 10:51:38 +08:00
|
|
|
errs.ArityMismatch{What: "command",
|
|
|
|
ValidLow: 1, ValidHigh: 1, Actual: 2},
|
2020-04-13 20:24:49 +08:00
|
|
|
"{put put}"),
|
2020-08-31 21:01:49 +08:00
|
|
|
// Command errors when the head is not callable or string containing slash.
|
2020-04-13 20:24:49 +08:00
|
|
|
That("[] foo").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "command",
|
2020-08-31 21:01:49 +08:00
|
|
|
Valid: "callable or string containing slash",
|
2021-10-07 08:21:28 +08:00
|
|
|
Actual: "[]"},
|
2020-04-13 20:24:49 +08:00
|
|
|
"[]"),
|
2022-07-05 10:00:29 +08:00
|
|
|
// Command errors when argument errors.
|
2020-09-03 13:48:39 +08:00
|
|
|
That("put [][1]").Throws(ErrorWithType(errs.OutOfRange{}), "[][1]"),
|
2020-04-13 23:28:27 +08:00
|
|
|
// Command errors when an option key is not string.
|
|
|
|
That("put &[]=[]").Throws(
|
|
|
|
errs.BadValue{What: "option key", Valid: "string", Actual: "list"},
|
|
|
|
"put &[]=[]"),
|
2020-03-31 11:45:32 +08:00
|
|
|
// Command errors when any optional evaluation errors.
|
2020-09-03 13:48:39 +08:00
|
|
|
That("put &x=[][1]").Throws(ErrorWithType(errs.OutOfRange{}), "[][1]"),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2020-03-31 11:45:32 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestCommand_Special(t *testing.T) {
|
|
|
|
Test(t,
|
2021-01-10 07:33:24 +08:00
|
|
|
// Regression test for #1204; ensures that the arguments of special
|
|
|
|
// forms are not accidentally compiled twice.
|
|
|
|
That("nop (and (use builtin)); nop $builtin:echo~").DoesNothing(),
|
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
// Behavior of individual special commands are tested in
|
|
|
|
// builtin_special_test.go.
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:30:24 +08:00
|
|
|
func TestCommand_LegacyTemporaryAssignment(t *testing.T) {
|
2021-01-10 22:48:21 +08:00
|
|
|
Test(t,
|
2021-04-09 06:57:40 +08:00
|
|
|
That("var a b = alice bob; {a,@b}=(put amy ben) put $a $@b; put $a $b").
|
2022-03-20 17:31:38 +08:00
|
|
|
Puts("amy", "ben", "alice", "bob").PrintsStderrWith("deprecated"),
|
2018-05-22 08:08:11 +08:00
|
|
|
// Temporary assignment of list element.
|
2022-03-20 17:31:38 +08:00
|
|
|
That("var l = [a]; l[0]=x put $l[0]; put $l[0]").
|
|
|
|
Puts("x", "a").PrintsStderrWith("deprecated"),
|
2018-05-22 08:08:11 +08:00
|
|
|
// Temporary assignment of map element.
|
2022-03-20 17:31:38 +08:00
|
|
|
That("var m = [&k=v]; m[k]=v2 put $m[k]; put $m[k]").
|
|
|
|
Puts("v2", "v").PrintsStderrWith("deprecated"),
|
2018-05-22 08:08:11 +08:00
|
|
|
// Temporary assignment before special form.
|
2022-03-20 17:31:38 +08:00
|
|
|
That("li=[foo bar] for x $li { put $x }").
|
|
|
|
Puts("foo", "bar").PrintsStderrWith("deprecated"),
|
2021-04-09 06:57:40 +08:00
|
|
|
// Multiple LHSs in temporary assignments.
|
2022-03-20 17:31:38 +08:00
|
|
|
That("{a b}={foo bar} put $a $b").
|
|
|
|
Puts("foo", "bar").PrintsStderrWith("deprecated"),
|
|
|
|
That("@a=(put a b) put $@a").
|
|
|
|
Puts("a", "b").PrintsStderrWith("deprecated"),
|
|
|
|
That("{a,@b}=(put a b c) put $@b").
|
|
|
|
Puts("b", "c").PrintsStderrWith("deprecated"),
|
2021-04-09 06:57:40 +08:00
|
|
|
// Using syntax of temporary assignment for non-temporary assignment no
|
|
|
|
// longer compiles
|
2022-10-02 12:01:39 +08:00
|
|
|
That("x=y").DoesNotCompile(
|
2022-11-21 00:39:01 +08:00
|
|
|
`using the syntax of temporary assignment for non-temporary assignment is no longer supported; use "var" or "set" instead`),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-03 07:32:28 +08:00
|
|
|
func TestCommand_LegacyTemporaryAssignmentSyntaxIsDeprecated(t *testing.T) {
|
|
|
|
testCompileTimeDeprecation(t, "a=foo echo $a",
|
|
|
|
"the legacy temporary assignment syntax is deprecated", 18)
|
|
|
|
}
|
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestCommand_Redir(t *testing.T) {
|
2021-07-14 11:44:37 +08:00
|
|
|
setup := func(ev *Evaler) {
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.ExtendGlobal(BuildNs().AddNs("file", file.Ns))
|
2021-07-14 11:44:37 +08:00
|
|
|
}
|
2021-08-07 05:18:09 +08:00
|
|
|
testutil.InTempDir(t)
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2021-07-14 11:44:37 +08:00
|
|
|
TestWithSetup(t, setup,
|
2020-03-31 11:45:32 +08:00
|
|
|
// Output and input redirection.
|
|
|
|
That("echo 233 > out1", " slurp < out1").Puts("233\n"),
|
|
|
|
// Append.
|
|
|
|
That("echo 1 > out; echo 2 >> out; slurp < out").Puts("1\n2\n"),
|
2021-10-03 08:32:35 +08:00
|
|
|
// Read and write.
|
|
|
|
// TODO: Add a meaningful use case that uses both read and write.
|
|
|
|
That("echo 233 <> out1", " slurp < out1").Puts("233\n"),
|
2020-03-31 11:45:32 +08:00
|
|
|
|
2018-05-22 08:08:11 +08:00
|
|
|
// Redirections from special form.
|
2020-01-12 08:42:18 +08:00
|
|
|
That(`for x [lorem ipsum] { echo $x } > out2`, `slurp < out2`).
|
|
|
|
Puts("lorem\nipsum\n"),
|
2020-03-31 11:45:32 +08:00
|
|
|
|
2020-01-12 08:42:18 +08:00
|
|
|
// Using numeric FDs as source and destination.
|
|
|
|
That(`{ echo foobar >&2 } 2> out3`, `slurp < out3`).
|
|
|
|
Puts("foobar\n"),
|
|
|
|
// Using named FDs as source and destination.
|
2021-10-03 08:32:35 +08:00
|
|
|
That("echo 233 stdout> out1", " slurp stdin< out1").Puts("233\n"),
|
2020-01-12 08:42:18 +08:00
|
|
|
That(`{ echo foobar >&stderr } stderr> out4`, `slurp < out4`).
|
|
|
|
Puts("foobar\n"),
|
|
|
|
// Using a new FD as source throws an exception.
|
2021-12-13 09:23:14 +08:00
|
|
|
That(`echo foo >&4`).Throws(InvalidFD{FD: 4}),
|
2020-01-12 08:42:18 +08:00
|
|
|
// Using a new FD as destination is OK, and makes it available.
|
|
|
|
That(`{ echo foo >&4 } 4>out5`, `slurp < out5`).Puts("foo\n"),
|
2020-03-31 11:45:32 +08:00
|
|
|
|
2018-05-22 08:08:11 +08:00
|
|
|
// Redirections from File object.
|
2022-01-03 08:10:40 +08:00
|
|
|
That(`echo haha > out3`, `var f = (file:open out3)`, `slurp <$f`, ` file:close $f`).
|
2020-01-12 08:42:18 +08:00
|
|
|
Puts("haha\n"),
|
2018-05-22 08:08:11 +08:00
|
|
|
// Redirections from Pipe object.
|
2022-01-03 08:10:40 +08:00
|
|
|
That(`var p = (file:pipe); echo haha > $p; file:close $p[w]; slurp < $p; file:close $p[r]`).
|
2020-01-12 08:42:18 +08:00
|
|
|
Puts("haha\n"),
|
2020-03-31 11:45:32 +08:00
|
|
|
|
2020-09-10 13:35:23 +08:00
|
|
|
// We can't read values from a file and shouldn't hang when iterating
|
|
|
|
// over input from a file.
|
2021-01-27 09:28:38 +08:00
|
|
|
// Regression test for https://src.elv.sh/issues/1010
|
2020-09-10 13:35:23 +08:00
|
|
|
That("echo abc > bytes", "each $echo~ < bytes").Prints("abc\n"),
|
2021-04-04 20:37:38 +08:00
|
|
|
That("echo def > bytes", "only-values < bytes | count").Puts(0),
|
2020-09-10 13:35:23 +08:00
|
|
|
|
2021-06-22 07:33:41 +08:00
|
|
|
// Writing value output to file throws an exception.
|
2022-06-08 03:28:43 +08:00
|
|
|
That("put foo >a").Throws(ErrPortDoesNotSupportValueOutput, "put foo >a"),
|
2021-06-22 07:33:41 +08:00
|
|
|
// Writing value output to closed port throws an exception too.
|
2022-06-08 03:28:43 +08:00
|
|
|
That("put foo >&-").Throws(ErrPortDoesNotSupportValueOutput, "put foo >&-"),
|
2021-06-22 07:33:41 +08:00
|
|
|
|
2020-04-13 23:28:27 +08:00
|
|
|
// Invalid redirection destination.
|
|
|
|
That("echo []> test").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "redirection destination",
|
|
|
|
Valid: "fd name or number", Actual: "[]"},
|
|
|
|
"[]"),
|
|
|
|
// Invalid fd redirection source.
|
|
|
|
That("echo >&test").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "redirection source",
|
|
|
|
Valid: "fd name or number or '-'", Actual: "test"},
|
|
|
|
"test"),
|
|
|
|
// Invalid redirection source.
|
2020-04-13 20:24:49 +08:00
|
|
|
That("echo > []").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "redirection source",
|
2023-07-19 00:52:06 +08:00
|
|
|
Valid: "string, file or map", Actual: "list"},
|
2020-04-13 20:24:49 +08:00
|
|
|
"[]"),
|
2023-07-19 00:52:06 +08:00
|
|
|
// Invalid map for redirection.
|
|
|
|
That("echo < [&]").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "map for input redirection",
|
|
|
|
Valid: "map with file in the 'r' field", Actual: "[&]"},
|
|
|
|
"[&]"),
|
|
|
|
That("echo > [&]").Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "map for output redirection",
|
|
|
|
Valid: "map with file in the 'w' field", Actual: "[&]"},
|
|
|
|
"[&]"),
|
2021-10-03 08:32:35 +08:00
|
|
|
|
|
|
|
// Exception when evaluating source or destination.
|
|
|
|
That("echo > (fail foo)").Throws(FailError{"foo"}, "fail foo"),
|
|
|
|
That("echo (fail foo)> file").Throws(FailError{"foo"}, "fail foo"),
|
2018-05-22 08:08:11 +08:00
|
|
|
)
|
2017-12-17 11:41:25 +08:00
|
|
|
}
|
2020-04-18 07:26:48 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestCommand_Stacktrace(t *testing.T) {
|
2020-09-03 13:48:39 +08:00
|
|
|
oops := ErrorWithMessage("oops")
|
2020-04-18 07:26:48 +08:00
|
|
|
Test(t,
|
|
|
|
// Stack traces.
|
|
|
|
That("fail oops").Throws(oops, "fail oops"),
|
|
|
|
That("fn f { fail oops }", "f").Throws(oops, "fail oops ", "f"),
|
|
|
|
That("fn f { fail oops }", "fn g { f }", "g").Throws(
|
|
|
|
oops, "fail oops ", "f ", "g"),
|
|
|
|
// Error thrown before execution.
|
2020-09-03 13:48:39 +08:00
|
|
|
That("fn f { }", "f a").Throws(ErrorWithType(errs.ArityMismatch{}), "f a"),
|
2020-04-18 07:26:48 +08:00
|
|
|
// Error from builtin.
|
|
|
|
That("count 1 2 3").Throws(
|
2020-09-03 13:48:39 +08:00
|
|
|
ErrorWithType(errs.ArityMismatch{}), "count 1 2 3"),
|
2020-04-18 07:26:48 +08:00
|
|
|
)
|
|
|
|
}
|