Skip to content

Commit 1a6f687

Browse files
committed
Consolidate test suite for CLI tools into single comprehensive test files
This change simplifies the test suite by: - Merging multiple small test files for each CLI tool into single comprehensive test files - Reducing test script complexity - Improving overall test coverage - Maintaining TAP-compliant test format - Updating test runner to support new test file structure The modifications improve test organization and reduce redundancy in test scripts while preserving the existing test coverage for context, git-context, and apply-md CLI tools.
1 parent 0552615 commit 1a6f687

14 files changed

+587
-818
lines changed

test/test_runner.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# test_runner.sh - Main test runner for context script (TAP-compliant)
3+
# test_runner.sh - Main test runner for CLI tools (TAP-compliant)
44

55
# Get the directory where the test_runner.sh script is located
66
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -14,8 +14,8 @@ TEST_NUMBER=0
1414
# Print TAP header
1515
echo "TAP version 13"
1616

17-
# Find all test files
18-
test_files=("$TEST_DIR"/unit/test_*.sh)
17+
# Find all consolidated test files (we now have one file per CLI tool)
18+
test_files=("$TEST_DIR"/test_*.sh)
1919
total_test_files=${#test_files[@]}
2020
echo "1..$total_test_files"
2121

@@ -28,7 +28,9 @@ done
2828
for test_file in "${test_files[@]}"; do
2929
if [ -f "$test_file" ]; then
3030
TEST_NUMBER=$((TEST_NUMBER + 1))
31-
test_name=$(basename "$test_file" .sh | sed 's/^test_//')
31+
test_name=$(basename "$test_file" .sh)
32+
33+
echo "# Running test suite: $test_name"
3234

3335
# Run the test and capture its output
3436
test_output=$("$test_file" 2>&1)
@@ -57,4 +59,4 @@ if [ $TESTS_FAILED -gt 0 ]; then
5759
exit 1
5860
fi
5961

60-
exit 0
62+
exit 0

test/unit/test_apply_md.sh

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#!/bin/bash
2+
3+
# test_apply_md.sh - Comprehensive tests for the apply-md CLI tool (TAP-compliant)
4+
5+
# Get the directory where this test script is located
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
TEST_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
8+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
9+
10+
# Source test utilities
11+
source "$TEST_DIR/test_utils.sh"
12+
13+
# Initialize variables
14+
test_number=0
15+
failures=0
16+
17+
# Print TAP plan
18+
echo "1..10"
19+
20+
# Create a temporary directory for this test
21+
test_dir=$(create_test_dir)
22+
echo "# Using temporary directory: $test_dir"
23+
24+
# Create test file for basic tests
25+
echo "Original content" > "$test_dir/test_file.txt"
26+
27+
# Create test markdown content for basic tests
28+
cat > "$test_dir/test_input.md" << EOF
29+
# Test Markdown
30+
31+
This is a test markdown file with code blocks.
32+
33+
\`\`\`txt test_file.txt
34+
Updated content
35+
\`\`\`
36+
EOF
37+
38+
# Create test markdown with multiple code blocks
39+
cat > "$test_dir/multiple_blocks.md" << EOF
40+
# Test Markdown
41+
42+
This is a test markdown file with multiple code blocks.
43+
44+
\`\`\`txt file1.txt
45+
Updated content 1
46+
\`\`\`
47+
48+
Some text in between code blocks.
49+
50+
\`\`\`txt file2.txt
51+
Updated content 2
52+
\`\`\`
53+
54+
Another code block with a different language:
55+
56+
\`\`\`js file3.js
57+
// This is JavaScript content
58+
console.log("Hello world");
59+
\`\`\`
60+
EOF
61+
62+
# Create test markdown content with reference to non-existent files
63+
cat > "$test_dir/missing_files.md" << EOF
64+
# Test Markdown
65+
66+
This is a test markdown file with code blocks referencing files that don't exist.
67+
68+
\`\`\`txt new_file.txt
69+
This is content for a new file
70+
\`\`\`
71+
72+
\`\`\`js src/new_folder/script.js
73+
// This is a JavaScript file in a new directory
74+
function hello() {
75+
return "world";
76+
}
77+
\`\`\`
78+
EOF
79+
80+
# Ensure apply-md script is executable
81+
chmod +x "$PROJECT_ROOT/apply-md"
82+
83+
echo "# Section 1: Argument Parsing Tests"
84+
85+
# Test 1: Help option
86+
help_output=$("$PROJECT_ROOT/apply-md" --help 2>&1)
87+
if echo "$help_output" | grep -q "Usage:"; then
88+
echo "ok $((test_number+=1)) - help option displays usage"
89+
else
90+
echo "not ok $((test_number+=1)) - help option displays usage"
91+
echo "# Help output did not contain 'Usage:'"
92+
echo "# Output: $help_output"
93+
failures=$((failures + 1))
94+
fi
95+
96+
# Test 2: Invalid option
97+
invalid_output=$("$PROJECT_ROOT/apply-md" --invalid-option 2>&1)
98+
if [ $? -ne 0 ] && echo "$invalid_output" | grep -q "Unknown parameter"; then
99+
echo "ok $((test_number+=1)) - invalid option causes error"
100+
else
101+
echo "not ok $((test_number+=1)) - invalid option causes error"
102+
echo "# Command with invalid option did not fail as expected"
103+
echo "# Output: $invalid_output"
104+
failures=$((failures + 1))
105+
fi
106+
107+
# Test 3: Combined arguments
108+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --dry-run --verbose 2>&1)
109+
if echo "$output" | grep -q "DRY RUN" && echo "$output" | grep -q "Starting markdown code application"; then
110+
echo "ok $((test_number+=1)) - combined arguments work correctly"
111+
else
112+
echo "not ok $((test_number+=1)) - combined arguments work correctly"
113+
echo "# Combined arguments did not produce expected output"
114+
echo "# Output: $output"
115+
failures=$((failures + 1))
116+
fi
117+
118+
echo "# Section 2: Basic Functionality Tests"
119+
120+
# Create test files for basic functionality
121+
echo "Original content" > "$test_dir/file1.txt"
122+
echo "Original content" > "$test_dir/file2.txt"
123+
124+
# 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
126+
echo "ok $((test_number+=1)) - basic file update"
127+
else
128+
echo "not ok $((test_number+=1)) - basic file update"
129+
echo "# File was not updated correctly"
130+
failures=$((failures + 1))
131+
fi
132+
133+
# Test 5: Dry run mode
134+
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)
136+
if grep -q "Original content" "$test_dir/test_file.txt" && echo "$output" | grep -q "Would update file"; then
137+
echo "ok $((test_number+=1)) - dry run mode"
138+
else
139+
echo "not ok $((test_number+=1)) - dry run mode"
140+
echo "# Dry run modified the file or didn't show the correct output"
141+
echo "# Output: $output"
142+
failures=$((failures + 1))
143+
fi
144+
145+
# Test 6: Verbose mode
146+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --verbose 2>&1)
147+
if echo "$output" | grep -q "Starting markdown code application"; then
148+
echo "ok $((test_number+=1)) - verbose mode"
149+
else
150+
echo "not ok $((test_number+=1)) - verbose mode"
151+
echo "# Verbose mode didn't display expected output"
152+
echo "# Output: $output"
153+
failures=$((failures + 1))
154+
fi
155+
156+
echo "# Section 3: Create Missing Files Tests"
157+
158+
# Test 7: Without create-missing flag
159+
output=$(cd "$test_dir" && cat missing_files.md | "$PROJECT_ROOT/apply-md" 2>&1)
160+
if [ ! -f "$test_dir/new_file.txt" ] && echo "$output" | grep -q "Warning: File does not exist"; then
161+
echo "ok $((test_number+=1)) - without create-missing flag"
162+
else
163+
echo "not ok $((test_number+=1)) - without create-missing flag"
164+
echo "# File was created when it shouldn't have been or warning not shown"
165+
echo "# Output: $output"
166+
failures=$((failures + 1))
167+
fi
168+
169+
# Test 8: With create-missing flag
170+
output=$(cd "$test_dir" && cat missing_files.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
171+
if [ -f "$test_dir/new_file.txt" ] && [ -f "$test_dir/src/new_folder/script.js" ]; then
172+
echo "ok $((test_number+=1)) - with create-missing flag"
173+
else
174+
echo "not ok $((test_number+=1)) - with create-missing flag"
175+
echo "# Files were not created correctly"
176+
echo "# Output: $output"
177+
failures=$((failures + 1))
178+
fi
179+
180+
echo "# Section 4: Multiple Blocks Tests"
181+
182+
# Test 9: Multiple file updates
183+
output=$(cd "$test_dir" && cat multiple_blocks.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
184+
if grep -q "Updated content 1" "$test_dir/file1.txt" && grep -q "Updated content 2" "$test_dir/file2.txt"; then
185+
echo "ok $((test_number+=1)) - multiple file updates"
186+
else
187+
echo "not ok $((test_number+=1)) - multiple file updates"
188+
echo "# Files were not updated correctly"
189+
echo "# Output: $output"
190+
failures=$((failures + 1))
191+
fi
192+
193+
# Test 10: Language specification is handled correctly
194+
if [ -f "$test_dir/file3.js" ] && grep -q "Hello world" "$test_dir/file3.js"; then
195+
echo "ok $((test_number+=1)) - language specification handled correctly"
196+
else
197+
echo "not ok $((test_number+=1)) - language specification handled correctly"
198+
echo "# File with language specification not created or updated correctly"
199+
echo "# Output: $output"
200+
failures=$((failures + 1))
201+
fi
202+
203+
# Clean up
204+
echo "# Tests completed, cleaning up"
205+
cleanup_test_dir "$test_dir"
206+
207+
# Exit with success if all tests passed
208+
exit $failures

test/unit/test_apply_md_argument_parsing.sh

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)