Skip to content

Commit a90dc78

Browse files
committed
Enhance test suite reliability and error handling
- Improve test runner error tracking and reporting - Add more robust error handling in test utility functions - Enhance test scripts with better directory navigation and error checking - Fix shellcheck warnings and improve shell script practices - Add explicit exit codes and comprehensive test logging
1 parent 101577c commit a90dc78

File tree

5 files changed

+72
-26
lines changed

5 files changed

+72
-26
lines changed

test/test_runner.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ for test_file in "$TEST_DIR"/unit/test_*.sh; do
2424
echo "Running tests in $(basename "$test_file")"
2525
echo "------------------------------------"
2626
bash "$test_file"
27+
28+
# Capture exit status
29+
test_status=$?
30+
if [ $test_status -ne 0 ]; then
31+
echo "Test file $(basename "$test_file") failed with status $test_status"
32+
fi
2733
fi
2834
done
2935

test/test_utils.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
TESTS_PASSED=0
77
TESTS_FAILED=0
88

9-
# Get the directory where the test_utils.sh script is located
10-
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11-
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
12-
139
# Function to run a test
1410
# Usage: run_test "test_name" "command_to_run"
1511
run_test() {
@@ -19,7 +15,7 @@ run_test() {
1915
echo -n "- Testing $test_name... "
2016

2117
# Run the command and capture output and exit status
22-
output=$(eval $command 2>&1)
18+
output=$(eval "$command" 2>&1)
2319
status=$?
2420

2521
if [ $status -eq 0 ]; then
@@ -45,7 +41,9 @@ create_test_dir() {
4541
# Usage: cleanup_test_dir "$test_dir"
4642
cleanup_test_dir() {
4743
local test_dir=$1
48-
rm -rf "$test_dir"
44+
if [ -d "$test_dir" ]; then
45+
rm -rf "$test_dir"
46+
fi
4947
}
5048

5149
# Function to assert that a string contains another string
@@ -80,5 +78,9 @@ create_test_file() {
8078
local file_path=$1
8179
local size_bytes=$2
8280

83-
dd if=/dev/zero of="$file_path" bs=1 count=$size_bytes status=none
81+
dd if=/dev/zero of="$file_path" bs=1 count="$size_bytes" status=none 2>/dev/null
8482
}
83+
84+
# Export variables to be available in subprocesses
85+
export TESTS_PASSED
86+
export TESTS_FAILED

test/unit/test_argument_parsing.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,50 @@
33
# test_argument_parsing.sh - Tests for command-line argument parsing
44

55
# Get the directory where this test script is located
6-
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
TEST_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
78
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
89

910
# Source test utilities
1011
source "$TEST_DIR/test_utils.sh"
1112

13+
# Test description
14+
echo "Testing command-line argument parsing"
15+
1216
# Create a temporary directory for this test
1317
test_dir=$(create_test_dir)
18+
echo "Using temporary directory: $test_dir"
1419

1520
# Setup test file
1621
echo "// Test file" > "$test_dir/test.js"
1722

23+
# Ensure context script is executable
24+
chmod +x "$PROJECT_ROOT/context"
25+
1826
# Test 1: Help option
1927
echo "Testing help option"
20-
run_test "help_option" "$PROJECT_ROOT/context --help | grep -q 'Usage:'"
28+
run_test "help_option" "\"$PROJECT_ROOT/context\" --help | grep -q 'Usage:'"
2129

2230
# Test 2: Summary option
2331
echo "Testing summary option"
24-
run_test "summary_option" "cd $test_dir && $PROJECT_ROOT/context --files=test.js --summary | grep -q 'Summary:'"
32+
run_test "summary_option" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"test.js\" --summary | grep -q 'Summary:'"
2533

2634
# Test 3: No ls-files option
2735
echo "Testing no ls-files option"
28-
run_test "no_ls_files_option" "cd $test_dir && $PROJECT_ROOT/context --files=test.js --no-ls-files | grep -qv 'Repository Files'"
36+
run_test "no_ls_files_option" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"test.js\" --no-ls-files | grep -qv 'Repository Files'"
2937

3038
# Test 4: Verbose option
3139
echo "Testing verbose option with non-existent file"
32-
output=$(cd $test_dir && $PROJECT_ROOT/context --files=nonexistent.js --verbose 2>&1 || true)
33-
run_test "verbose_option" "echo '$output' | grep -q 'Warning: File not found:'"
40+
output=$(cd "$test_dir" && "$PROJECT_ROOT/context" --files="nonexistent.js" --verbose 2>&1 || true)
41+
run_test "verbose_option" "echo \"$output\" | grep -q 'Warning: File not found:'"
3442

3543
# Test 5: Invalid option
3644
echo "Testing invalid option"
37-
run_test "invalid_option" "! $PROJECT_ROOT/context --invalid-option 2>/dev/null"
45+
run_test "invalid_option" "! \"$PROJECT_ROOT/context\" --invalid-option 2>/dev/null"
3846

3947
# Clean up
48+
echo "Cleaning up temporary directory"
4049
cleanup_test_dir "$test_dir"
50+
51+
# Exit with success
52+
exit 0

test/unit/test_file_processing.sh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
# test_file_processing.sh - Tests for file processing functionality
44

55
# Get the directory where this test script is located
6-
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
TEST_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
78
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
89

910
# Source test utilities
1011
source "$TEST_DIR/test_utils.sh"
1112

13+
# Test description
14+
echo "Testing file processing functionality"
15+
1216
# Create a temporary directory for this test
1317
test_dir=$(create_test_dir)
18+
echo "Using temporary directory: $test_dir"
1419

1520
# Setup test files
1621
mkdir -p "$test_dir/src"
@@ -19,21 +24,29 @@ echo "// Test file 2" > "$test_dir/src/file2.js"
1924
mkdir -p "$test_dir/node_modules"
2025
echo "// Should be excluded" > "$test_dir/node_modules/exclude_me.js"
2126

27+
# Ensure context script is executable
28+
chmod +x "$PROJECT_ROOT/context"
29+
2230
# Test 1: Basic file inclusion
2331
echo "Testing basic file inclusion"
24-
run_test "basic_file_inclusion" "cd $test_dir && $PROJECT_ROOT/context --files=src/file1.js | grep -q 'Test file 1'"
32+
run_test "basic_file_inclusion" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" \"src/file1.js\" | grep -q 'Test file 1'"
2533

2634
# Test 2: File pattern inclusion
2735
echo "Testing file pattern inclusion"
28-
run_test "file_pattern_inclusion" "cd $test_dir && $PROJECT_ROOT/context --files='src/*.js' | grep -q 'Test file 2'"
36+
run_test "file_pattern_inclusion" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"src/*.js\" | grep -q 'Test file 2'"
2937

3038
# Test 3: Exclude pattern
3139
echo "Testing exclude pattern"
32-
run_test "exclude_pattern" "cd $test_dir && $PROJECT_ROOT/context --files='**/*.js' --exclude='node_modules/**' | grep -qv 'Should be excluded'"
40+
run_test "exclude_pattern" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"**/*.js\" --exclude=\"node_modules/**\" | grep -qv 'Should be excluded'"
3341

3442
# Test 4: Multiple file patterns
3543
echo "Testing multiple file patterns"
36-
run_test "multiple_file_patterns" "cd $test_dir && $PROJECT_ROOT/context --files='src/file1.js' --files='src/file2.js' | grep -q 'Test file 1' && cd $test_dir && $PROJECT_ROOT/context --files='src/file1.js' --files='src/file2.js' | grep -q 'Test file 2'"
44+
multiple_files_test="cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"src/file1.js\" --files=\"src/file2.js\" | grep -q 'Test file 1' && cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"src/file1.js\" --files=\"src/file2.js\" | grep -q 'Test file 2'"
45+
run_test "multiple_file_patterns" "$multiple_files_test"
3746

3847
# Clean up
48+
echo "Cleaning up temporary directory"
3949
cleanup_test_dir "$test_dir"
50+
51+
# Exit with success
52+
exit 0

test/unit/test_size_calculations.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,49 @@
33
# test_size_calculations.sh - Tests for size calculation and truncation
44

55
# Get the directory where this test script is located
6-
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
TEST_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
78
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
89

910
# Source test utilities
1011
source "$TEST_DIR/test_utils.sh"
1112

13+
# Test description
14+
echo "Testing size calculations and truncation"
15+
1216
# Create a temporary directory for this test
1317
test_dir=$(create_test_dir)
18+
echo "Using temporary directory: $test_dir"
1419

1520
# Setup test files
1621
create_test_file "$test_dir/small.txt" 1024 # 1KB file
1722
create_test_file "$test_dir/large.txt" 5120 # 5KB file
1823

24+
# Ensure context script is executable
25+
chmod +x "$PROJECT_ROOT/context"
26+
1927
# Test 1: Max size limit not exceeded
2028
echo "Testing max size limit not exceeded"
21-
run_test "max_size_not_exceeded" "cd $test_dir && $PROJECT_ROOT/context --files=small.txt --max-size=2KB"
29+
run_test "max_size_not_exceeded" "cd \"$test_dir\" && \"$PROJECT_ROOT/context\" --files=\"small.txt\" --max-size=2KB"
2230

2331
# Test 2: Max size limit exceeded
2432
echo "Testing max size limit exceeded"
25-
run_test "max_size_exceeded" "cd $test_dir && ! $PROJECT_ROOT/context --files=small.txt --files=large.txt --max-size=2KB"
33+
max_size_exceeded_test="cd \"$test_dir\" && ! \"$PROJECT_ROOT/context\" --files=\"small.txt\" --files=\"large.txt\" --max-size=2KB"
34+
run_test "max_size_exceeded" "$max_size_exceeded_test"
2635

2736
# Test 3: File truncation
2837
echo "Testing file truncation"
29-
result=$(cd $test_dir && $PROJECT_ROOT/context --files=large.txt --truncate-large=2KB)
30-
run_test "file_truncation" "echo '$result' | grep -q '\[File truncated due to size limit\]'"
38+
result=$(cd "$test_dir" && "$PROJECT_ROOT/context" --files="large.txt" --truncate-large=2KB)
39+
run_test "file_truncation" "echo \"$result\" | grep -q '\[File truncated due to size limit\]'"
3140

3241
# Test 4: Human readable size display
3342
echo "Testing human readable size display"
34-
result=$(cd $test_dir && $PROJECT_ROOT/context --files=small.txt --show-file-sizes)
35-
run_test "human_readable_size" "echo '$result' | grep -q 'Size: 1 KB'"
43+
result=$(cd "$test_dir" && "$PROJECT_ROOT/context" --files="small.txt" --show-file-sizes)
44+
run_test "human_readable_size" "echo \"$result\" | grep -q 'Size: 1 KB'"
3645

3746
# Clean up
47+
echo "Cleaning up temporary directory"
3848
cleanup_test_dir "$test_dir"
49+
50+
# Exit with success
51+
exit 0

0 commit comments

Comments
 (0)