Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/watch_checksum_test.sh
Original file line number Diff line number Diff line change
@@ -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")"
}
Loading