Skip to content

Commit 0552615

Browse files
committed
Add comprehensive unit tests for apply-md script
These tests cover core functionality of the apply-md script, including: - Argument parsing - Basic file update mechanics - Handling of create-missing flag - Processing multiple code blocks - Dry run and verbose modes The test suite uses TAP (Test Anything Protocol) format and creates temporary test directories to validate script behavior in isolation.
1 parent a35ab98 commit 0552615

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
# test_apply_md_argument_parsing.sh - Tests for apply-md argument parsing (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..3"
19+
20+
# Create a temporary directory for this test
21+
test_dir=$(create_test_dir)
22+
echo "# Using temporary directory: $test_dir"
23+
24+
# Ensure apply-md script is executable
25+
chmod +x "$PROJECT_ROOT/apply-md"
26+
27+
# Test 1: Help option
28+
help_output=$("$PROJECT_ROOT/apply-md" --help 2>&1)
29+
if echo "$help_output" | grep -q "Usage:"; then
30+
echo "ok $((test_number+=1)) - help option displays usage"
31+
else
32+
echo "not ok $((test_number+=1)) - help option displays usage"
33+
echo "# Help output did not contain 'Usage:'"
34+
echo "# Output: $help_output"
35+
failures=$((failures + 1))
36+
fi
37+
38+
# Test 2: Invalid option
39+
invalid_output=$("$PROJECT_ROOT/apply-md" --invalid-option 2>&1)
40+
if [ $? -ne 0 ] && echo "$invalid_output" | grep -q "Unknown parameter"; then
41+
echo "ok $((test_number+=1)) - invalid option causes error"
42+
else
43+
echo "not ok $((test_number+=1)) - invalid option causes error"
44+
echo "# Command with invalid option did not fail as expected"
45+
echo "# Output: $invalid_output"
46+
failures=$((failures + 1))
47+
fi
48+
49+
# Test 3: Combined arguments
50+
# Create test file
51+
echo "Original content" > "$test_dir/test_file.txt"
52+
53+
# Create test markdown content
54+
cat > "$test_dir/test_input.md" << EOF
55+
\`\`\`txt test_file.txt
56+
Updated content
57+
\`\`\`
58+
EOF
59+
60+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --dry-run --verbose 2>&1)
61+
if echo "$output" | grep -q "DRY RUN" && echo "$output" | grep -q "Starting markdown code application"; then
62+
echo "ok $((test_number+=1)) - combined arguments work correctly"
63+
else
64+
echo "not ok $((test_number+=1)) - combined arguments work correctly"
65+
echo "# Combined arguments did not produce expected output"
66+
echo "# Output: $output"
67+
failures=$((failures + 1))
68+
fi
69+
70+
# Clean up
71+
echo "# Tests completed, cleaning up"
72+
cleanup_test_dir "$test_dir"
73+
74+
# Exit with success if all tests passed
75+
exit $failures

test/unit/test_apply_md_basic.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
# test_apply_md_basic.sh - Basic tests for apply-md functionality (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..3"
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
25+
echo "Original content" > "$test_dir/test_file.txt"
26+
27+
# Create test markdown content
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+
# Ensure apply-md script is executable
39+
chmod +x "$PROJECT_ROOT/apply-md"
40+
41+
# Test 1: Basic file update
42+
if cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" && grep -q "Updated content" test_file.txt; then
43+
echo "ok $((test_number+=1)) - basic file update"
44+
else
45+
echo "not ok $((test_number+=1)) - basic file update"
46+
echo "# File was not updated correctly"
47+
failures=$((failures + 1))
48+
fi
49+
50+
# Test 2: Dry run mode
51+
echo "Original content" > "$test_dir/test_file.txt"
52+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --dry-run 2>&1)
53+
if grep -q "Original content" "$test_dir/test_file.txt" && echo "$output" | grep -q "Would update file"; then
54+
echo "ok $((test_number+=1)) - dry run mode"
55+
else
56+
echo "not ok $((test_number+=1)) - dry run mode"
57+
echo "# Dry run modified the file or didn't show the correct output"
58+
echo "# Output: $output"
59+
failures=$((failures + 1))
60+
fi
61+
62+
# Test 3: Verbose mode
63+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --verbose 2>&1)
64+
if echo "$output" | grep -q "Starting markdown code application"; then
65+
echo "ok $((test_number+=1)) - verbose mode"
66+
else
67+
echo "not ok $((test_number+=1)) - verbose mode"
68+
echo "# Verbose mode didn't display expected output"
69+
echo "# Output: $output"
70+
failures=$((failures + 1))
71+
fi
72+
73+
# Clean up
74+
echo "# Tests completed, cleaning up"
75+
cleanup_test_dir "$test_dir"
76+
77+
# Exit with success if all tests passed
78+
exit $failures
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
# test_apply_md_create_missing.sh - Tests for create-missing functionality (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..2"
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 markdown content with reference to a non-existent file
25+
cat > "$test_dir/test_input.md" << EOF
26+
# Test Markdown
27+
28+
This is a test markdown file with code blocks referencing files that don't exist.
29+
30+
\`\`\`txt new_file.txt
31+
This is content for a new file
32+
\`\`\`
33+
34+
\`\`\`js src/new_folder/script.js
35+
// This is a JavaScript file in a new directory
36+
function hello() {
37+
return "world";
38+
}
39+
\`\`\`
40+
EOF
41+
42+
# Ensure apply-md script is executable
43+
chmod +x "$PROJECT_ROOT/apply-md"
44+
45+
# Test 1: Without create-missing flag
46+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" 2>&1)
47+
if [ ! -f "$test_dir/new_file.txt" ] && echo "$output" | grep -q "Warning: File does not exist"; then
48+
echo "ok $((test_number+=1)) - without create-missing flag"
49+
else
50+
echo "not ok $((test_number+=1)) - without create-missing flag"
51+
echo "# File was created when it shouldn't have been or warning not shown"
52+
echo "# Output: $output"
53+
failures=$((failures + 1))
54+
fi
55+
56+
# Test 2: With create-missing flag
57+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
58+
if [ -f "$test_dir/new_file.txt" ] && [ -f "$test_dir/src/new_folder/script.js" ]; then
59+
echo "ok $((test_number+=1)) - with create-missing flag"
60+
else
61+
echo "not ok $((test_number+=1)) - with create-missing flag"
62+
echo "# Files were not created correctly"
63+
echo "# Output: $output"
64+
failures=$((failures + 1))
65+
fi
66+
67+
# Clean up
68+
echo "# Tests completed, cleaning up"
69+
cleanup_test_dir "$test_dir"
70+
71+
# Exit with success if all tests passed
72+
exit $failures
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
# test_apply_md_multiple_blocks.sh - Tests for handling multiple code blocks (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..2"
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 files
25+
echo "Original content 1" > "$test_dir/file1.txt"
26+
echo "Original content 2" > "$test_dir/file2.txt"
27+
28+
# Create test markdown content with multiple code blocks
29+
cat > "$test_dir/test_input.md" << EOF
30+
# Test Markdown
31+
32+
This is a test markdown file with multiple code blocks.
33+
34+
\`\`\`txt file1.txt
35+
Updated content 1
36+
\`\`\`
37+
38+
Some text in between code blocks.
39+
40+
\`\`\`txt file2.txt
41+
Updated content 2
42+
\`\`\`
43+
44+
Another code block with a different language:
45+
46+
\`\`\`js file3.js
47+
// This is JavaScript content
48+
console.log("Hello world");
49+
\`\`\`
50+
EOF
51+
52+
# Ensure apply-md script is executable
53+
chmod +x "$PROJECT_ROOT/apply-md"
54+
55+
# Test 1: Multiple file updates
56+
output=$(cd "$test_dir" && cat test_input.md | "$PROJECT_ROOT/apply-md" --create-missing 2>&1)
57+
if grep -q "Updated content 1" "$test_dir/file1.txt" && grep -q "Updated content 2" "$test_dir/file2.txt"; then
58+
echo "ok $((test_number+=1)) - multiple file updates"
59+
else
60+
echo "not ok $((test_number+=1)) - multiple file updates"
61+
echo "# Files were not updated correctly"
62+
echo "# Output: $output"
63+
failures=$((failures + 1))
64+
fi
65+
66+
# Test 2: Language specification is handled correctly
67+
if [ -f "$test_dir/file3.js" ] && grep -q "Hello world" "$test_dir/file3.js"; then
68+
echo "ok $((test_number+=1)) - language specification handled correctly"
69+
else
70+
echo "not ok $((test_number+=1)) - language specification handled correctly"
71+
echo "# File with language specification not created or updated correctly"
72+
echo "# Output: $output"
73+
failures=$((failures + 1))
74+
fi
75+
76+
# Clean up
77+
echo "# Tests completed, cleaning up"
78+
cleanup_test_dir "$test_dir"
79+
80+
# Exit with success if all tests passed
81+
exit $failures

0 commit comments

Comments
 (0)