1 | #### Turn off external binaries with shvar PATH='' {}
|
2 | shopt --set parse_brace parse_proc
|
3 |
|
4 | echo hi > file
|
5 |
|
6 | # Note: this CACHES the lookup, so shvar has to clear cache when modifying it
|
7 | cp -v file /tmp >&2
|
8 | echo 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 |
|
15 | shvar 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
|
25 | cp -v file /tmp >&2
|
26 | echo status=$?
|
27 |
|
28 | ## STDOUT:
|
29 | status=0
|
30 | status=127
|
31 | status=127
|
32 | status=0
|
33 | ## END
|
34 |
|
35 | #### More shvar PATH=''
|
36 | shopt --set parse_brace command_sub_errexit parse_proc
|
37 |
|
38 | shvar 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:
|
54 | status=127
|
55 | status=127
|
56 | _status 127
|
57 | ## END
|
58 |
|
59 |
|
60 | #### builtins and externals not available in hay eval
|
61 | shopt --set parse_brace
|
62 | shopt --unset errexit
|
63 |
|
64 | hay define Package
|
65 |
|
66 | try {
|
67 | hay eval :result {
|
68 | Package foo {
|
69 | /bin/ls
|
70 | }
|
71 | }
|
72 | }
|
73 | echo "status $_status"
|
74 |
|
75 | try {
|
76 | hay eval :result {
|
77 | cd /tmp
|
78 | }
|
79 | }
|
80 | echo "status $_status"
|
81 |
|
82 | ## STDOUT:
|
83 | status 127
|
84 | status 127
|
85 | ## END
|
86 |
|
87 | #### procs in hay eval
|
88 | shopt --set parse_brace parse_at parse_proc
|
89 |
|
90 | hay define Package
|
91 |
|
92 | proc outside {
|
93 | echo outside
|
94 | Package OUT
|
95 | }
|
96 |
|
97 | hay eval :result {
|
98 | outside
|
99 |
|
100 | proc inside {
|
101 | echo inside
|
102 | }
|
103 |
|
104 | inside
|
105 | }
|
106 |
|
107 | const args = result['children'][0]['args']
|
108 | write --sep ' ' -- $[len(result['children'])] @args
|
109 |
|
110 | ## STDOUT:
|
111 | outside
|
112 | inside
|
113 | 1 OUT
|
114 | ## END
|
115 |
|
116 |
|
117 | #### variables mutated within hay eval don't persist
|
118 | shopt --set parse_brace
|
119 |
|
120 | hay define Package
|
121 |
|
122 | setvar x = 42
|
123 |
|
124 | hay eval :result {
|
125 | Package foo
|
126 |
|
127 | setvar x = 1
|
128 | }
|
129 |
|
130 | echo "x = $x"
|
131 |
|
132 | ## STDOUT:
|
133 | x = 42
|
134 | ## END
|
135 |
|
136 |
|
137 |
|
138 | #### hay at top level allows arbitrary commands
|
139 | shopt --set parse_brace
|
140 |
|
141 | hay define Package
|
142 |
|
143 | Package $(seq 2) {
|
144 | seq 3 4
|
145 | }
|
146 |
|
147 | json write (_hay()) | jq '.children[0].args' > actual.txt
|
148 |
|
149 | diff -u - actual.txt <<EOF
|
150 | [
|
151 | "1",
|
152 | "2"
|
153 | ]
|
154 | EOF
|
155 |
|
156 | hay eval :result {
|
157 | echo inside
|
158 | Package $(seq 2) {
|
159 | seq 3 4
|
160 | }
|
161 | }
|
162 |
|
163 | ## status: 127
|
164 | ## STDOUT:
|
165 | 3
|
166 | 4
|
167 | inside
|
168 | ## END
|
169 |
|