Skip to content

Commit d7b6563

Browse files
Use single container with install for profile-specific tests
Co-authored-by: HectorCastelli <3715874+HectorCastelli@users.noreply.github.com>
1 parent 64daad5 commit d7b6563

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

scripts/tests.sh

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fi
1515
IMAGE_NAME="dotfiles-test"
1616
REPORT_FILE="${REPORT_FILE:-test_report.txt}"
1717
REPO_DIR="$(git rev-parse --show-toplevel)"
18+
PROFILE_TEST_CONTAINER="" # Container for profile-specific tests
1819

1920
# Build the test image
2021
build_image() {
@@ -71,11 +72,57 @@ run_test_case() {
7172
return $exit_code
7273
}
7374

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+
74116
# Assert function: wrapper for run_test_case for compatibility
75117
# Usage: assert <description> <command>
76118
# 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
77120
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
79126
}
80127

81128
# Run common tests that apply to all profiles
@@ -138,15 +185,32 @@ run_profile_tests() {
138185
printf "========================================\n"
139186
} | tee -a "$REPORT_FILE"
140187

141-
# Run common tests for all profiles
188+
# Run common tests for all profiles (each in their own container)
142189
run_common_tests "$profile_name"
143190

144191
# Run profile-specific tests if they exist
145192
if [ -f "$profile_dir/tests.sh" ]; then
146193
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)
148208
# shellcheck disable=SC1090
149209
. "$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=""
150214
fi
151215
}
152216

0 commit comments

Comments
 (0)