Skip to content

Commit fdc9005

Browse files
committed
Improve shell script test consistency and robustness
- Refactor test scripts to use parameter expansion instead of sed - Use more robust shell practices like command status checking - Export PROJECT_ROOT to make it available in test scripts - Add shellcheck-friendly modifications like local variable declarations - Improve error handling and test reliability across test suite
1 parent bf713b3 commit fdc9005

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

test/test_runner.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Get the directory where the test_runner.sh script is located
66
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
77
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
8+
export PROJECT_ROOT # Export to make it available to test scripts
89

910
# Initialize counters
1011
TESTS_PASSED=0
@@ -42,7 +43,7 @@ for test_file in "${test_files[@]}"; do
4243
else
4344
echo "not ok $TEST_NUMBER - $test_name"
4445
echo "# Test output:"
45-
echo "$test_output" | sed 's/^/# /'
46+
echo "${test_output//$'\n'/$'\n'# }" # Use parameter expansion instead of sed
4647
TESTS_FAILED=$((TESTS_FAILED + 1))
4748
fi
4849
fi

test/test_utils.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ run_test() {
1818
fi
1919

2020
# Create temporary files for command output and error
21-
local output_file=$(mktemp)
22-
local error_file=$(mktemp)
21+
local output_file
22+
output_file=$(mktemp)
23+
local error_file
24+
error_file=$(mktemp)
2325

2426
# Run the command
2527
eval "$command" > "$output_file" 2> "$error_file"
2628
local status=$?
2729

28-
local output=$(cat "$output_file")
29-
local error=$(cat "$error_file")
30+
local output
31+
output=$(cat "$output_file")
32+
local error
33+
error=$(cat "$error_file")
3034

3135
# Handle the result
3236
if [ $status -eq 0 ]; then
@@ -37,12 +41,12 @@ run_test() {
3741

3842
if [ -n "$output" ]; then
3943
echo "# Output:"
40-
echo "$output" | sed 's/^/# /'
44+
echo "${output//$'\n'/$'\n'# }" # Use parameter expansion instead of sed
4145
fi
4246

4347
if [ -n "$error" ]; then
4448
echo "# Error:"
45-
echo "$error" | sed 's/^/# /'
49+
echo "${error//$'\n'/$'\n'# }" # Use parameter expansion instead of sed
4650
fi
4751

4852
# Save the failure status for the exit code
@@ -58,7 +62,8 @@ run_test() {
5862
# Function to create a temporary directory for tests
5963
# Usage: create_test_dir
6064
create_test_dir() {
61-
local tmp_dir=$(mktemp -d -t "context-test-XXXXXX")
65+
local tmp_dir
66+
tmp_dir=$(mktemp -d -t "context-test-XXXXXX")
6267
echo "$tmp_dir"
6368
}
6469

@@ -105,7 +110,8 @@ create_test_file() {
105110
echo "It contains exactly $size_bytes bytes of data." >> "$file_path"
106111

107112
# Calculate how many bytes we need to add to reach the desired size
108-
local current_size=$(wc -c < "$file_path")
113+
local current_size
114+
current_size=$(wc -c < "$file_path")
109115
local remaining_bytes=$((size_bytes - current_size))
110116

111117
if [ $remaining_bytes -gt 0 ]; then
@@ -114,8 +120,9 @@ create_test_file() {
114120
fi
115121

116122
# Verify the file is the right size
117-
local final_size=$(wc -c < "$file_path")
118-
if [ $final_size -ne $size_bytes ]; then
123+
local final_size
124+
final_size=$(wc -c < "$file_path")
125+
if [ "$final_size" -ne "$size_bytes" ]; then
119126
echo "# Warning: Created file size ($final_size) doesn't match requested size ($size_bytes)" >&2
120127
fi
121128
}

test/unit/test_apply_md.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fi
9595

9696
# Test 2: Invalid option
9797
invalid_output=$("$PROJECT_ROOT/apply-md" --invalid-option 2>&1)
98-
if [ $? -ne 0 ] && echo "$invalid_output" | grep -q "Unknown parameter"; then
98+
if ! "$PROJECT_ROOT/apply-md" --invalid-option >/dev/null 2>&1 && echo "$invalid_output" | grep -q "Unknown parameter"; then
9999
echo "ok $((test_number+=1)) - invalid option causes error"
100100
else
101101
echo "not ok $((test_number+=1)) - invalid option causes error"
@@ -105,7 +105,7 @@ else
105105
fi
106106

107107
# Test 3: Combined arguments
108-
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --dry-run --verbose 2>&1)
108+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" --dry-run --verbose < test_input.md 2>&1)
109109
if echo "$output" | grep -q "DRY RUN" && echo "$output" | grep -q "Starting markdown code application"; then
110110
echo "ok $((test_number+=1)) - combined arguments work correctly"
111111
else
@@ -122,7 +122,7 @@ echo "Original content" > "$test_dir/file1.txt"
122122
echo "Original content" > "$test_dir/file2.txt"
123123

124124
# Test 4: Basic file update
125-
if cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" && grep -q "Updated content" test_file.txt; then
125+
if cd "$test_dir" && "$PROJECT_ROOT/apply-md" < test_input.md && grep -q "Updated content" test_file.txt; then
126126
echo "ok $((test_number+=1)) - basic file update"
127127
else
128128
echo "not ok $((test_number+=1)) - basic file update"
@@ -132,7 +132,7 @@ fi
132132

133133
# Test 5: Dry run mode
134134
echo "Original content" > "$test_dir/test_file.txt"
135-
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --dry-run 2>&1)
135+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" --dry-run < test_input.md 2>&1)
136136
if grep -q "Original content" "$test_dir/test_file.txt" && echo "$output" | grep -q "Would update file"; then
137137
echo "ok $((test_number+=1)) - dry run mode"
138138
else
@@ -143,7 +143,7 @@ else
143143
fi
144144

145145
# Test 6: Verbose mode
146-
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --verbose 2>&1)
146+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" --verbose < test_input.md 2>&1)
147147
if echo "$output" | grep -q "Starting markdown code application"; then
148148
echo "ok $((test_number+=1)) - verbose mode"
149149
else
@@ -156,7 +156,7 @@ fi
156156
echo "# Section 3: Create Missing Files Tests"
157157

158158
# Test 7: Without create-missing flag
159-
output=$(cd "$test_dir" && cat missing_files.md | "$PROJECT_ROOT/apply-md" 2>&1)
159+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" < missing_files.md 2>&1)
160160
if [ ! -f "$test_dir/new_file.txt" ] && echo "$output" | grep -q "Warning: File does not exist"; then
161161
echo "ok $((test_number+=1)) - without create-missing flag"
162162
else
@@ -167,7 +167,7 @@ else
167167
fi
168168

169169
# Test 8: With create-missing flag
170-
output=$(cd "$test_dir" && cat missing_files.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
170+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" --create-missing < missing_files.md 2>&1)
171171
if [ -f "$test_dir/new_file.txt" ] && [ -f "$test_dir/src/new_folder/script.js" ]; then
172172
echo "ok $((test_number+=1)) - with create-missing flag"
173173
else
@@ -180,7 +180,7 @@ fi
180180
echo "# Section 4: Multiple Blocks Tests"
181181

182182
# Test 9: Multiple file updates
183-
output=$(cd "$test_dir" && cat multiple_blocks.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
183+
output=$(cd "$test_dir" && "$PROJECT_ROOT/apply-md" --create-missing < multiple_blocks.md 2>&1)
184184
if grep -q "Updated content 1" "$test_dir/file1.txt" && grep -q "Updated content 2" "$test_dir/file2.txt"; then
185185
echo "ok $((test_number+=1)) - multiple file updates"
186186
else
@@ -205,4 +205,4 @@ echo "# Tests completed, cleaning up"
205205
cleanup_test_dir "$test_dir"
206206

207207
# Exit with success if all tests passed
208-
exit $failures
208+
exit $failures

test/unit/test_context.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ fi
124124

125125
# Test 8: Max size limit not exceeded
126126
small_output=$(cd "$test_dir" && "$PROJECT_ROOT/context" small.txt --max-size=2KB 2>&1)
127-
if [ $? -eq 0 ]; then
127+
# Check command status directly instead of using $?
128+
if "$PROJECT_ROOT/context" small.txt --max-size=2KB >/dev/null 2>&1; then
128129
echo "ok $((test_number+=1)) - max size limit not exceeded"
129130
else
130131
echo "not ok $((test_number+=1)) - max size limit not exceeded"

test/unit/test_git_context.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ echo "# Using temporary directory: $test_dir"
2323

2424
# Setup a minimal git repository
2525
mkdir -p "$test_dir/repo"
26-
cd "$test_dir/repo"
26+
cd "$test_dir/repo" || exit 1
2727
git init > /dev/null 2>&1
2828
git config --local user.email "test@example.com"
2929
git config --local user.name "Test User"
@@ -59,7 +59,7 @@ else
5959
fi
6060

6161
# Test 2: No-prompt option
62-
no_prompt_output=$(cd "$test_dir/repo" && "$PROJECT_ROOT/git-context" --no-prompt 2>&1)
62+
no_prompt_output=$(cd "$test_dir/repo" || exit 1 && "$PROJECT_ROOT/git-context" --no-prompt 2>&1)
6363
if ! echo "$no_prompt_output" | grep -q "Commit Message Guidance"; then
6464
echo "ok $((test_number+=1)) - no-prompt option suppresses guidance"
6565
else
@@ -80,11 +80,11 @@ fi
8080
echo "# Section 2: Output Format Tests"
8181

8282
# Make a change to test file for diff output
83-
cd "$test_dir/repo"
83+
cd "$test_dir/repo" || exit 1
8484
echo "Modified for diff" > test_file.txt
8585

8686
# Test 4: Check for Git Status section
87-
output=$(cd "$test_dir/repo" && "$PROJECT_ROOT/git-context" 2>&1)
87+
output=$(cd "$test_dir/repo" || exit 1 && "$PROJECT_ROOT/git-context" 2>&1)
8888
if echo "$output" | grep -q "## Git Status"; then
8989
echo "ok $((test_number+=1)) - output contains Git Status section"
9090
else
@@ -123,7 +123,7 @@ fi
123123
echo "# Section 3: Prompt Handling Tests"
124124

125125
# Test 8: Default prompt
126-
prompt_output=$(cd "$test_dir/repo" && "$PROJECT_ROOT/git-context" 2>&1)
126+
prompt_output=$(cd "$test_dir/repo" || exit 1 && "$PROJECT_ROOT/git-context" 2>&1)
127127
if echo "$prompt_output" | grep -q "Default prompt content"; then
128128
echo "ok $((test_number+=1)) - default prompt is included"
129129
else
@@ -134,7 +134,7 @@ else
134134
fi
135135

136136
# Test 9: Custom prompt file
137-
custom_output=$(cd "$test_dir/repo" && "$PROJECT_ROOT/git-context" --prompt=custom_prompt.txt 2>&1)
137+
custom_output=$(cd "$test_dir/repo" || exit 1 && "$PROJECT_ROOT/git-context" --prompt=custom_prompt.txt 2>&1)
138138
if echo "$custom_output" | grep -q "Custom prompt content"; then
139139
echo "ok $((test_number+=1)) - custom prompt file is used"
140140
else

0 commit comments

Comments
 (0)