summaryrefslogtreecommitdiff
path: root/tests/helpers.sh
blob: bb52b6739e65762f12cbbf4ab945ad4621478b36 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/env bash
set -euo pipefail

: "${TEST_REPO_ROOT:=${REPO_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}}"
: "${TEST_PHOTOALBUM:=${PHOTOALBUM:-$TEST_REPO_ROOT/bin/photoalbum}}"
: "${TEST_IMAGEMAGICK:=magick}"
: "${TEST_TMPDIR:=}"

test::setup() {
    TEST_TMPDIR=$(mktemp -d)
}

test::teardown() {
    if [ -n "$TEST_TMPDIR" ]; then
        rm -rf "$TEST_TMPDIR"
        TEST_TMPDIR=''
    fi
}

test::run_case() {
    local -r description="$1"; shift
    local output_file
    local -i status=0

    output_file=$(mktemp)
    set +e
    ( set -euo pipefail; trap test::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"
}

test::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"
}

test::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"
}

test::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
}

test::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
}

test::assert_contains_before() {
    local -r first="$1"; shift
    local -r second="$1"; shift
    local -r haystack="$1"; shift
    local before_first

    before_first="${haystack%%"$first"*}"

    if [[ "$haystack" != *"$first"* \
        || "$haystack" != *"$second"* \
        || "$before_first" == *"$second"* ]]; then
        echo "FAIL: expected $first to appear before $second" >&2
        echo "$haystack" >&2
        exit 1
    fi
}

test::assert_file_exists() {
    local -r path="$1"; shift

    if [ ! -f "$path" ]; then
        echo "FAIL: expected file $path to exist" >&2
        exit 1
    fi
}

test::assert_dir_exists() {
    local -r path="$1"; shift

    if [ ! -d "$path" ]; then
        echo "FAIL: expected directory $path to exist" >&2
        exit 1
    fi
}

test::assert_path_absent() {
    local -r path="$1"; shift

    if [ -e "$path" ]; then
        echo "FAIL: expected $path to be absent" >&2
        exit 1
    fi
}

test::assert_find_count() {
    local -r expected="$1"; shift
    local -r dir="$1"; shift
    local -r name="$1"; shift
    local actual

    actual=$(find "$dir" -maxdepth 1 -type f -name "$name" | wc -l)

    if [ "$actual" -ne "$expected" ]; then
        echo "FAIL: expected $expected files matching $name in $dir" >&2
        echo "found $actual" >&2
        exit 1
    fi
}

test::assert_no_staging_dirs() {
    local -r dir="$1"; shift
    local found

    found=$(find "$dir" -type d -name '.photoalbum.*' -print -quit)

    if [ -n "$found" ]; then
        echo "FAIL: expected no staging directories under $dir" >&2
        echo "found $found" >&2
        exit 1
    fi
}

test::run_photoalbum() {
    "$TEST_PHOTOALBUM" "$@" 2>&1
}

test::install_fake_imagemagick() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"
    TEST_IMAGEMAGICK="$bin_dir/magick"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = identify ]; then
    if [ -n "${TEST_IMAGEMAGICK_IDENTIFY_LOG:-}" ]; then
        printf '%s\n' "$*" >> "$TEST_IMAGEMAGICK_IDENTIFY_LOG"
    fi
    if [ -n "${TEST_IMAGEMAGICK_IDENTIFY_OUTPUT:-}" ]; then
        printf '%s\n' "$TEST_IMAGEMAGICK_IDENTIFY_OUTPUT"
    fi
    exit 0
fi

dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
{
    printf 'fake image\n'
    printf 'args:'
    printf ' %q' "$@"
    printf '\n'
} > "$dest"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_parallel_imagemagick_spy() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = identify ]; then
    exit 0
fi

lock_file="${TEST_PARALLEL_MAGICK_LOCK:?}"
active_file="${TEST_PARALLEL_MAGICK_ACTIVE:?}"
max_file="${TEST_PARALLEL_MAGICK_MAX:?}"
log_file="${TEST_PARALLEL_MAGICK_LOG:?}"

(
    flock 9
    active=0
    if [ -f "$active_file" ]; then
        active=$(<"$active_file")
    fi
    active=$(( active + 1 ))
    printf '%s\n' "$active" > "$active_file"

    max=0
    if [ -f "$max_file" ]; then
        max=$(<"$max_file")
    fi
    if (( active > max )); then
        printf '%s\n' "$active" > "$max_file"
    fi

    printf 'start %s %s\n' "$active" "$*" >> "$log_file"
) 9>"$lock_file"

sleep 0.1

dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
{
    printf 'fake image\n'
    printf 'args:'
    printf ' %q' "$@"
    printf '\n'
} > "$dest"

(
    flock 9
    active=$(<"$active_file")
    active=$(( active - 1 ))
    printf '%s\n' "$active" > "$active_file"
    printf 'finish %s %s\n' "$active" "$*" >> "$log_file"
) 9>"$lock_file"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_wait_n_imagemagick_spy() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = identify ]; then
    exit 0
fi

lock_file="${TEST_WAIT_N_MAGICK_LOCK:?}"
log_file="${TEST_WAIT_N_MAGICK_LOG:?}"
dest="${@: -1}"
dest_name="$(basename "$dest")"
dest_dir="$(basename "$(dirname "$dest")")"
dest_label="$dest_dir/$dest_name"

(
    flock 9
    printf 'start %s\n' "$dest_label" >> "$log_file"
) 9>"$lock_file"

if [ "$dest_label" = 'photos/01.jpg' ]; then
    sleep "${TEST_WAIT_N_SLOW_SECONDS:-0.4}"
else
    sleep "${TEST_WAIT_N_FAST_SECONDS:-0.02}"
fi

mkdir -p "$(dirname "$dest")"
{
    printf 'fake image\n'
    printf 'args:'
    printf ' %q' "$@"
    printf '\n'
} > "$dest"

(
    flock 9
    printf 'finish %s\n' "$dest_label" >> "$log_file"
) 9>"$lock_file"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_parallel_identify_spy() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = identify ]; then
    lock_file="${TEST_PARALLEL_IDENTIFY_LOCK:?}"
    active_file="${TEST_PARALLEL_IDENTIFY_ACTIVE:?}"
    max_file="${TEST_PARALLEL_IDENTIFY_MAX:?}"
    log_file="${TEST_PARALLEL_IDENTIFY_LOG:?}"

    (
        flock 9
        active=0
        if [ -f "$active_file" ]; then
            active=$(<"$active_file")
        fi
        active=$(( active + 1 ))
        printf '%s\n' "$active" > "$active_file"

        max=0
        if [ -f "$max_file" ]; then
            max=$(<"$max_file")
        fi
        if (( active > max )); then
            printf '%s\n' "$active" > "$max_file"
        fi

        printf 'start %s %s\n' "$active" "$*" >> "$log_file"
    ) 9>"$lock_file"

    sleep 0.1
    printf '  exif:Make: ParallelCam\n'

    (
        flock 9
        active=$(<"$active_file")
        active=$(( active - 1 ))
        printf '%s\n' "$active" > "$active_file"
        printf 'finish %s %s\n' "$active" "$*" >> "$log_file"
    ) 9>"$lock_file"
    exit 0
fi

dest="${@: -1}"
mkdir -p "$(dirname "$dest")"
{
    printf 'fake image\n'
    printf 'args:'
    printf ' %q' "$@"
    printf '\n'
} > "$dest"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_failing_imagemagick() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

echo 'simulated ImageMagick failure' >&2
exit 42
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_hanging_imagemagick() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

sleep "${TEST_HANG_SECONDS:-5}"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_blocking_imagemagick() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/magick" <<'MAGICK'
#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = identify ]; then
    exit 0
fi

if [ -n "${TEST_BLOCKING_MAGICK_TERMINATED:-}" ]; then
    trap 'printf "terminated\n" > "$TEST_BLOCKING_MAGICK_TERMINATED"; exit 143' \
        INT TERM HUP
fi

printf 'started\n' > "${TEST_BLOCKING_MAGICK_STARTED:?}"
while [ ! -e "${TEST_BLOCKING_MAGICK_RELEASE:?}" ]; do
    sleep 0.1
done
exit "${TEST_BLOCKING_MAGICK_EXIT:-42}"
MAGICK
    chmod 0755 "$bin_dir/magick"
    cp "$bin_dir/magick" "$bin_dir/convert"
}

test::install_failing_generation_tools() {
    local -r bin_dir="$1"; shift
    local name

    mkdir -p "$bin_dir"

    for name in magick convert tar; do
        cat > "$bin_dir/$name" <<'TOOL'
#!/usr/bin/env bash
set -euo pipefail

if [ -n "${TEST_FORBIDDEN_TOOL_LOG:-}" ]; then
    printf 'called %s\n' "$(basename "$0")" >> "$TEST_FORBIDDEN_TOOL_LOG"
fi
echo "unexpected generation tool invocation: $(basename "$0")" >&2
exit 97
TOOL
        chmod 0755 "$bin_dir/$name"
    done
}

test::install_mv_spy() {
    local -r bin_dir="$1"; shift
    local real_mv

    mkdir -p "$bin_dir"
    real_mv=$(command -v mv)

    cat > "$bin_dir/mv" <<MV
#!/usr/bin/env bash
set -euo pipefail

count=0
if [ -n "\${TEST_MV_COUNT_FILE:-}" ] && [ -f "\$TEST_MV_COUNT_FILE" ]; then
    count=\$(<"\$TEST_MV_COUNT_FILE")
fi
count=\$(( count + 1 ))
if [ -n "\${TEST_MV_COUNT_FILE:-}" ]; then
    printf '%s\n' "\$count" > "\$TEST_MV_COUNT_FILE"
fi

if [[ -n "\${TEST_FAIL_MV_ON:-}" && "\$count" = "\$TEST_FAIL_MV_ON" ]]; then
    echo 'simulated mv failure' >&2
    exit 42
fi

"$real_mv" "\$@"
MV
    chmod 0755 "$bin_dir/mv"
}

test::install_sort_spy() {
    local -r bin_dir="$1"; shift
    local real_sort

    mkdir -p "$bin_dir"
    real_sort=$(command -v sort)

    cat > "$bin_dir/sort" <<SORT
#!/usr/bin/env bash
set -euo pipefail

input_file=\$(mktemp)
cat > "\$input_file"

if [[ -n "\${TEST_SORT_LOG:-}" && " \$* " == *" -R "* ]] \\
    && grep -q '[.]jpg$' "\$input_file"; then
    printf 'photo-shuffle %s\n' "\$*" >> "\$TEST_SORT_LOG"
    tac "\$input_file"
    rm -f "\$input_file"
    exit 0
fi

"$real_sort" "\$@" "\$input_file"
rm -f "\$input_file"
SORT
    chmod 0755 "$bin_dir/sort"
}

test::install_tar_spy() {
    local -r bin_dir="$1"; shift
    local real_tar

    mkdir -p "$bin_dir"
    real_tar=$(command -v tar)

    cat > "$bin_dir/tar" <<TAR
#!/usr/bin/env bash
set -euo pipefail

{
    printf 'argc=%s\n' "\$#"
    i=0
    for arg in "\$@"; do
        printf 'arg%s=%q\n' "\$i" "\$arg"
        i=\$(( i + 1 ))
    done
} >> "\$TEST_TAR_LOG"

"$real_tar" "\$@"
TAR
    chmod 0755 "$bin_dir/tar"
}

test::install_rsync_spy() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/rsync" <<'RSYNC'
#!/usr/bin/env bash
set -euo pipefail

{
    printf 'argc=%s\n' "$#"
    i=0
    for arg in "$@"; do
        printf 'arg%s=%q\n' "$i" "$arg"
        i=$(( i + 1 ))
    done
} >> "$TEST_RSYNC_LOG"
RSYNC
    chmod 0755 "$bin_dir/rsync"
}

test::install_hanging_tar() {
    local -r bin_dir="$1"; shift

    mkdir -p "$bin_dir"

    cat > "$bin_dir/tar" <<'TAR'
#!/usr/bin/env bash
set -euo pipefail

sleep "${TEST_HANG_SECONDS:-5}"
TAR
    chmod 0755 "$bin_dir/tar"
}

test::install_coreutils_without_imagemagick() {
    local -r bin_dir="$1"; shift
    local command_path
    local name
    local -a names=(
        basename
        bash
        date
        dirname
        find
        grep
        mkdir
        mktemp
        rm
        sed
        sort
        tac
        tar
        wc
    )

    mkdir -p "$bin_dir"

    for name in "${names[@]}"; do
        command_path=$(command -v "$name")
        ln -s "$command_path" "$bin_dir/$name"
    done
}

test::generate_fixture_images() {
    local -r incoming_dir="$1"; shift

    mkdir -p "$incoming_dir"
    "$TEST_IMAGEMAGICK" -size 160x90 xc:red \
        "$incoming_dir/01-landscape.jpg"
    "$TEST_IMAGEMAGICK" -size 90x160 xc:blue \
        "$incoming_dir/02-portrait.jpg"
    "$TEST_IMAGEMAGICK" -size 120x120 xc:green \
        "$incoming_dir/03-square.jpg"
    "$TEST_IMAGEMAGICK" -size 100x80 xc:yellow \
        "$incoming_dir/04 filename with spaces.jpg"
    "$TEST_IMAGEMAGICK" -size 140x90 xc:purple \
        "$incoming_dir/05-extra.jpg"
    "$TEST_IMAGEMAGICK" -size 150x90 xc:orange \
        "$incoming_dir/06-extra.jpg"
}

test::write_album_config() {
    local -r config_file="$1"; shift
    local -r incoming_dir="$1"; shift
    local -r dist_dir="$1"; shift
    local -r title="$1"; shift
    local -r maxpreviews="$1"; shift

    {
        printf 'TITLE=%q\n' "$title"
        printf 'THUMBHEIGHT=30\n'
        printf 'HEIGHT=120\n'
        printf 'MAXPREVIEWS=%q\n' "$maxpreviews"
        printf 'IMAGE_JOBS=3\n'
        printf 'INCOMING_DIR=%q\n' "$incoming_dir"
        printf 'DIST_DIR=%q\n' "$dist_dir"
        printf 'TEMPLATE_DIR=%q/share/templates/default\n' "$TEST_REPO_ROOT"
        printf 'SPLASH_PAGE=yes\n'
        printf 'TARBALL_INCLUDE=no\n'
    } > "$config_file"
}