|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck disable=SC2329 # Test functions are invoked indirectly by bashunit |
| 4 | + |
| 5 | +############################ |
| 6 | +# bashunit::main::watch_get_checksum |
| 7 | +############################ |
| 8 | + |
| 9 | +function test_checksum_is_stable_for_unchanged_paths() { |
| 10 | + local dir |
| 11 | + dir=$(bashunit::temp_dir watch_stable) |
| 12 | + : >"$dir/foo.sh" |
| 13 | + |
| 14 | + local first second |
| 15 | + first=$(bashunit::main::watch_get_checksum "$dir") |
| 16 | + second=$(bashunit::main::watch_get_checksum "$dir") |
| 17 | + |
| 18 | + assert_equals "$first" "$second" |
| 19 | +} |
| 20 | + |
| 21 | +function test_checksum_changes_when_file_mtime_changes() { |
| 22 | + local dir |
| 23 | + dir=$(bashunit::temp_dir watch_change) |
| 24 | + local file="$dir/foo.sh" |
| 25 | + : >"$file" |
| 26 | + touch -t 202001010000 "$file" |
| 27 | + |
| 28 | + local before after |
| 29 | + before=$(bashunit::main::watch_get_checksum "$dir") |
| 30 | + touch -t 202501010000 "$file" |
| 31 | + after=$(bashunit::main::watch_get_checksum "$dir") |
| 32 | + |
| 33 | + assert_not_equals "$before" "$after" |
| 34 | +} |
| 35 | + |
| 36 | +function test_checksum_differs_when_new_file_appears() { |
| 37 | + local dir |
| 38 | + dir=$(bashunit::temp_dir watch_new_file) |
| 39 | + : >"$dir/a.sh" |
| 40 | + |
| 41 | + local before after |
| 42 | + before=$(bashunit::main::watch_get_checksum "$dir") |
| 43 | + : >"$dir/b.sh" |
| 44 | + after=$(bashunit::main::watch_get_checksum "$dir") |
| 45 | + |
| 46 | + assert_not_equals "$before" "$after" |
| 47 | +} |
| 48 | + |
| 49 | +function test_checksum_ignores_non_sh_files() { |
| 50 | + local dir |
| 51 | + dir=$(bashunit::temp_dir watch_non_sh) |
| 52 | + : >"$dir/keep.sh" |
| 53 | + |
| 54 | + local before after |
| 55 | + before=$(bashunit::main::watch_get_checksum "$dir") |
| 56 | + : >"$dir/ignored.txt" |
| 57 | + : >"$dir/ignored.md" |
| 58 | + after=$(bashunit::main::watch_get_checksum "$dir") |
| 59 | + |
| 60 | + assert_equals "$before" "$after" |
| 61 | +} |
| 62 | + |
| 63 | +function test_checksum_handles_single_file_path() { |
| 64 | + local dir |
| 65 | + dir=$(bashunit::temp_dir watch_file) |
| 66 | + local file="$dir/single.sh" |
| 67 | + : >"$file" |
| 68 | + touch -t 202001010000 "$file" |
| 69 | + |
| 70 | + local before after |
| 71 | + before=$(bashunit::main::watch_get_checksum "$file") |
| 72 | + touch -t 202501010000 "$file" |
| 73 | + after=$(bashunit::main::watch_get_checksum "$file") |
| 74 | + |
| 75 | + assert_not_equals "$before" "$after" |
| 76 | +} |
| 77 | + |
| 78 | +function test_checksum_is_empty_for_missing_path() { |
| 79 | + local missing="/nonexistent/path/$$/xyz" |
| 80 | + |
| 81 | + assert_empty "$(bashunit::main::watch_get_checksum "$missing")" |
| 82 | +} |
0 commit comments