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