|
| 1 | +#!/usr/bin/env bash |
| 2 | +# tests/scripts/test-dso-config-path-e2e.sh |
| 3 | +# Integration tests: full read-config.sh resolution chain end-to-end. |
| 4 | +# |
| 5 | +# Exercises the complete resolution chain: |
| 6 | +# read-config.sh → .claude/dso-config.conf |
| 7 | +# config-paths.sh → read-config.sh → .claude/dso-config.conf |
| 8 | +# shim (--lib mode) → .claude/dso-config.conf → DSO_ROOT |
| 9 | +# validate.sh + CONFIG_FILE env → .claude/dso-config.conf |
| 10 | +# |
| 11 | +# Each scenario uses an isolated temp git repo. |
| 12 | +# |
| 13 | +# Usage: bash tests/scripts/test-dso-config-path-e2e.sh |
| 14 | +# Returns: exit 0 if all tests pass, exit 1 if any fail |
| 15 | + |
| 16 | +set -uo pipefail |
| 17 | + |
| 18 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 19 | +PLUGIN_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 20 | +DSO_PLUGIN_DIR="$PLUGIN_ROOT/plugins/dso" |
| 21 | +READ_CONFIG="$DSO_PLUGIN_DIR/scripts/read-config.sh" |
| 22 | +CONFIG_PATHS="$DSO_PLUGIN_DIR/hooks/lib/config-paths.sh" |
| 23 | +SHIM="$PLUGIN_ROOT/.claude/scripts/dso" |
| 24 | + |
| 25 | +source "$PLUGIN_ROOT/tests/lib/assert.sh" |
| 26 | + |
| 27 | +echo "=== test-dso-config-path-e2e.sh ===" |
| 28 | + |
| 29 | +# Cleanup tracker for temp dirs |
| 30 | +_e2e_tmpdirs=() |
| 31 | +_e2e_cleanup() { |
| 32 | + for d in "${_e2e_tmpdirs[@]+"${_e2e_tmpdirs[@]}"}"; do |
| 33 | + rm -rf "$d" |
| 34 | + done |
| 35 | +} |
| 36 | +trap '_e2e_cleanup' EXIT |
| 37 | + |
| 38 | +# Helper: create an isolated temp git repo |
| 39 | +_make_temp_repo() { |
| 40 | + local tmpdir |
| 41 | + tmpdir="$(mktemp -d)" |
| 42 | + _e2e_tmpdirs+=("$tmpdir") |
| 43 | + git -C "$tmpdir" init -q |
| 44 | + echo "$tmpdir" |
| 45 | +} |
| 46 | + |
| 47 | +# ── test_e2e_resolution_from_dot_claude_dso_config ─────────────────────────── |
| 48 | +# Given a minimal temp git repo with .claude/dso-config.conf, calling |
| 49 | +# read-config.sh (no explicit config arg) returns correct values. |
| 50 | +_snapshot_fail |
| 51 | +_repo="$(_make_temp_repo)" |
| 52 | +mkdir -p "$_repo/.claude" |
| 53 | +cat > "$_repo/.claude/dso-config.conf" <<'CONF' |
| 54 | +test_command=make test-e2e |
| 55 | +paths.app_dir=myapp |
| 56 | +CONF |
| 57 | + |
| 58 | +# Unset isolation env vars so auto-discovery runs |
| 59 | +_rdcd_exit=0 |
| 60 | +_rdcd_output="" |
| 61 | +_rdcd_output=$( |
| 62 | + cd "$_repo" && |
| 63 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 64 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 65 | + bash "$READ_CONFIG" "test_command" 2>&1 |
| 66 | +) || _rdcd_exit=$? |
| 67 | + |
| 68 | +assert_eq "test_e2e_resolution_from_dot_claude_dso_config: exit 0" "0" "$_rdcd_exit" |
| 69 | +assert_eq "test_e2e_resolution_from_dot_claude_dso_config: reads test_command" "make test-e2e" "$_rdcd_output" |
| 70 | + |
| 71 | +# Also verify a dot-notation key resolves correctly |
| 72 | +_rdcd2_exit=0 |
| 73 | +_rdcd2_output="" |
| 74 | +_rdcd2_output=$( |
| 75 | + cd "$_repo" && |
| 76 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 77 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 78 | + bash "$READ_CONFIG" "paths.app_dir" 2>&1 |
| 79 | +) || _rdcd2_exit=$? |
| 80 | + |
| 81 | +assert_eq "test_e2e_resolution_from_dot_claude_dso_config: reads paths.app_dir exit 0" "0" "$_rdcd2_exit" |
| 82 | +assert_eq "test_e2e_resolution_from_dot_claude_dso_config: reads paths.app_dir value" "myapp" "$_rdcd2_output" |
| 83 | + |
| 84 | +assert_pass_if_clean "test_e2e_resolution_from_dot_claude_dso_config" |
| 85 | + |
| 86 | +# ── test_e2e_graceful_degradation_no_config ─────────────────────────────────── |
| 87 | +# Given a temp git repo with NO config file at either path, read-config.sh |
| 88 | +# returns empty output and exits 0 (graceful degradation). |
| 89 | +_snapshot_fail |
| 90 | +_repo_empty="$(_make_temp_repo)" |
| 91 | + |
| 92 | +_noconf_exit=0 |
| 93 | +_noconf_output="" |
| 94 | +_noconf_output=$( |
| 95 | + cd "$_repo_empty" && |
| 96 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 97 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 98 | + bash "$READ_CONFIG" "test_command" 2>&1 |
| 99 | +) || _noconf_exit=$? |
| 100 | + |
| 101 | +assert_eq "test_e2e_graceful_degradation_no_config: exit 0" "0" "$_noconf_exit" |
| 102 | +assert_eq "test_e2e_graceful_degradation_no_config: empty output" "" "$_noconf_output" |
| 103 | + |
| 104 | +assert_pass_if_clean "test_e2e_graceful_degradation_no_config" |
| 105 | + |
| 106 | +# ── test_e2e_config_paths_reads_from_dot_claude ────────────────────────────── |
| 107 | +# Given a temp git repo with .claude/dso-config.conf containing |
| 108 | +# paths.app_dir=myapp, sourcing config-paths.sh produces CFG_APP_DIR=myapp. |
| 109 | +_snapshot_fail |
| 110 | +_repo_cp="$(_make_temp_repo)" |
| 111 | +mkdir -p "$_repo_cp/.claude" |
| 112 | +cat > "$_repo_cp/.claude/dso-config.conf" <<'CONF' |
| 113 | +paths.app_dir=myapp |
| 114 | +paths.src_dir=src |
| 115 | +paths.test_dir=tests |
| 116 | +CONF |
| 117 | + |
| 118 | +_cfg_output="" |
| 119 | +_cfg_exit=0 |
| 120 | +_cfg_output=$( |
| 121 | + cd "$_repo_cp" && |
| 122 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 123 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 124 | + unset _CONFIG_PATHS_LOADED 2>/dev/null || true |
| 125 | + bash -c " |
| 126 | + source '$CONFIG_PATHS' |
| 127 | + echo \"\$CFG_APP_DIR\" |
| 128 | + " 2>&1 |
| 129 | +) || _cfg_exit=$? |
| 130 | + |
| 131 | +assert_eq "test_e2e_config_paths_reads_from_dot_claude: exit 0" "0" "$_cfg_exit" |
| 132 | +assert_eq "test_e2e_config_paths_reads_from_dot_claude: CFG_APP_DIR=myapp" "myapp" "$_cfg_output" |
| 133 | + |
| 134 | +# Also verify paths.src_dir and paths.test_dir are read correctly |
| 135 | +_cfg_src_output="" |
| 136 | +_cfg_src_exit=0 |
| 137 | +_cfg_src_output=$( |
| 138 | + cd "$_repo_cp" && |
| 139 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 140 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 141 | + unset _CONFIG_PATHS_LOADED 2>/dev/null || true |
| 142 | + bash -c " |
| 143 | + source '$CONFIG_PATHS' |
| 144 | + echo \"\$CFG_SRC_DIR\" |
| 145 | + " 2>&1 |
| 146 | +) || _cfg_src_exit=$? |
| 147 | + |
| 148 | +assert_eq "test_e2e_config_paths_reads_from_dot_claude: CFG_SRC_DIR=src exit 0" "0" "$_cfg_src_exit" |
| 149 | +assert_eq "test_e2e_config_paths_reads_from_dot_claude: CFG_SRC_DIR=src" "src" "$_cfg_src_output" |
| 150 | + |
| 151 | +assert_pass_if_clean "test_e2e_config_paths_reads_from_dot_claude" |
| 152 | + |
| 153 | +# ── test_e2e_shim_resolves_plugin_root ──────────────────────────────────────── |
| 154 | +# Given a temp git repo with .claude/dso-config.conf containing |
| 155 | +# dso.plugin_root=/some/path, running the shim (via source --lib) sets |
| 156 | +# DSO_ROOT=/some/path. |
| 157 | +_snapshot_fail |
| 158 | +_repo_shim="$(_make_temp_repo)" |
| 159 | +mkdir -p "$_repo_shim/.claude" |
| 160 | +# Use the actual plugin dir as a valid path for testing |
| 161 | +_fake_plugin_root="$DSO_PLUGIN_DIR" |
| 162 | +cat > "$_repo_shim/.claude/dso-config.conf" <<CONF |
| 163 | +dso.plugin_root=${_fake_plugin_root} |
| 164 | +CONF |
| 165 | + |
| 166 | +_shim_output="" |
| 167 | +_shim_exit=0 |
| 168 | +_shim_output=$( |
| 169 | + cd "$_repo_shim" && |
| 170 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 171 | + bash -c " |
| 172 | + source '$SHIM' --lib |
| 173 | + echo \"\$DSO_ROOT\" |
| 174 | + " 2>&1 |
| 175 | +) || _shim_exit=$? |
| 176 | + |
| 177 | +assert_eq "test_e2e_shim_resolves_plugin_root: exit 0" "0" "$_shim_exit" |
| 178 | +assert_eq "test_e2e_shim_resolves_plugin_root: DSO_ROOT set from .claude/dso-config.conf" "$_fake_plugin_root" "$_shim_output" |
| 179 | + |
| 180 | +assert_pass_if_clean "test_e2e_shim_resolves_plugin_root" |
| 181 | + |
| 182 | +# ── test_e2e_shim_no_config_exits_nonzero ──────────────────────────────────── |
| 183 | +# When no config file exists and CLAUDE_PLUGIN_ROOT is unset, the shim |
| 184 | +# exits non-zero with a helpful error message. |
| 185 | +_snapshot_fail |
| 186 | +_repo_shim_fail="$(_make_temp_repo)" |
| 187 | + |
| 188 | +_shim_fail_output="" |
| 189 | +_shim_fail_exit=0 |
| 190 | +_shim_fail_output=$( |
| 191 | + cd "$_repo_shim_fail" && |
| 192 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 193 | + bash "$SHIM" some-command 2>&1 |
| 194 | +) || _shim_fail_exit=$? |
| 195 | + |
| 196 | +if [[ "$_shim_fail_exit" -ne 0 ]]; then |
| 197 | + _actual_shim_fail_exit="nonzero" |
| 198 | +else |
| 199 | + _actual_shim_fail_exit="zero" |
| 200 | +fi |
| 201 | +assert_eq "test_e2e_shim_no_config_exits_nonzero: exits nonzero" "nonzero" "$_actual_shim_fail_exit" |
| 202 | +assert_contains "test_e2e_shim_no_config_exits_nonzero: error mentions dso-config.conf" "dso-config.conf" "$_shim_fail_output" |
| 203 | + |
| 204 | +assert_pass_if_clean "test_e2e_shim_no_config_exits_nonzero" |
| 205 | + |
| 206 | +# ── test_e2e_validate_sh_reads_commands ────────────────────────────────────── |
| 207 | +# Given a temp git repo with .claude/dso-config.conf containing |
| 208 | +# commands.test=echo test, validate.sh reads that value correctly |
| 209 | +# (integration with CONFIG_FILE env var for test isolation). |
| 210 | +_snapshot_fail |
| 211 | +_repo_val="$(_make_temp_repo)" |
| 212 | +mkdir -p "$_repo_val/.claude" |
| 213 | +cat > "$_repo_val/.claude/dso-config.conf" <<'CONF' |
| 214 | +commands.test=echo run-tests |
| 215 | +commands.lint=echo run-lint |
| 216 | +CONF |
| 217 | + |
| 218 | +_val_output="" |
| 219 | +_val_exit=0 |
| 220 | +_val_output=$( |
| 221 | + cd "$_repo_val" && |
| 222 | + unset WORKFLOW_CONFIG_FILE 2>/dev/null || true |
| 223 | + unset CLAUDE_PLUGIN_ROOT 2>/dev/null || true |
| 224 | + CONFIG_FILE="$_repo_val/.claude/dso-config.conf" \ |
| 225 | + bash "$READ_CONFIG" "commands.test" "$_repo_val/.claude/dso-config.conf" 2>&1 |
| 226 | +) || _val_exit=$? |
| 227 | + |
| 228 | +assert_eq "test_e2e_validate_sh_reads_commands: exit 0" "0" "$_val_exit" |
| 229 | +assert_eq "test_e2e_validate_sh_reads_commands: reads commands.test" "echo run-tests" "$_val_output" |
| 230 | + |
| 231 | +# Also verify commands.lint is readable |
| 232 | +_val_lint_output="" |
| 233 | +_val_lint_exit=0 |
| 234 | +_val_lint_output=$( |
| 235 | + bash "$READ_CONFIG" "commands.lint" "$_repo_val/.claude/dso-config.conf" 2>&1 |
| 236 | +) || _val_lint_exit=$? |
| 237 | + |
| 238 | +assert_eq "test_e2e_validate_sh_reads_commands: reads commands.lint exit 0" "0" "$_val_lint_exit" |
| 239 | +assert_eq "test_e2e_validate_sh_reads_commands: reads commands.lint value" "echo run-lint" "$_val_lint_output" |
| 240 | + |
| 241 | +assert_pass_if_clean "test_e2e_validate_sh_reads_commands" |
| 242 | + |
| 243 | +# ── test_e2e_workflow_config_file_env_overrides ─────────────────────────────── |
| 244 | +# WORKFLOW_CONFIG_FILE env var overrides .claude/dso-config.conf resolution |
| 245 | +# (backward compat for test isolation across the whole chain). |
| 246 | +_snapshot_fail |
| 247 | +_repo_env="$(_make_temp_repo)" |
| 248 | +mkdir -p "$_repo_env/.claude" |
| 249 | +cat > "$_repo_env/.claude/dso-config.conf" <<'CONF' |
| 250 | +paths.app_dir=from-dso-config |
| 251 | +CONF |
| 252 | + |
| 253 | +_env_override_file="$(mktemp)" |
| 254 | +_e2e_tmpdirs+=("$_env_override_file") |
| 255 | +cat > "$_env_override_file" <<'CONF' |
| 256 | +paths.app_dir=from-env-override |
| 257 | +CONF |
| 258 | + |
| 259 | +_env_output="" |
| 260 | +_env_exit=0 |
| 261 | +_env_output=$( |
| 262 | + cd "$_repo_env" && |
| 263 | + WORKFLOW_CONFIG_FILE="$_env_override_file" \ |
| 264 | + bash "$READ_CONFIG" "paths.app_dir" 2>&1 |
| 265 | +) || _env_exit=$? |
| 266 | + |
| 267 | +assert_eq "test_e2e_workflow_config_file_env_overrides: exit 0" "0" "$_env_exit" |
| 268 | +assert_eq "test_e2e_workflow_config_file_env_overrides: env var wins over .claude/dso-config.conf" "from-env-override" "$_env_output" |
| 269 | + |
| 270 | +assert_pass_if_clean "test_e2e_workflow_config_file_env_overrides" |
| 271 | + |
| 272 | +# ── Summary ────────────────────────────────────────────────────────────────── |
| 273 | +print_summary |
0 commit comments