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
0 commit comments