1 |
#!/usr/bin/env bash |
2 |
|
3 |
#### Eval |
4 |
eval "a=3" |
5 |
echo $a |
6 |
## stdout: 3 |
7 |
|
8 |
#### Source |
9 |
lib=$TMP/spec-test-lib.sh |
10 |
echo 'LIBVAR=libvar' > $lib |
11 |
. $lib # dash doesn't have source |
12 |
echo $LIBVAR |
13 |
## stdout: libvar |
14 |
|
15 |
#### Source nonexistent |
16 |
source /nonexistent/path |
17 |
echo status=$? |
18 |
## stdout: status=1 |
19 |
## OK dash/zsh stdout: status=127 |
20 |
|
21 |
#### Source with no arguments |
22 |
source |
23 |
echo status=$? |
24 |
## stdout: status=1 |
25 |
## OK bash stdout: status=2 |
26 |
## OK dash stdout: status=127 |
27 |
|
28 |
#### Source with arguments |
29 |
. spec/testdata/show-argv.sh foo bar # dash doesn't have source |
30 |
## STDOUT: |
31 |
show-argv: foo bar |
32 |
## END |
33 |
## N-I dash STDOUT: |
34 |
show-argv: |
35 |
## END |
36 |
|
37 |
#### Source from a function, mutating argv and defining a local var |
38 |
f() { |
39 |
. spec/testdata/source-argv.sh # no argv |
40 |
. spec/testdata/source-argv.sh args to src # new argv |
41 |
echo $@ |
42 |
echo foo=$foo # defined in source-argv.sh |
43 |
} |
44 |
f args to func |
45 |
echo foo=$foo # not defined |
46 |
## STDOUT: |
47 |
source-argv: args to func |
48 |
source-argv: args to src |
49 |
to func |
50 |
foo=foo_val |
51 |
foo= |
52 |
## END |
53 |
## N-I dash STDOUT: |
54 |
source-argv: args to func |
55 |
source-argv: to func |
56 |
func |
57 |
foo=foo_val |
58 |
foo= |
59 |
## END |
60 |
|
61 |
#### Source with syntax error |
62 |
# TODO: We should probably use dash behavior of a fatal error. |
63 |
# Although set-o errexit handles this. We don't want to break the invariant |
64 |
# that a builtin like 'source' behaves like an external program. An external |
65 |
# program can't halt the shell! |
66 |
echo 'echo >' > $TMP/syntax-error.sh |
67 |
. $TMP/syntax-error.sh |
68 |
echo status=$? |
69 |
## stdout: status=2 |
70 |
## OK bash/mksh stdout: status=1 |
71 |
## OK zsh stdout: status=126 |
72 |
## OK dash stdout-json: "" |
73 |
## OK dash status: 2 |
74 |
|
75 |
#### Eval with syntax error |
76 |
eval 'echo >' |
77 |
echo status=$? |
78 |
## stdout: status=2 |
79 |
## OK bash/zsh stdout: status=1 |
80 |
## OK dash stdout-json: "" |
81 |
## OK dash status: 2 |
82 |
## OK mksh stdout-json: "" |
83 |
## OK mksh status: 1 |