|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck disable=SC2329 # Test functions are invoked indirectly by bashunit |
| 4 | +############################ |
| 5 | +# bashunit::watch::is_available |
| 6 | +############################ |
| 7 | + |
| 8 | +function test_is_available_returns_inotifywait_when_present() { |
| 9 | + bashunit::mock bashunit::watch::_command_exists mock_true |
| 10 | + |
| 11 | + assert_equals "inotifywait" "$(bashunit::watch::is_available)" |
| 12 | +} |
| 13 | + |
| 14 | +function test_is_available_returns_fswatch_when_inotifywait_missing() { |
| 15 | + local call_count=0 |
| 16 | + function bashunit::watch::_command_exists() { |
| 17 | + call_count=$((call_count + 1)) |
| 18 | + [[ $call_count -eq 2 ]] |
| 19 | + } |
| 20 | + |
| 21 | + assert_equals "fswatch" "$(bashunit::watch::is_available)" |
| 22 | +} |
| 23 | + |
| 24 | +function test_is_available_returns_empty_when_no_tool_found() { |
| 25 | + bashunit::mock bashunit::watch::_command_exists mock_false |
| 26 | + |
| 27 | + assert_empty "$(bashunit::watch::is_available)" |
| 28 | +} |
| 29 | + |
| 30 | +############################ |
| 31 | +# bashunit::watch::run — error path (no tool) |
| 32 | +# run() calls exit 1, so we must capture it in a subshell |
| 33 | +############################ |
| 34 | + |
| 35 | +function test_run_exits_nonzero_when_no_tool_available() { |
| 36 | + bashunit::mock bashunit::watch::is_available echo "" |
| 37 | + |
| 38 | + local exit_code=0 |
| 39 | + (bashunit::watch::run "tests/" >/dev/null 2>&1) || exit_code=$? |
| 40 | + |
| 41 | + assert_greater_than "0" "$exit_code" |
| 42 | +} |
| 43 | + |
| 44 | +function test_run_error_message_mentions_required_tools() { |
| 45 | + bashunit::mock bashunit::watch::is_available echo "" |
| 46 | + |
| 47 | + local output |
| 48 | + output=$(bashunit::watch::run "tests/" 2>&1) || true |
| 49 | + |
| 50 | + assert_contains "inotifywait" "$output" |
| 51 | + assert_contains "fswatch" "$output" |
| 52 | +} |
| 53 | + |
| 54 | +function test_run_error_message_includes_install_hints() { |
| 55 | + bashunit::mock bashunit::watch::is_available echo "" |
| 56 | + |
| 57 | + local output |
| 58 | + output=$(bashunit::watch::run "tests/" 2>&1) || true |
| 59 | + |
| 60 | + assert_contains "apt install inotify-tools" "$output" |
| 61 | + assert_contains "brew install fswatch" "$output" |
| 62 | +} |
| 63 | + |
| 64 | +############################ |
| 65 | +# bashunit::watch::wait_for_change — tool dispatch |
| 66 | +############################ |
| 67 | + |
| 68 | +function test_wait_for_change_calls_inotifywait_on_linux() { |
| 69 | + bashunit::spy inotifywait |
| 70 | + |
| 71 | + bashunit::watch::wait_for_change "inotifywait" "tests/" 2>/dev/null || true |
| 72 | + |
| 73 | + assert_have_been_called inotifywait |
| 74 | +} |
| 75 | + |
| 76 | +function test_wait_for_change_calls_fswatch_on_macos() { |
| 77 | + bashunit::spy fswatch |
| 78 | + |
| 79 | + bashunit::watch::wait_for_change "fswatch" "tests/" 2>/dev/null || true |
| 80 | + |
| 81 | + assert_have_been_called fswatch |
| 82 | +} |
| 83 | + |
| 84 | +function test_wait_for_change_does_nothing_for_unknown_tool() { |
| 85 | + bashunit::spy inotifywait |
| 86 | + bashunit::spy fswatch |
| 87 | + |
| 88 | + bashunit::watch::wait_for_change "unknown-tool" "tests/" 2>/dev/null || true |
| 89 | + |
| 90 | + assert_not_called inotifywait |
| 91 | + assert_not_called fswatch |
| 92 | +} |
0 commit comments