Skip to content

Commit f73af6d

Browse files
authored
Merge pull request #626 from TypedDevs/docs/watch-test-and-sidebar-validation
test(watch): unit cover watch_get_checksum polling helper
2 parents bddf016 + 160acfa commit f73af6d

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/main.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,14 @@ function bashunit::main::watch_get_checksum() {
543543
if [ -d "$file" ]; then
544544
local found
545545
found=$(find "$file" -name '*.sh' -type f \
546-
-exec stat -f '%m %N' {} + 2>/dev/null ||
546+
-exec stat -c '%Y %n' {} + 2>/dev/null ||
547547
find "$file" -name '*.sh' -type f \
548-
-exec stat -c '%Y %n' {} + 2>/dev/null) || true
548+
-exec stat -f '%m %N' {} + 2>/dev/null) || true
549549
checksum="${checksum}${found}"
550550
elif [ -f "$file" ]; then
551551
local mtime
552-
mtime=$(stat -f '%m' "$file" 2>/dev/null ||
553-
stat -c '%Y' "$file" 2>/dev/null) || true
552+
mtime=$(stat -c '%Y' "$file" 2>/dev/null ||
553+
stat -f '%m' "$file" 2>/dev/null) || true
554554
checksum="${checksum}${mtime} ${file}"
555555
fi
556556
done

tests/unit/watch_checksum_test.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)