|
15 | 15 | IMAGE_NAME="dotfiles-test" |
16 | 16 | REPORT_FILE="${REPORT_FILE:-test_report.txt}" |
17 | 17 | REPO_DIR="$(git rev-parse --show-toplevel)" |
| 18 | +PROFILE_TEST_CONTAINER="" # Container for profile-specific tests |
18 | 19 |
|
19 | 20 | # Build the test image |
20 | 21 | build_image() { |
@@ -71,11 +72,57 @@ run_test_case() { |
71 | 72 | return $exit_code |
72 | 73 | } |
73 | 74 |
|
| 75 | +# Run a test case in an existing container (for profile-specific tests) |
| 76 | +# Usage: run_test_case_in_container <container_id> <description> <command> |
| 77 | +run_test_case_in_container() { |
| 78 | + container="$1" |
| 79 | + description="$2" |
| 80 | + shift 2 |
| 81 | + command="$*" |
| 82 | + |
| 83 | + # Run the command in the existing container and capture output and exit code |
| 84 | + if output=$("$CONTAINER_RUNTIME" exec "$container" sh -c "$command" 2>&1); then |
| 85 | + exit_code=0 |
| 86 | + else |
| 87 | + exit_code=$? |
| 88 | + fi |
| 89 | + |
| 90 | + # Write results to report |
| 91 | + { |
| 92 | + printf "\n[TEST] %s\n" "$description" |
| 93 | + printf "Command: %s\n" "$command" |
| 94 | + |
| 95 | + if [ -n "$output" ]; then |
| 96 | + printf "Output:\n%s\n" "$output" |
| 97 | + fi |
| 98 | + |
| 99 | + if [ $exit_code -eq 0 ]; then |
| 100 | + printf "[PASS] %s\n" "$description" |
| 101 | + else |
| 102 | + printf "[FAIL] %s (exit code: %d)\n" "$description" "$exit_code" |
| 103 | + fi |
| 104 | + } >>"$REPORT_FILE" |
| 105 | + |
| 106 | + # Also print to stdout |
| 107 | + if [ $exit_code -eq 0 ]; then |
| 108 | + printf "[PASS] %s\n" "$description" |
| 109 | + else |
| 110 | + printf "[FAIL] %s (exit code: %d)\n" "$description" "$exit_code" |
| 111 | + fi |
| 112 | + |
| 113 | + return $exit_code |
| 114 | +} |
| 115 | + |
74 | 116 | # Assert function: wrapper for run_test_case for compatibility |
75 | 117 | # Usage: assert <description> <command> |
76 | 118 | # Always returns 0 to allow tests to continue even on failure |
| 119 | +# If PROFILE_TEST_CONTAINER is set, uses that container; otherwise creates a new one |
77 | 120 | assert() { |
78 | | - run_test_case "$@" || true |
| 121 | + if [ -n "$PROFILE_TEST_CONTAINER" ]; then |
| 122 | + run_test_case_in_container "$PROFILE_TEST_CONTAINER" "$@" || true |
| 123 | + else |
| 124 | + run_test_case "$@" || true |
| 125 | + fi |
79 | 126 | } |
80 | 127 |
|
81 | 128 | # Run common tests that apply to all profiles |
@@ -138,15 +185,32 @@ run_profile_tests() { |
138 | 185 | printf "========================================\n" |
139 | 186 | } | tee -a "$REPORT_FILE" |
140 | 187 |
|
141 | | - # Run common tests for all profiles |
| 188 | + # Run common tests for all profiles (each in their own container) |
142 | 189 | run_common_tests "$profile_name" |
143 | 190 |
|
144 | 191 | # Run profile-specific tests if they exist |
145 | 192 | if [ -f "$profile_dir/tests.sh" ]; then |
146 | 193 | printf "\n--- Profile-Specific Tests ---\n" | tee -a "$REPORT_FILE" |
147 | | - # Source the profile's test file and run tests |
| 194 | + |
| 195 | + # Start a container for profile-specific tests |
| 196 | + PROFILE_TEST_CONTAINER=$("$CONTAINER_RUNTIME" run -d \ |
| 197 | + -v "$REPO_DIR:/dotfiles:Z" \ |
| 198 | + "$IMAGE_NAME" \ |
| 199 | + sleep infinity 2>&1) |
| 200 | + |
| 201 | + # Run the install script in the container first |
| 202 | + if [ -f "$profile_dir/install.sh" ]; then |
| 203 | + printf "Running install script in profile test container...\n" |
| 204 | + "$CONTAINER_RUNTIME" exec "$PROFILE_TEST_CONTAINER" sh -c "cd /dotfiles/profiles/$profile_name && sh install.sh" >/dev/null 2>&1 || true |
| 205 | + fi |
| 206 | + |
| 207 | + # Source the profile's test file and run tests (using the shared container) |
148 | 208 | # shellcheck disable=SC1090 |
149 | 209 | . "$profile_dir/tests.sh" || true |
| 210 | + |
| 211 | + # Clean up the profile test container |
| 212 | + "$CONTAINER_RUNTIME" rm -f "$PROFILE_TEST_CONTAINER" >/dev/null 2>&1 || true |
| 213 | + PROFILE_TEST_CONTAINER="" |
150 | 214 | fi |
151 | 215 | } |
152 | 216 |
|
|
0 commit comments