diff --git a/src/main.sh b/src/main.sh index cb8a5908..40f3e134 100644 --- a/src/main.sh +++ b/src/main.sh @@ -543,14 +543,14 @@ function bashunit::main::watch_get_checksum() { if [ -d "$file" ]; then local found found=$(find "$file" -name '*.sh' -type f \ - -exec stat -f '%m %N' {} + 2>/dev/null || + -exec stat -c '%Y %n' {} + 2>/dev/null || find "$file" -name '*.sh' -type f \ - -exec stat -c '%Y %n' {} + 2>/dev/null) || true + -exec stat -f '%m %N' {} + 2>/dev/null) || true checksum="${checksum}${found}" elif [ -f "$file" ]; then local mtime - mtime=$(stat -f '%m' "$file" 2>/dev/null || - stat -c '%Y' "$file" 2>/dev/null) || true + mtime=$(stat -c '%Y' "$file" 2>/dev/null || + stat -f '%m' "$file" 2>/dev/null) || true checksum="${checksum}${mtime} ${file}" fi done diff --git a/tests/unit/watch_checksum_test.sh b/tests/unit/watch_checksum_test.sh new file mode 100644 index 00000000..64c094ab --- /dev/null +++ b/tests/unit/watch_checksum_test.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +# shellcheck disable=SC2329 # Test functions are invoked indirectly by bashunit + +############################ +# bashunit::main::watch_get_checksum +############################ + +function test_checksum_is_stable_for_unchanged_paths() { + local dir + dir=$(bashunit::temp_dir watch_stable) + : >"$dir/foo.sh" + + local first second + first=$(bashunit::main::watch_get_checksum "$dir") + second=$(bashunit::main::watch_get_checksum "$dir") + + assert_equals "$first" "$second" +} + +function test_checksum_changes_when_file_mtime_changes() { + local dir + dir=$(bashunit::temp_dir watch_change) + local file="$dir/foo.sh" + : >"$file" + touch -t 202001010000 "$file" + + local before after + before=$(bashunit::main::watch_get_checksum "$dir") + touch -t 202501010000 "$file" + after=$(bashunit::main::watch_get_checksum "$dir") + + assert_not_equals "$before" "$after" +} + +function test_checksum_differs_when_new_file_appears() { + local dir + dir=$(bashunit::temp_dir watch_new_file) + : >"$dir/a.sh" + + local before after + before=$(bashunit::main::watch_get_checksum "$dir") + : >"$dir/b.sh" + after=$(bashunit::main::watch_get_checksum "$dir") + + assert_not_equals "$before" "$after" +} + +function test_checksum_ignores_non_sh_files() { + local dir + dir=$(bashunit::temp_dir watch_non_sh) + : >"$dir/keep.sh" + + local before after + before=$(bashunit::main::watch_get_checksum "$dir") + : >"$dir/ignored.txt" + : >"$dir/ignored.md" + after=$(bashunit::main::watch_get_checksum "$dir") + + assert_equals "$before" "$after" +} + +function test_checksum_handles_single_file_path() { + local dir + dir=$(bashunit::temp_dir watch_file) + local file="$dir/single.sh" + : >"$file" + touch -t 202001010000 "$file" + + local before after + before=$(bashunit::main::watch_get_checksum "$file") + touch -t 202501010000 "$file" + after=$(bashunit::main::watch_get_checksum "$file") + + assert_not_equals "$before" "$after" +} + +function test_checksum_is_empty_for_missing_path() { + local missing="/nonexistent/path/$$/xyz" + + assert_empty "$(bashunit::main::watch_get_checksum "$missing")" +}