From 91b44348a3721ab0f793cbb5f6a770e76a70e4e2 Mon Sep 17 00:00:00 2001 From: vabatta <2137077+vabatta@users.noreply.github.com> Date: Wed, 21 Jan 2026 14:31:10 +0100 Subject: [PATCH 1/2] fix(theme): remove dead code and improve coverage - Remove dead code in run.sh (--detect/--list handling, theme_detect failure) main.sh handles these flags before calling theme_run - Add tests for THEME=light env fallback and invalid --detect override - Add kcov-exclude for directory-not-exists check (defensive code) --- dist/theme/bin/theme | 17 +++-------------- scripts/theme/discover.sh | 2 ++ scripts/theme/main_spec.sh | 15 +++++++++++++++ scripts/theme/run.sh | 21 ++++----------------- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/dist/theme/bin/theme b/dist/theme/bin/theme index c3cbf30..00a3fc2 100755 --- a/dist/theme/bin/theme +++ b/dist/theme/bin/theme @@ -237,9 +237,11 @@ theme_discover_handlers() { theme_source_handlers_dir() { local dir="$1" + # @start-kcov-exclude - tests always create directories; defensive only if [[ ! -d "$dir" ]]; then return 0 fi + # @end-kcov-exclude local f for f in "$dir"/*.sh; do @@ -322,17 +324,6 @@ theme_warn() { theme_run() { local arg="${1:-}" - if [[ "$arg" == "--detect" ]]; then - theme_detect - printf '%s\n' "$THEME_APPEARANCE" - return 0 - fi - - if [[ "$arg" == "--list" ]]; then - theme_list - return 0 - fi - theme_source_config if ! theme_discover_provider; then @@ -353,9 +344,7 @@ theme_run() { ;; esac - if ! theme_detect "$override"; then - return 1 - fi + theme_detect "$override" "$THEME_PROVIDER" "$THEME_APPEARANCE" "$THEME_SOURCE" diff --git a/scripts/theme/discover.sh b/scripts/theme/discover.sh index 358b8af..f3ca027 100644 --- a/scripts/theme/discover.sh +++ b/scripts/theme/discover.sh @@ -66,9 +66,11 @@ theme_source_handlers_dir() { local dir="$1" # Handle non-existent directory gracefully + # @start-kcov-exclude - tests always create directories; defensive only if [[ ! -d "$dir" ]]; then return 0 fi + # @end-kcov-exclude # Source all .sh files in the directory local f diff --git a/scripts/theme/main_spec.sh b/scripts/theme/main_spec.sh index 4df1a5c..4abafe2 100644 --- a/scripts/theme/main_spec.sh +++ b/scripts/theme/main_spec.sh @@ -98,6 +98,21 @@ Describe 'theme' The output should be present End + It 'falls back to THEME env var when set to light' + # Remove detector so it falls through to THEME env + rm -f "$XDG_CONFIG_HOME/theme/detectors.d/noop.sh" + export THEME=light + When run script "$BIN" -q --detect + The status should be success + The output should equal "light" + End + + It 'rejects invalid override with --detect' + When run script "$BIN" --detect invalid + The status should be failure + The stderr should include "invalid appearance" + End + It 'falls back to light when THEME env var is invalid' export THEME=invalid_value # Clear any system-level detection by running in isolated env diff --git a/scripts/theme/run.sh b/scripts/theme/run.sh index 61835b5..a57aea1 100644 --- a/scripts/theme/run.sh +++ b/scripts/theme/run.sh @@ -14,23 +14,11 @@ theme_warn() { } # Main orchestration function -# Usage: theme_run [--detect|--list|dark|light|auto] +# Usage: theme_run [dark|light|auto] +# Note: --detect and --list are handled by main.sh before calling this theme_run() { local arg="${1:-}" - # Handle --detect: only detect and print, no provider/handlers - if [[ "$arg" == "--detect" ]]; then - theme_detect - printf '%s\n' "$THEME_APPEARANCE" - return 0 - fi - - # Handle --list: list provider and handlers - if [[ "$arg" == "--list" ]]; then - theme_list - return 0 - fi - # Source user configuration (provider.sh, handlers.d/*.sh) theme_source_config @@ -55,9 +43,8 @@ theme_run() { ;; esac - if ! theme_detect "$override"; then - return 1 - fi + # Note: override is validated above, so theme_detect can't fail here + theme_detect "$override" # Call provider with appearance and source "$THEME_PROVIDER" "$THEME_APPEARANCE" "$THEME_SOURCE" From 1c5e24ba301d59495bc15916a4ce300aa3c66379 Mon Sep 17 00:00:00 2001 From: vabatta <2137077+vabatta@users.noreply.github.com> Date: Wed, 21 Jan 2026 14:34:41 +0100 Subject: [PATCH 2/2] test(theme): add test for non-existent handlers.d directory --- dist/theme/bin/theme | 2 -- scripts/theme/discover.sh | 2 -- scripts/theme/main_spec.sh | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/dist/theme/bin/theme b/dist/theme/bin/theme index 00a3fc2..12f977c 100755 --- a/dist/theme/bin/theme +++ b/dist/theme/bin/theme @@ -237,11 +237,9 @@ theme_discover_handlers() { theme_source_handlers_dir() { local dir="$1" - # @start-kcov-exclude - tests always create directories; defensive only if [[ ! -d "$dir" ]]; then return 0 fi - # @end-kcov-exclude local f for f in "$dir"/*.sh; do diff --git a/scripts/theme/discover.sh b/scripts/theme/discover.sh index f3ca027..358b8af 100644 --- a/scripts/theme/discover.sh +++ b/scripts/theme/discover.sh @@ -66,11 +66,9 @@ theme_source_handlers_dir() { local dir="$1" # Handle non-existent directory gracefully - # @start-kcov-exclude - tests always create directories; defensive only if [[ ! -d "$dir" ]]; then return 0 fi - # @end-kcov-exclude # Source all .sh files in the directory local f diff --git a/scripts/theme/main_spec.sh b/scripts/theme/main_spec.sh index 4abafe2..f135058 100644 --- a/scripts/theme/main_spec.sh +++ b/scripts/theme/main_spec.sh @@ -414,6 +414,20 @@ EOF The output should equal "PROVIDER_RAN" The stderr should include "no detectors configured" End + + It 'handles non-existent handlers.d directory gracefully' + cat > "$XDG_CONFIG_HOME/theme/provider.sh" << 'EOF' +theme_provider_test() { + echo "PROVIDER_RAN" +} +EOF + # Remove handlers.d directory entirely + rm -rf "$XDG_CONFIG_HOME/theme/handlers.d" + export THEME=dark + When run script "$BIN" -q + The status should be success + The output should equal "PROVIDER_RAN" + End End #═══════════════════════════════════════════════════════════════