blob: 6608c34d84c588588c91454e9a8bfdc82ae22c46 (
plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#!/usr/bin/env bash
set -euo pipefail
declare -r REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
declare -r PHOTOALBUM="${PHOTOALBUM:-$REPO_ROOT/bin/photoalbum}"
TEST_TMPDIR="${TEST_TMPDIR:-}"
setup() {
TEST_TMPDIR=$(mktemp -d)
}
teardown() {
if [ -n "$TEST_TMPDIR" ]; then
rm -rf "$TEST_TMPDIR"
fi
}
run_test() {
local -r description="$1"; shift
local output_file
local -i status=0
output_file=$(mktemp)
set +e
( set -euo pipefail; trap teardown EXIT; "$@" ) > "$output_file" 2>&1
status=$?
set -e
if (( status != 0 )); then
echo "FAIL: $description" >&2
cat "$output_file" >&2
rm -f "$output_file"
exit 1
fi
rm -f "$output_file"
}
assert_failure() {
local -r description="$1"; shift
local output_file
local -i status=0
output_file=$(mktemp)
set +e
"$@" > "$output_file" 2>&1
status=$?
set -e
if (( status == 0 )); then
echo "FAIL: $description" >&2
cat "$output_file" >&2
rm -f "$output_file"
exit 1
fi
rm -f "$output_file"
}
capture_failure_output() {
local output
local output_file
local -i status=0
output_file=$(mktemp)
set +e
"$@" > "$output_file" 2>&1
status=$?
set -e
if (( status == 0 )); then
echo 'FAIL: expected command to fail' >&2
output=$(<"$output_file")
echo "$output" >&2
rm -f "$output_file"
exit 1
fi
output=$(<"$output_file")
rm -f "$output_file"
printf '%s\n' "$output"
}
assert_contains() {
local -r needle="$1"; shift
local -r haystack="$1"; shift
if [[ "$haystack" != *"$needle"* ]]; then
echo "FAIL: expected output to contain $needle" >&2
echo "$haystack" >&2
exit 1
fi
}
assert_not_contains() {
local -r needle="$1"; shift
local -r haystack="$1"; shift
if [[ "$haystack" == *"$needle"* ]]; then
echo "FAIL: expected output not to contain $needle" >&2
echo "$haystack" >&2
exit 1
fi
}
run_photoalbum() {
"$PHOTOALBUM" "$@" 2>&1
}
test_version() {
local output
output=$(run_photoalbum --version)
assert_contains 'This is Photoalbum Version' "$output"
}
test_init() {
setup
(
cd "$TEST_TMPDIR"
PHOTOALBUM_DEFAULT_RC="$TEST_TMPDIR/missing" \
"$PHOTOALBUM" --init >/dev/null
test -f photoalbum.conf
test ! -f Makefile
grep -q \
"^TEMPLATE_DIR=$REPO_ROOT/share/templates/default$" \
photoalbum.conf
)
teardown
}
test_clean() {
setup
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR" \
> "$TEST_TMPDIR/photoalbum.conf"
mkdir -p "$TEST_TMPDIR/dist"
(
cd "$TEST_TMPDIR"
"$PHOTOALBUM" --clean
test ! -e "$TEST_TMPDIR/dist"
)
teardown
}
test_clean_with_config() {
local config_file
setup
config_file="$TEST_TMPDIR/custom.conf"
printf 'DIST_DIR=%q/custom-dist\n' "$TEST_TMPDIR" > "$config_file"
mkdir -p "$TEST_TMPDIR/custom-dist"
(
cd "$TEST_TMPDIR"
"$PHOTOALBUM" --clean --config "$config_file"
test ! -e "$TEST_TMPDIR/custom-dist"
)
teardown
}
test_clean_missing_config_fails() {
local output
setup
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR" > "$TEST_TMPDIR/photoalbumrc"
output=$(
cd "$TEST_TMPDIR"
capture_failure_output "$PHOTOALBUM" --clean
)
assert_contains 'Error: Can not find config file ./photoalbum.conf' "$output"
assert_contains 'Run photoalbum --init to create ./photoalbum.conf.' "$output"
teardown
}
test_generate_with_config_missing_incoming_fails() {
local config_file
local output
setup
config_file="$TEST_TMPDIR/custom.conf"
{
printf 'INCOMING_DIR=%q/missing\n' "$TEST_TMPDIR"
printf 'DIST_DIR=%q/custom-dist\n' "$TEST_TMPDIR"
} > "$config_file"
output=$(
cd "$TEST_TMPDIR"
capture_failure_output "$PHOTOALBUM" --generate --config "$config_file"
)
assert_contains "ERROR: You have to create $TEST_TMPDIR/missing first" \
"$output"
teardown
}
test_generate_missing_incoming_fails() {
setup
{
printf 'INCOMING_DIR=%q/missing\n' "$TEST_TMPDIR"
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR"
} > "$TEST_TMPDIR/photoalbum.conf"
(
cd "$TEST_TMPDIR"
assert_failure \
'--generate fails when INCOMING_DIR is missing' \
"$PHOTOALBUM" --generate
)
teardown
}
test_generate_escapes_html_values() {
local config_file
local css_photo
local fake_bin
local original_basepath
local original_basepath_html
local page_html
local photo_html
local photo_name
local title
local title_html
local view_html
setup
fake_bin="$TEST_TMPDIR/bin"
config_file="$TEST_TMPDIR/photoalbum.conf"
photo_name="kid's_\"<tag>&.jpg"
photo_html='kid's_"<tag>&.jpg'
css_photo='kid\000027s_\000022\00003ctag\00003e\000026.jpg'
title="A & \"quoted\" <title> 'ok'"
title_html='A & "quoted" <title> 'ok''
original_basepath="https://example.test/original?album=\"<x>&owner=O'Neil"
original_basepath_html='https://example.test/original?album="<x>&owner=O'Neil'
mkdir -p "$fake_bin" "$TEST_TMPDIR/incoming"
cat > "$fake_bin/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail
dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
printf 'fake image\n' > "$dest"
MAGICK
chmod 0755 "$fake_bin/magick"
printf 'fake image\n' > "$TEST_TMPDIR/incoming/$photo_name"
{
printf 'TITLE=%q\n' "$title"
printf 'THUMBHEIGHT=30\n'
printf 'HEIGHT=120\n'
printf 'MAXPREVIEWS=40\n'
printf 'INCOMING_DIR=%q/incoming\n' "$TEST_TMPDIR"
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR"
printf 'TEMPLATE_DIR=%q/share/templates/default\n' "$REPO_ROOT"
printf 'ORIGINAL_BASEPATH=%q\n' "$original_basepath"
printf 'TARBALL_INCLUDE=yes\n'
printf 'TARBALL_SUFFIX=%q\n' '&"'\''.tar'
} > "$config_file"
(
cd "$TEST_TMPDIR"
PATH="$fake_bin:$PATH" "$PHOTOALBUM" --generate
)
page_html=$(<"$TEST_TMPDIR/dist/html/page-1.html")
view_html=$(<"$TEST_TMPDIR/dist/html/1-1.html")
assert_contains "<title>$title_html</title>" "$page_html"
assert_contains \
"background-image: url(\"../blurs/$css_photo\");" \
"$page_html"
assert_contains "name='$photo_html'" "$page_html"
assert_contains "src='../thumbs/$photo_html'" "$page_html"
assert_contains '&"'.tar' "$page_html"
assert_contains "href=\"page-1.html#$photo_html\"" "$view_html"
assert_contains "href ='../photos/$photo_html'" "$view_html"
assert_contains \
"href=\"$original_basepath_html/$photo_html\"" \
"$view_html"
assert_not_contains '<title>A & "quoted" <title>' "$page_html"
assert_not_contains "$photo_name" "$view_html"
teardown
}
test_generate_preserves_space_filename_without_reprocessing() {
local config_file
local fake_bin
local first_output
local photo_name
local second_output
setup
fake_bin="$TEST_TMPDIR/bin"
config_file="$TEST_TMPDIR/photoalbum.conf"
photo_name='a b.jpg'
mkdir -p "$fake_bin" "$TEST_TMPDIR/incoming"
cat > "$fake_bin/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail
dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
printf 'fake image\n' > "$dest"
MAGICK
chmod 0755 "$fake_bin/magick"
printf 'fake image\n' > "$TEST_TMPDIR/incoming/$photo_name"
{
printf 'TITLE=%q\n' 'Space test'
printf 'THUMBHEIGHT=30\n'
printf 'HEIGHT=120\n'
printf 'MAXPREVIEWS=40\n'
printf 'INCOMING_DIR=%q/incoming\n' "$TEST_TMPDIR"
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR"
printf 'TEMPLATE_DIR=%q/share/templates/default\n' "$REPO_ROOT"
printf 'TARBALL_INCLUDE=no\n'
} > "$config_file"
first_output=$(
cd "$TEST_TMPDIR"
PATH="$fake_bin:$PATH" "$PHOTOALBUM" --generate
)
second_output=$(
cd "$TEST_TMPDIR"
PATH="$fake_bin:$PATH" "$PHOTOALBUM" --generate
)
test -f "$TEST_TMPDIR/dist/photos/$photo_name"
test ! -e "$TEST_TMPDIR/dist/photos/a_b.jpg"
assert_contains "Processing $photo_name to" "$first_output"
assert_contains "Already exists: $TEST_TMPDIR/dist/photos/$photo_name" \
"$second_output"
assert_not_contains "Processing $photo_name to" "$second_output"
teardown
}
test_generate_handles_space_and_underscore_names_distinctly() {
local config_file
local fake_bin
local page_html
setup
fake_bin="$TEST_TMPDIR/bin"
config_file="$TEST_TMPDIR/photoalbum.conf"
mkdir -p "$fake_bin" "$TEST_TMPDIR/incoming"
cat > "$fake_bin/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail
dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
printf 'fake image\n' > "$dest"
MAGICK
chmod 0755 "$fake_bin/magick"
printf 'fake image\n' > "$TEST_TMPDIR/incoming/a b.jpg"
printf 'fake image\n' > "$TEST_TMPDIR/incoming/a_b.jpg"
{
printf 'TITLE=%q\n' 'Collision test'
printf 'THUMBHEIGHT=30\n'
printf 'HEIGHT=120\n'
printf 'MAXPREVIEWS=40\n'
printf 'INCOMING_DIR=%q/incoming\n' "$TEST_TMPDIR"
printf 'DIST_DIR=%q/dist\n' "$TEST_TMPDIR"
printf 'TEMPLATE_DIR=%q/share/templates/default\n' "$REPO_ROOT"
printf 'TARBALL_INCLUDE=no\n'
} > "$config_file"
(
cd "$TEST_TMPDIR"
PATH="$fake_bin:$PATH" "$PHOTOALBUM" --generate
)
page_html=$(<"$TEST_TMPDIR/dist/html/page-1.html")
test -f "$TEST_TMPDIR/dist/photos/a b.jpg"
test -f "$TEST_TMPDIR/dist/photos/a_b.jpg"
test -f "$TEST_TMPDIR/dist/thumbs/a b.jpg"
test -f "$TEST_TMPDIR/dist/thumbs/a_b.jpg"
assert_contains "src='../thumbs/a b.jpg'" "$page_html"
assert_contains "src='../thumbs/a_b.jpg'" "$page_html"
teardown
}
test_positional_commands_fail() {
assert_failure 'positional clean is rejected' "$PHOTOALBUM" clean
assert_failure 'positional generate is rejected' "$PHOTOALBUM" generate
assert_failure 'positional version is rejected' "$PHOTOALBUM" version
}
test_extra_args_fail() {
assert_failure 'extra operand is rejected' "$PHOTOALBUM" --version extra
assert_failure 'unsupported option is rejected' "$PHOTOALBUM" --unknown
assert_failure 'missing config value is rejected' "$PHOTOALBUM" --config
assert_failure \
'--config is rejected with --init' \
"$PHOTOALBUM" --init --config custom.conf
}
main() {
trap teardown EXIT
run_test '--version succeeds' test_version
run_test '--init succeeds' test_init
run_test '--clean succeeds' test_clean
run_test '--clean --config succeeds' test_clean_with_config
run_test '--clean missing config fails clearly' test_clean_missing_config_fails
run_test \
'--generate --config reads selected config' \
test_generate_with_config_missing_incoming_fails
run_test \
'--generate missing incoming fails' \
test_generate_missing_incoming_fails
run_test \
'--generate escapes generated HTML values' \
test_generate_escapes_html_values
run_test \
'--generate preserves filenames with spaces without reprocessing' \
test_generate_preserves_space_filename_without_reprocessing
run_test \
'--generate handles spaces and underscores distinctly' \
test_generate_handles_space_and_underscore_names_distinctly
run_test 'positional commands fail' test_positional_commands_fail
run_test 'extra args fail' test_extra_args_fail
}
main "$@"
|