Skip to content

Commit 6ceb980

Browse files
committed
Refactor test suite to simplify test tracking and reporting
- Remove global test counters and result tracking from individual test scripts - Use local test number and failure tracking variables - Eliminate redundant test results file updates - Simplify test exit strategy by returning failures as exit code - Remove unnecessary test utility functions like `plan()` and `update_test_results()` This refactoring improves test script modularity and reduces global state management in the test suite.
1 parent c14a0e5 commit 6ceb980

File tree

5 files changed

+60
-174
lines changed

5 files changed

+60
-174
lines changed

test/test_runner.sh

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
77
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
88

9-
# Source test utilities
10-
source "$TEST_DIR/test_utils.sh"
11-
129
# Initialize counters
1310
TESTS_PASSED=0
1411
TESTS_FAILED=0
@@ -22,14 +19,7 @@ test_files=("$TEST_DIR"/unit/test_*.sh)
2219
total_test_files=${#test_files[@]}
2320
echo "1..$total_test_files"
2421

25-
# Create or clear the test results file
26-
echo "PASSED:0" > "$TEST_DIR/test_results.txt"
27-
echo "FAILED:0" >> "$TEST_DIR/test_results.txt"
28-
29-
# Make test utilities executable
30-
chmod +x "$TEST_DIR/test_utils.sh"
31-
32-
# Make test scripts executable
22+
# Make all test scripts executable
3323
for test_file in "${test_files[@]}"; do
3424
chmod +x "$test_file"
3525
done

test/test_utils.sh

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,18 @@
22

33
# test_utils.sh - Utilities for testing the context script (TAP-compliant)
44

5-
# Get the directory where the test_utils.sh script is located
6-
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7-
8-
# Initialize test counters
9-
TESTS_PASSED=0
10-
TESTS_FAILED=0
11-
TEST_NUMBER_INTERNAL=0
12-
13-
# Function to update test result counts
14-
update_test_results() {
15-
echo "PASSED:$TESTS_PASSED" > "$TEST_DIR/test_results.txt"
16-
echo "FAILED:$TESTS_FAILED" >> "$TEST_DIR/test_results.txt"
17-
}
18-
195
# Function to run a test and output TAP-compliant results
206
# Usage: run_test "test_description" "command_to_run"
217
run_test() {
228
local description="$1"
239
local command="$2"
2410
local skip_reason="${3:-}"
2511

26-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
12+
local test_number=$((test_number + 1))
2713

2814
# Skip test if reason provided
2915
if [ -n "$skip_reason" ]; then
30-
echo "ok $TEST_NUMBER_INTERNAL # SKIP $description - $skip_reason"
16+
echo "ok $test_number # SKIP $description - $skip_reason"
3117
return 0
3218
fi
3319

@@ -44,10 +30,9 @@ run_test() {
4430

4531
# Handle the result
4632
if [ $status -eq 0 ]; then
47-
echo "ok $TEST_NUMBER_INTERNAL - $description"
48-
TESTS_PASSED=$((TESTS_PASSED + 1))
33+
echo "ok $test_number - $description"
4934
else
50-
echo "not ok $TEST_NUMBER_INTERNAL - $description"
35+
echo "not ok $test_number - $description"
5136
echo "# Command: $command"
5237

5338
if [ -n "$output" ]; then
@@ -60,12 +45,12 @@ run_test() {
6045
echo "$error" | sed 's/^/# /'
6146
fi
6247

63-
TESTS_FAILED=$((TESTS_FAILED + 1))
48+
# Save the failure status for the exit code
49+
failures=$((failures + 1))
6450
fi
6551

6652
# Clean up temporary files
6753
rm -f "$output_file" "$error_file"
68-
update_test_results
6954

7055
return $status
7156
}
@@ -98,18 +83,15 @@ assert() {
9883
local condition="$1"
9984
local description="$2"
10085

101-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
86+
local test_number=$((test_number + 1))
10287

10388
if eval "$condition"; then
104-
echo "ok $TEST_NUMBER_INTERNAL - $description"
105-
TESTS_PASSED=$((TESTS_PASSED + 1))
89+
echo "ok $test_number - $description"
10690
else
107-
echo "not ok $TEST_NUMBER_INTERNAL - $description"
91+
echo "not ok $test_number - $description"
10892
echo "# Failed condition: $condition"
109-
TESTS_FAILED=$((TESTS_FAILED + 1))
93+
failures=$((failures + 1))
11094
fi
111-
112-
update_test_results
11395
}
11496

11597
# Function to create a test file with a specific size
@@ -137,19 +119,3 @@ create_test_file() {
137119
echo "# Warning: Created file size ($final_size) doesn't match requested size ($size_bytes)" >&2
138120
fi
139121
}
140-
141-
# Print TAP plan for individual test files
142-
plan() {
143-
local count="$1"
144-
echo "1..$count"
145-
}
146-
147-
# Export functions and variables for use in subshells
148-
export -f run_test
149-
export -f create_test_dir
150-
export -f cleanup_test_dir
151-
export -f tap_diag
152-
export -f assert
153-
export -f create_test_file
154-
export -f plan
155-
export -f update_test_results

test/unit/test_argument_parsing.sh

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
1010
# Source test utilities
1111
source "$TEST_DIR/test_utils.sh"
1212

13-
# Initialize test counters for this file
14-
TESTS_PASSED=0
15-
TESTS_FAILED=0
16-
TEST_NUMBER_INTERNAL=0
13+
# Initialize variables
14+
test_number=0
15+
failures=0
1716

1817
# Print TAP plan
19-
echo "1..4" # Reduced from 5 to 4 tests
18+
echo "1..4"
2019

2120
# Create a temporary directory for this test
2221
test_dir=$(create_test_dir)
@@ -38,64 +37,48 @@ EOF
3837
chmod +x "$PROJECT_ROOT/context"
3938

4039
# Test 1: Help option
41-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
4240
help_output=$("$PROJECT_ROOT/context" --help 2>&1)
4341
if echo "$help_output" | grep -q "Usage:"; then
44-
echo "ok $TEST_NUMBER_INTERNAL - help option displays usage"
45-
TESTS_PASSED=$((TESTS_PASSED + 1))
42+
echo "ok $((test_number+=1)) - help option displays usage"
4643
else
47-
echo "not ok $TEST_NUMBER_INTERNAL - help option displays usage"
44+
echo "not ok $((test_number+=1)) - help option displays usage"
4845
echo "# Help output did not contain 'Usage:'"
49-
TESTS_FAILED=$((TESTS_FAILED + 1))
46+
failures=$((failures + 1))
5047
fi
5148

5249
# Test 2: Summary option
53-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
5450
summary_output=$(cd "$test_dir" && "$PROJECT_ROOT/context" test.js --summary 2>&1)
5551
if echo "$summary_output" | grep -q "Summary:"; then
56-
echo "ok $TEST_NUMBER_INTERNAL - summary option displays summary"
57-
TESTS_PASSED=$((TESTS_PASSED + 1))
52+
echo "ok $((test_number+=1)) - summary option displays summary"
5853
else
59-
echo "not ok $TEST_NUMBER_INTERNAL - summary option displays summary"
54+
echo "not ok $((test_number+=1)) - summary option displays summary"
6055
echo "# Summary output did not contain 'Summary:'"
6156
echo "# Output: $summary_output"
62-
TESTS_FAILED=$((TESTS_FAILED + 1))
57+
failures=$((failures + 1))
6358
fi
6459

6560
# Test 3: No ls-files option
66-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
6761
nolsfiles_output=$(cd "$test_dir" && "$PROJECT_ROOT/context" test.js --no-ls-files 2>&1)
6862
if ! echo "$nolsfiles_output" | grep -q "Repository Files"; then
69-
echo "ok $TEST_NUMBER_INTERNAL - no-ls-files option hides repository files"
70-
TESTS_PASSED=$((TESTS_PASSED + 1))
63+
echo "ok $((test_number+=1)) - no-ls-files option hides repository files"
7164
else
72-
echo "not ok $TEST_NUMBER_INTERNAL - no-ls-files option hides repository files"
65+
echo "not ok $((test_number+=1)) - no-ls-files option hides repository files"
7366
echo "# Output still contained 'Repository Files' despite --no-ls-files"
74-
TESTS_FAILED=$((TESTS_FAILED + 1))
67+
failures=$((failures + 1))
7568
fi
7669

77-
# Test 4: Invalid option (previously Test 5)
78-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
70+
# Test 4: Invalid option
7971
if ! "$PROJECT_ROOT/context" --invalid-option >/dev/null 2>&1; then
80-
echo "ok $TEST_NUMBER_INTERNAL - invalid option causes error"
81-
TESTS_PASSED=$((TESTS_PASSED + 1))
72+
echo "ok $((test_number+=1)) - invalid option causes error"
8273
else
83-
echo "not ok $TEST_NUMBER_INTERNAL - invalid option causes error"
74+
echo "not ok $((test_number+=1)) - invalid option causes error"
8475
echo "# Command with invalid option did not fail as expected"
85-
TESTS_FAILED=$((TESTS_FAILED + 1))
76+
failures=$((failures + 1))
8677
fi
8778

8879
# Clean up
8980
echo "# Tests completed, cleaning up"
9081
cleanup_test_dir "$test_dir"
9182

92-
# Update test results file
93-
echo "PASSED:$TESTS_PASSED" > "$TEST_DIR/test_results.txt"
94-
echo "FAILED:$TESTS_FAILED" >> "$TEST_DIR/test_results.txt"
95-
9683
# Exit with success if all tests passed
97-
if [ $TESTS_FAILED -eq 0 ]; then
98-
exit 0
99-
else
100-
exit 1
101-
fi
84+
exit $failures

test/unit/test_file_processing.sh

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
1010
# Source test utilities
1111
source "$TEST_DIR/test_utils.sh"
1212

13-
# Initialize test counters for this file
14-
TESTS_PASSED=0
15-
TESTS_FAILED=0
16-
TEST_NUMBER_INTERNAL=0
13+
# Initialize variables
14+
test_number=0
15+
failures=0
1716

1817
# Print TAP plan
1918
echo "1..4"
@@ -33,62 +32,46 @@ echo "// Should be excluded - node_modules content" > "$test_dir/node_modules/ex
3332
chmod +x "$PROJECT_ROOT/context"
3433

3534
# Test 1: Basic file inclusion
36-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
3735
if cd "$test_dir" && "$PROJECT_ROOT/context" src/file1.js | grep -q "Test file 1"; then
38-
echo "ok $TEST_NUMBER_INTERNAL - basic file inclusion"
39-
TESTS_PASSED=$((TESTS_PASSED + 1))
36+
echo "ok $((test_number+=1)) - basic file inclusion"
4037
else
41-
echo "not ok $TEST_NUMBER_INTERNAL - basic file inclusion"
38+
echo "not ok $((test_number+=1)) - basic file inclusion"
4239
echo "# Command failed: cd $test_dir && $PROJECT_ROOT/context src/file1.js"
43-
TESTS_FAILED=$((TESTS_FAILED + 1))
40+
failures=$((failures + 1))
4441
fi
4542

4643
# Test 2: File pattern inclusion - simplified
47-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
4844
if cd "$test_dir" && "$PROJECT_ROOT/context" src/file2.js | grep -q "Test file 2"; then
49-
echo "ok $TEST_NUMBER_INTERNAL - file pattern inclusion"
50-
TESTS_PASSED=$((TESTS_PASSED + 1))
45+
echo "ok $((test_number+=1)) - file pattern inclusion"
5146
else
52-
echo "not ok $TEST_NUMBER_INTERNAL - file pattern inclusion"
47+
echo "not ok $((test_number+=1)) - file pattern inclusion"
5348
echo "# Command failed: cd $test_dir && $PROJECT_ROOT/context src/file2.js"
54-
TESTS_FAILED=$((TESTS_FAILED + 1))
49+
failures=$((failures + 1))
5550
fi
5651

5752
# Test 3: Exclude pattern
58-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
5953
output=$(cd "$test_dir" && "$PROJECT_ROOT/context" src/file1.js --exclude="node_modules/" 2>&1)
6054
if ! echo "$output" | grep -q "node_modules content"; then
61-
echo "ok $TEST_NUMBER_INTERNAL - exclude pattern"
62-
TESTS_PASSED=$((TESTS_PASSED + 1))
55+
echo "ok $((test_number+=1)) - exclude pattern"
6356
else
64-
echo "not ok $TEST_NUMBER_INTERNAL - exclude pattern"
57+
echo "not ok $((test_number+=1)) - exclude pattern"
6558
echo "# Output contained excluded content"
66-
TESTS_FAILED=$((TESTS_FAILED + 1))
59+
failures=$((failures + 1))
6760
fi
6861

6962
# Test 4: Multiple file arguments
70-
TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1))
7163
output=$(cd "$test_dir" && "$PROJECT_ROOT/context" src/file1.js src/file2.js 2>&1)
7264
if echo "$output" | grep -q "Test file 1" && echo "$output" | grep -q "Test file 2"; then
73-
echo "ok $TEST_NUMBER_INTERNAL - multiple file arguments"
74-
TESTS_PASSED=$((TESTS_PASSED + 1))
65+
echo "ok $((test_number+=1)) - multiple file arguments"
7566
else
76-
echo "not ok $TEST_NUMBER_INTERNAL - multiple file arguments"
67+
echo "not ok $((test_number+=1)) - multiple file arguments"
7768
echo "# Output did not contain both test files"
78-
TESTS_FAILED=$((TESTS_FAILED + 1))
69+
failures=$((failures + 1))
7970
fi
8071

8172
# Clean up
8273
echo "# Tests completed, cleaning up"
8374
cleanup_test_dir "$test_dir"
8475

85-
# Update test results file
86-
echo "PASSED:$TESTS_PASSED" > "$TEST_DIR/test_results.txt"
87-
echo "FAILED:$TESTS_FAILED" >> "$TEST_DIR/test_results.txt"
88-
8976
# Exit with success if all tests passed
90-
if [ $TESTS_FAILED -eq 0 ]; then
91-
exit 0
92-
else
93-
exit 1
94-
fi
77+
exit $failures

0 commit comments

Comments
 (0)