OILS / spec / nix-idioms.test.sh View on Github | oilshell.org

197 lines, 94 significant
1## compare_shells: bash
2## oils_failures_allowed: 2
3
4#### var ref to array 'preHooks[@]'
5#
6# This idiom discussed on
7# https://github.com/NixOS/nixpkgs/pull/147629
8
9show() {
10 echo show
11
12 # These are actually different
13 argv.py ${!hooksSlice}
14
15 argv.py ${!hooksSlice+"${!hooksSlice}"}
16}
17
18hooksSlice='preHooks[@]'
19
20preHooks=()
21show
22
23preHooks=('foo bar' baz)
24show
25
26# WTF this exposes a difference? But not the test case below?
27
28# What's happening here?
29# Uncomment this and get an error in bash about hookSlice, even though we never
30# undefined it.
31
32#wtf=1
33#
34# line 6: !hooksSlice: unbound variable
35
36if test -n "$wtf"; then
37 # 4.4.0(1)-release
38 # echo $BASH_VERSION
39
40 set -u
41 preHooks=()
42 show
43
44 preHooks=('foo bar' baz)
45 show
46fi
47
48## STDOUT:
49show
50[]
51[]
52show
53['foo', 'bar', 'baz']
54['foo bar', 'baz']
55## END
56
57#### Similar to above with set -u
58show() {
59 echo show
60
61 # bash gives an error here - !hookSlice unbound, even though preHooks exists
62 # OSH currently does the "logical" thing
63
64 # NOT testing this -- I think this is WHAT NIX WORKS AROUND WITH
65 #argv.py ${!hooksSlice}
66
67 argv.py ${!hooksSlice+"${!hooksSlice}"}
68}
69
70hooksSlice='preHooks[@]'
71
72set -u
73preHooks=()
74show
75
76preHooks=('foo bar' baz)
77show
78
79## STDOUT:
80show
81[]
82show
83['foo bar', 'baz']
84## END
85
86#### ${!ref} to undefined array
87
88set -u
89shopt -s eval_unsafe_arith || true 2>/dev/null
90
91#preHooks=()
92hookSlice="preHooks[@]"
93
94argv.py ${!hookSlice+"${!hookSlice}"}
95
96for element in ${!hookSlice+"${!hookSlice}"}; do
97 echo $element
98done
99
100## STDOUT:
101[]
102## END
103
104#### ${!ref} to undefined string var is fatal, INCONSISTENT with array
105hookSlice='preHooks'
106
107argv.py ${!hookSlice}
108
109set -u
110
111argv.py ${!hookSlice}
112
113echo end
114
115## status: 1
116## STDOUT:
117[]
118## END
119
120#### export with dynamic var name +=
121
122orig() {
123 export NIX_LDFLAGS${role_post}+=" -L$1/lib64"
124}
125
126new() {
127 local var_name="NIX_LDFLAGS$role_post"
128 local value=" -L$1/lib64"
129
130 eval "$var_name"+='$value'
131 export "$var_name"
132}
133
134role_post='_foo'
135
136# set -u
137
138if test -n "${BASH_VERSION:-}"; then
139 orig one
140fi
141
142declare -p NIX_LDFLAGS_foo # inspect it
143unset NIX_LDFLAGS_foo
144
145new one
146
147declare -p NIX_LDFLAGS_foo # inspect it
148
149## STDOUT:
150declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
151declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
152## END
153## OK osh STDOUT:
154declare -x NIX_LDFLAGS_foo=' -Lone/lib64'
155## END
156
157#### let idiom can be written in POSIX shell - eval ": \$(( ))"
158
159for i in 0 1 2; do
160 echo i=$i
161
162 NIX_ENFORCE_NO_NATIVE=$i
163
164 outputVar=NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu
165 inputVar=NIX_ENFORCE_NO_NATIVE
166
167 # Original Nix idiom
168
169 if test -n "$BASH_VERSION"; then
170 let "${outputVar} |= ${!inputVar:-0}" "1"
171 else
172 # OSH alternative
173 eval ": \$(( ${outputVar} |= ${!inputVar:-0} ))"
174 fi
175
176 echo NIX_ENFORCE_NO_NATIVE=$NIX_ENFORCE_NO_NATIVE
177 echo NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=$NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu
178 echo
179
180done
181
182
183## STDOUT:
184i=0
185NIX_ENFORCE_NO_NATIVE=0
186NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=0
187
188i=1
189NIX_ENFORCE_NO_NATIVE=1
190NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=1
191
192i=2
193NIX_ENFORCE_NO_NATIVE=2
194NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=3
195
196## END
197