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

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