Skip to content

Commit ce713d4

Browse files
Deduplicate test functions and add SETUP logging for install script
Co-authored-by: HectorCastelli <3715874+HectorCastelli@users.noreply.github.com>
1 parent d7b6563 commit ce713d4

File tree

1 file changed

+74
-52
lines changed

1 file changed

+74
-52
lines changed

scripts/tests.sh

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,70 +23,70 @@ build_image() {
2323
"$CONTAINER_RUNTIME" build -t "$IMAGE_NAME" -f "$REPO_DIR/Containerfile" "$REPO_DIR"
2424
}
2525

26-
# Run a single test case in its own container
27-
# Usage: run_test_case <description> <command>
26+
# Run a test case (optionally in an existing container)
27+
# Usage: run_test_case <description> <command> [container_id]
2828
run_test_case() {
2929
description="$1"
3030
shift
31-
command="$*"
32-
33-
# Start a container for this test
34-
container=$("$CONTAINER_RUNTIME" run -d \
35-
-v "$REPO_DIR:/dotfiles:Z" \
36-
"$IMAGE_NAME" \
37-
sleep infinity 2>&1)
38-
39-
# Run the command in the container and capture output and exit code
40-
if output=$("$CONTAINER_RUNTIME" exec "$container" sh -c "$command" 2>&1); then
41-
exit_code=0
42-
else
43-
exit_code=$?
44-
fi
45-
46-
# Clean up the container
47-
"$CONTAINER_RUNTIME" rm -f "$container" >/dev/null 2>&1 || true
48-
49-
# Write results to report
50-
{
51-
printf "\n[TEST] %s\n" "$description"
52-
printf "Command: %s\n" "$command"
53-
54-
if [ -n "$output" ]; then
55-
printf "Output:\n%s\n" "$output"
56-
fi
57-
58-
if [ $exit_code -eq 0 ]; then
59-
printf "[PASS] %s\n" "$description"
31+
32+
# Check if last argument looks like a container ID (starts with alphanumeric)
33+
# We need to parse all args to find if the last one is a container ID
34+
args=""
35+
container_id=""
36+
37+
# Collect all arguments
38+
while [ $# -gt 0 ]; do
39+
if [ $# -eq 1 ]; then
40+
# Last argument - could be container_id or part of command
41+
# Check if it looks like a container ID (40+ hex chars or short container name)
42+
case "$1" in
43+
*\ *)
44+
# Has space, definitely part of command
45+
args="$args $1"
46+
;;
47+
*)
48+
# No space - could be container ID or single-word command
49+
# If it's 12+ chars and alphanumeric, treat as container ID
50+
if [ ${#1} -ge 12 ]; then
51+
container_id="$1"
52+
else
53+
args="$args $1"
54+
fi
55+
;;
56+
esac
6057
else
61-
printf "[FAIL] %s (exit code: %d)\n" "$description" "$exit_code"
58+
args="$args $1"
6259
fi
63-
} >>"$REPORT_FILE"
64-
65-
# Also print to stdout
66-
if [ $exit_code -eq 0 ]; then
67-
printf "[PASS] %s\n" "$description"
60+
shift
61+
done
62+
63+
command="$args"
64+
65+
# If container_id is provided, use existing container; otherwise create new one
66+
if [ -n "$container_id" ]; then
67+
container="$container_id"
68+
cleanup_after=false
6869
else
69-
printf "[FAIL] %s (exit code: %d)\n" "$description" "$exit_code"
70+
# Start a container for this test
71+
container=$("$CONTAINER_RUNTIME" run -d \
72+
-v "$REPO_DIR:/dotfiles:Z" \
73+
"$IMAGE_NAME" \
74+
sleep infinity 2>&1)
75+
cleanup_after=true
7076
fi
7177

72-
return $exit_code
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
78+
# Run the command in the container and capture output and exit code
8479
if output=$("$CONTAINER_RUNTIME" exec "$container" sh -c "$command" 2>&1); then
8580
exit_code=0
8681
else
8782
exit_code=$?
8883
fi
8984

85+
# Clean up the container if we created it
86+
if [ "$cleanup_after" = true ]; then
87+
"$CONTAINER_RUNTIME" rm -f "$container" >/dev/null 2>&1 || true
88+
fi
89+
9090
# Write results to report
9191
{
9292
printf "\n[TEST] %s\n" "$description"
@@ -119,7 +119,7 @@ run_test_case_in_container() {
119119
# If PROFILE_TEST_CONTAINER is set, uses that container; otherwise creates a new one
120120
assert() {
121121
if [ -n "$PROFILE_TEST_CONTAINER" ]; then
122-
run_test_case_in_container "$PROFILE_TEST_CONTAINER" "$@" || true
122+
run_test_case "$@" "$PROFILE_TEST_CONTAINER" || true
123123
else
124124
run_test_case "$@" || true
125125
fi
@@ -201,7 +201,29 @@ run_profile_tests() {
201201
# Run the install script in the container first
202202
if [ -f "$profile_dir/install.sh" ]; then
203203
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
204+
205+
# Capture install output for the report
206+
if install_output=$("$CONTAINER_RUNTIME" exec "$PROFILE_TEST_CONTAINER" sh -c "cd /dotfiles/profiles/$profile_name && sh install.sh" 2>&1); then
207+
install_exit_code=0
208+
else
209+
install_exit_code=$?
210+
fi
211+
212+
# Log the setup step to the report
213+
{
214+
printf "\n[SETUP] Install script for profile-specific tests\n"
215+
printf "Command: cd /dotfiles/profiles/%s && sh install.sh\n" "$profile_name"
216+
217+
if [ -n "$install_output" ]; then
218+
printf "Output:\n%s\n" "$install_output"
219+
fi
220+
221+
if [ $install_exit_code -eq 0 ]; then
222+
printf "[PASS] Setup completed successfully\n"
223+
else
224+
printf "[FAIL] Setup failed (exit code: %d)\n" "$install_exit_code"
225+
fi
226+
} >>"$REPORT_FILE"
205227
fi
206228

207229
# Source the profile's test file and run tests (using the shared container)

0 commit comments

Comments
 (0)