OILS / spec / hay-isolation.test.sh View on Github | oilshell.org

169 lines, 94 significant
1#### Turn off external binaries with shvar PATH='' {}
2shopt --set parse_brace parse_proc
3
4echo hi > file
5
6# Note: this CACHES the lookup, so shvar has to clear cache when modifying it
7cp -v file /tmp >&2
8echo status=$?
9
10# TODO: implement this, and call it whenever shvar mutates PATH?
11# what about when PATH is mutated? No leave it out for now.
12
13# hash -r # clear the cache, no longer necessary
14
15shvar PATH='' {
16 cp -v file /tmp
17 echo status=$?
18
19 # this also doesn't work
20 command cp -v file /tmp
21 echo status=$?
22}
23
24# Now it's available again
25cp -v file /tmp >&2
26echo status=$?
27
28## STDOUT:
29status=0
30status=127
31status=127
32status=0
33## END
34
35#### More shvar PATH=''
36shopt --set parse_brace command_sub_errexit parse_proc
37
38shvar PATH='' {
39 ( cp -v file /tmp >&2 )
40 echo status=$?
41
42 forkwait {
43 cp -v file /tmp >&2
44 }
45 echo status=$?
46
47 try {
48 true $(cp -v file /tmp >&2)
49 }
50 echo _status $_status
51}
52
53## STDOUT:
54status=127
55status=127
56_status 127
57## END
58
59
60#### builtins and externals not available in hay eval
61shopt --set parse_brace
62shopt --unset errexit
63
64hay define Package
65
66try {
67 hay eval :result {
68 Package foo {
69 /bin/ls
70 }
71 }
72}
73echo "status $_status"
74
75try {
76 hay eval :result {
77 cd /tmp
78 }
79}
80echo "status $_status"
81
82## STDOUT:
83status 127
84status 127
85## END
86
87#### procs in hay eval
88shopt --set parse_brace parse_at parse_proc
89
90hay define Package
91
92proc outside {
93 echo outside
94 Package OUT
95}
96
97hay eval :result {
98 outside
99
100 proc inside {
101 echo inside
102 }
103
104 inside
105}
106
107const args = result['children'][0]['args']
108write --sep ' ' -- $[len(result['children'])] @args
109
110## STDOUT:
111outside
112inside
1131 OUT
114## END
115
116
117#### variables mutated within hay eval don't persist
118shopt --set parse_brace
119
120hay define Package
121
122setvar x = 42
123
124hay eval :result {
125 Package foo
126
127 setvar x = 1
128}
129
130echo "x = $x"
131
132## STDOUT:
133x = 42
134## END
135
136
137
138#### hay at top level allows arbitrary commands
139shopt --set parse_brace
140
141hay define Package
142
143Package $(seq 2) {
144 seq 3 4
145}
146
147json write (_hay()) | jq '.children[0].args' > actual.txt
148
149diff -u - actual.txt <<EOF
150[
151 "1",
152 "2"
153]
154EOF
155
156hay eval :result {
157 echo inside
158 Package $(seq 2) {
159 seq 3 4
160 }
161}
162
163## status: 127
164## STDOUT:
1653
1664
167inside
168## END
169