-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest
executable file
·381 lines (310 loc) · 8.98 KB
/
test
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env -S -i /bin/bash
set -e
set -u
BASE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ISMAC=$(test "$(uname -s)" = "Darwin" && echo "1" || echo "")
ISLINUX=$(test "$(uname -s)" = "Linux" && echo "1" || echo "")
# We use `env -i` to disable inheritance of environment variables from
# the developer's own command-line, so that test cases are in control
# over what is and isn't set. But, this means PATH is also cleared.
# Add at least /usr/local, because Docker or Podman may be there.
export PATH="$PATH:/usr/local/bin/"
ENV_IGNORE=("_" "SHLVL" "PWD" "OLDPWD" "PATH")
clean_env() {
for name in $(compgen -e); do
if [[ ! " ${ENV_IGNORE[*]} " =~ " ${name} " ]]; then
# echo "... unset $name"
unset -v "$name"
fi
done
}
cd $BASE
./tests/test-fresh-npm
trim() {
read input
echo -n "${input%%[[:space:]]}"
}
pushd() {
# quiet
command pushd "$1" > /dev/null
}
popd() {
# quiet
command popd > /dev/null
}
title() {
clean_fs
clean_env
echo
echo "fresh-node test: $1..."
}
if_mac() {
test -n "$ISMAC" && $1 || echo " [skip: mac-only]"
}
if_linux() {
test -n "$ISLINUX" && $1 || echo " [skip: linux-only]"
}
assert_equals() {
actual="$1"
expected="$2"
if [[ "$actual" == "$expected" ]]; then
printf " equals \"$expected\"\n"
else
printf " \"${actual}\" == \"${expected}\"\n"
false
fi
}
assert_lastline() {
assert_equals "$(echo "$1" | tail -n2 | trim)" "$2"
}
assert_contains() {
printf " contains \"$2\"\n"
if [[ "$1" != *"$2"* ]]; then
printf "\n$1"
false
fi
}
assert_matches() {
printf " contains \"$2\" (regex)\n"
if [[ ! "$1" =~ $2 ]]; then
printf "\n$1"
false
fi
}
assert_msg() {
printf " $1\n"
}
clean_fs() {
rm -rf ./fixtures
}
trap clean_fs 0 ERR
title "install / install to ~/.local/bin"
{
mkdir -p fixtures/install/myhome
pushd fixtures/install
myhome="$PWD/myhome"
mypath="/bin:/usr/bin:$myhome/.local/bin"
result=$(FRESH_INTERNAL_TEST=1 PATH="$mypath" HOME="$myhome" $BASE/bin/fresh-install)
assert_matches "$result" "creating .*/myhome/.local/bin"
assert_contains "$result" "installing fresh-node"
assert_matches "$result" "Fresh .+ is now ready!"
result=$(PATH="/bin:/usr/bin:$myhome/.local/bin" fresh-node)
assert_lastline "$result" "Fake fresh-node"
popd
}
title "install / install to ~/bin"
{
mkdir -p fixtures/install/myhome
pushd fixtures/install
myhome="$PWD/myhome"
mypath="/bin:/usr/bin:$myhome/bin"
result=$(FRESH_INTERNAL_TEST=1 PATH="$mypath" HOME="$myhome" $BASE/bin/fresh-install)
assert_matches "$result" "creating .*/myhome/bin"
assert_contains "$result" "installing fresh-node"
assert_matches "$result" "Fresh .+ is now ready!"
result=$(PATH="/bin:/usr/bin:$myhome/bin" fresh-node)
assert_lastline "$result" "Fake fresh-node"
popd
}
title "install / cannot find a place"
{
mkdir -p fixtures/install/myhome
pushd fixtures/install
myhome="$PWD/myhome"
mypath="/bin:/usr/bin"
code=0
result=$(FRESH_INTERNAL_TEST=1 PATH="$mypath" HOME="$myhome" $BASE/bin/fresh-install 2>&1) || code="$?"
assert_contains "$result" "Tried the following places: /usr/local/bin, ~/"
assert_contains "$result" "fresh-install: Could not find writable installation folder"
assert_equals "$code" "1"
# must not create any directory or files unless in PATH
assert_msg "not create ~/bin/" && test ! -e "$myhome/bin"
assert_msg "not create ~/.local/" && test ! -e "$myhome/.local"
popd
}
title "basic invocation"
{
./bin/fresh-node18 -- exit > /dev/null
}
title "runcmd"
{
result=$(./bin/fresh-node18 -- echo 42)
assert_lastline "$result" 42
result=$(./bin/fresh-node18 -- 'echo 42')
assert_lastline "$result" 42
}
title "forward exit code"
{
code=0
./bin/fresh-node18 -- exit 42 > /dev/null || code="$?"
assert_equals "$code" "42"
}
# This verifies both that the documentation is up-to-date with the
# the current image, and that the current image offers the currently
# documented software and versions thereof.
title "packages / advertised software versions"
{
result_welcome=$(./bin/fresh-node18 -- exit)
# There are invisible control characters at the start and end of the output (window title setup),
# that we need to strip out for the "contains" assertion to work.
# This is currently done naturally by the output having a line break at the start and end,
# and grepping only for lines we are intersted in.
result_live=$(FRESH_INTERNAL_RUNCMD=_fresh_versions ./bin/fresh-node18 | grep --color=never '░')
assert_contains "$result_welcome" "$result_live"
}
title "environment / default CHROMIUM_FLAGS"
{
result=$(./bin/fresh-node18 -- 'echo $CHROMIUM_FLAGS')
assert_lastline "$result" "--no-sandbox"
}
title "environment / warn if no MW_ vars with -env-mw"
{
mkdir -p fixtures/myproject
pushd fixtures/myproject
result=$($BASE/bin/fresh-node18 -env-mw -- exit)
assert_contains "$result" "env: MW_*, MEDIAWIKI_* (none found)"
popd
}
title "environment / protect FOO by default"
{
export FOO="inherited"
result=$(./bin/fresh-node18 -- 'echo $FOO')
assert_lastline "$result" ""
}
title "environment / protect MW_FOO by default"
{
export MW_FOO="inherited"
result=$(./bin/fresh-node18 -- 'echo $MW_FOO')
assert_lastline "$result" ""
}
title "environment / inherit MW_FOO with -env"
{
export MW_FOO="inherited"
result=$(./bin/fresh-node18 -env -- 'echo $MW_FOO')
assert_lastline "$result" "inherited"
}
title "environment / inherit MEDIAWIKI_FOO with -env"
{
export MEDIAWIKI_FOO="inherited"
result=$(./bin/fresh-node18 -env -- 'echo $MEDIAWIKI_FOO')
assert_lastline "$result" "inherited"
}
title "environment / inherit MEDIAWIKI_BAR .env file with -env"
{
mkdir -p fixtures/myproject
pushd fixtures/myproject
echo "MEDIAWIKI_BAR=inherited" > .env
result=$($BASE/bin/fresh-node18 -env -- 'echo $MEDIAWIKI_BAR')
assert_lastline "$result" "inherited"
popd
}
title "environment / inherit MEDIAWIKI_BAZ .env file with -env, overriding exported var"
{
mkdir -p fixtures/myproject
pushd fixtures/myproject
export MEDIAWIKI_BAZ="inherited"
echo "MEDIAWIKI_BAZ=overridden" > .env
result=$($BASE/bin/fresh-node18 -env -- 'echo $MEDIAWIKI_BAZ')
assert_lastline "$result" "overridden"
popd
}
title "environment / warn if no BROWSERSTACK_ vars with -env-bs"
{
export FOO="inherited"
result=$($BASE/bin/fresh-node18 -env-bs -- exit)
assert_contains "$result" "env: BROWSERSTACK_* (none found)"
}
title "environment / inherit BROWSERSTACK vars with -env-bs"
{
export BROWSERSTACK_KEY="quay"
result=$($BASE/bin/fresh-node18 -env-bs -- 'echo $BROWSERSTACK_KEY')
assert_lastline "$result" "quay"
}
title "environment / warn if no SAUCE_ vars with -env-sauce"
{
export FOO="inherited"
result=$($BASE/bin/fresh-node18 -env-sauce -- exit)
assert_contains "$result" "env: SAUCE_* (none found)"
}
title "environment / inherit SAUCE vars with -env-sauce"
{
export SAUCE_ACCESS_KEY="caye"
result=$($BASE/bin/fresh-node18 -env-sauce -- 'echo $SAUCE_ACCESS_KEY')
assert_lastline "$result" "caye"
}
title "environment / protect FOO even with -env-mw"
{
export FOO="inherited"
result=$(./bin/fresh-node18 -env-mw -- 'echo $FOO')
assert_lastline "$result" ""
}
title "environment / inherit FOO with -env=FOO"
{
export FOO="one"
export FOOLING="two"
export BAR="nope"
export BAR_FOO="nah"
result=$(./bin/fresh-node18 -env=FOO -- 'echo $FOO $BAR $BAR_FOO $FOOLING')
assert_lastline "$result" "one two"
}
title "user / default (Linux)"
λ() {
host_uid=$(id -u)
result=$(./bin/fresh-node18 -- /usr/bin/id -u)
assert_lastline "$result" "$host_uid"
}
if_linux λ
# user ID will not be passed on macOS.
title "user / default (macOS)"
λ() {
result=$(./bin/fresh-node18 -- /usr/bin/id -un)
assert_lastline "$result" "nobody"
}
if_mac λ
title "user / with -root"
{
result=$(./bin/fresh-node18 -root -- /usr/bin/id -un)
assert_lastline "$result" "root"
}
title "mount / expose current directory as readable"
{
mkdir -p fixtures/myread
pushd fixtures/myread
echo "exposed content" > file.txt
result=$($BASE/bin/fresh-node18 -- cat /myread/file.txt) || true
assert_lastline "$result" "exposed content"
popd
}
title "mount / expose current directory as writable"
{
mkdir -p fixtures/mywrite
pushd fixtures/mywrite
echo "original content" > file.txt
$BASE/bin/fresh-node18 -- 'echo changed content > /mywrite/file.txt' > /dev/null || true
result=$(cat file.txt)
assert_lastline "$result" "changed content"
popd
}
title "mount / cannot write to .git subdirectory file"
{
mkdir -p fixtures/myproject/.git
pushd fixtures/myproject
echo "original content" > .git/file.txt
code=0
$BASE/bin/fresh-node18 -- 'echo changed content > /myproject/.git/file.txt' > /dev/null || code="$?"
result=$(cat .git/file.txt)
assert_lastline "$result" "original content"
assert_equals "$code" "2"
popd
}
title "mount / cannot create a .git subdirectory file"
{
mkdir -p fixtures/myproject/.git
pushd fixtures/myproject
code=0
$BASE/bin/fresh-node18 -- touch /myproject/.git/file.txt > /dev/null || code="$?"
result=$(test -f .git/file.txt && echo created file || echo not created)
assert_equals "$result" "not created"
assert_equals "$code" "1"
popd
}