Skip to content

Commit 11b4186

Browse files
committed
Add comprehensive unit tests for git-context script functionality
These new unit tests cover three key areas of the git-context script: - Argument parsing validation - Output format consistency - Prompt handling and customization The tests use Test Anything Protocol (TAP) format to provide clear, structured test results and ensure robust script behavior across different scenarios.
1 parent 4db4865 commit 11b4186

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
# test_git_argument_parsing.sh - Tests for git-context command-line 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..4"
19+
20+
# Create a temporary directory for this test
21+
test_dir=$(create_test_dir)
22+
echo "# Using temporary directory: $test_dir"
23+
24+
# Setup a minimal git repository
25+
mkdir -p "$test_dir/repo"
26+
cd "$test_dir/repo"
27+
git init > /dev/null 2>&1
28+
git config --local user.email "[email protected]"
29+
git config --local user.name "Test User"
30+
31+
# Create a test file and make an initial commit
32+
echo "Initial content" > test_file.txt
33+
git add test_file.txt
34+
git commit -m "Initial commit" > /dev/null 2>&1
35+
36+
# Ensure git-context script is executable
37+
chmod +x "$PROJECT_ROOT/git-context"
38+
39+
# Test 1: Help option
40+
help_output=$("$PROJECT_ROOT/git-context" --help 2>&1)
41+
if echo "$help_output" | grep -q "Usage:"; then
42+
echo "ok $((test_number+=1)) - help option displays usage"
43+
else
44+
echo "not ok $((test_number+=1)) - help option displays usage"
45+
echo "# Help output did not contain 'Usage:'"
46+
failures=$((failures + 1))
47+
fi
48+
49+
# Test 2: Recent commits option
50+
# Modify the file and make additional commits
51+
echo "Second commit content" >> test_file.txt
52+
git add test_file.txt
53+
git commit -m "Second commit" > /dev/null 2>&1
54+
echo "Third commit content" >> test_file.txt
55+
git add test_file.txt
56+
git commit -m "Third commit" > /dev/null 2>&1
57+
58+
# Check that --recent-commits=1 only shows one commit
59+
recent_commits_output=$("$PROJECT_ROOT/git-context" --recent-commits=1 2>&1)
60+
if echo "$recent_commits_output" | grep -q "Recent Commits" &&
61+
[ $(echo "$recent_commits_output" | grep -c "commit") -eq 1 ]; then
62+
echo "ok $((test_number+=1)) - recent-commits option limits commit count"
63+
else
64+
echo "not ok $((test_number+=1)) - recent-commits option limits commit count"
65+
echo "# Output did not show exactly 1 commit with --recent-commits=1"
66+
echo "# Output: $recent_commits_output"
67+
failures=$((failures + 1))
68+
fi
69+
70+
# Test 3: No-prompt option
71+
no_prompt_output=$("$PROJECT_ROOT/git-context" --no-prompt 2>&1)
72+
if ! echo "$no_prompt_output" | grep -q "Commit Message Guidance"; then
73+
echo "ok $((test_number+=1)) - no-prompt option suppresses guidance"
74+
else
75+
echo "not ok $((test_number+=1)) - no-prompt option suppresses guidance"
76+
echo "# Output contained 'Commit Message Guidance' despite --no-prompt"
77+
failures=$((failures + 1))
78+
fi
79+
80+
# Test 4: Invalid option
81+
if ! "$PROJECT_ROOT/git-context" --invalid-option >/dev/null 2>&1; then
82+
echo "ok $((test_number+=1)) - invalid option causes error"
83+
else
84+
echo "not ok $((test_number+=1)) - invalid option causes error"
85+
echo "# Command with invalid option did not fail as expected"
86+
failures=$((failures + 1))
87+
fi
88+
89+
# Clean up
90+
echo "# Tests completed, cleaning up"
91+
cleanup_test_dir "$test_dir"
92+
93+
# Exit with success if all tests passed
94+
exit $failures
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
# test_git_output_format.sh - Tests for git-context output format (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..5"
19+
20+
# Create a temporary directory for this test
21+
test_dir=$(create_test_dir)
22+
echo "# Using temporary directory: $test_dir"
23+
24+
# Setup a minimal git repository
25+
mkdir -p "$test_dir/repo"
26+
cd "$test_dir/repo"
27+
git init > /dev/null 2>&1
28+
git config --local user.email "[email protected]"
29+
git config --local user.name "Test User"
30+
31+
# Create a test file and make an initial commit
32+
echo "Initial content" > test_file.txt
33+
git add test_file.txt
34+
git commit -m "Initial commit" > /dev/null 2>&1
35+
36+
# Make a change to test file
37+
echo "Modified content" >> test_file.txt
38+
39+
# Ensure git-context script is executable
40+
chmod +x "$PROJECT_ROOT/git-context"
41+
42+
# Test 1: Check for Git Status section
43+
output=$("$PROJECT_ROOT/git-context" 2>&1)
44+
if echo "$output" | grep -q "## Git Status"; then
45+
echo "ok $((test_number+=1)) - output contains Git Status section"
46+
else
47+
echo "not ok $((test_number+=1)) - output contains Git Status section"
48+
echo "# Output did not contain Git Status section"
49+
failures=$((failures + 1))
50+
fi
51+
52+
# Test 2: Check for Current Changes section
53+
if echo "$output" | grep -q "## Current Changes (Diff)"; then
54+
echo "ok $((test_number+=1)) - output contains Current Changes section"
55+
else
56+
echo "not ok $((test_number+=1)) - output contains Current Changes section"
57+
echo "# Output did not contain Current Changes section"
58+
failures=$((failures + 1))
59+
fi
60+
61+
# Test 3: Check for Files Changed section
62+
if echo "$output" | grep -q "## Files Changed"; then
63+
echo "ok $((test_number+=1)) - output contains Files Changed section"
64+
else
65+
echo "not ok $((test_number+=1)) - output contains Files Changed section"
66+
echo "# Output did not contain Files Changed section"
67+
failures=$((failures + 1))
68+
fi
69+
70+
# Test 4: Check for Recent Commits section
71+
if echo "$output" | grep -q "## Recent Commits"; then
72+
echo "ok $((test_number+=1)) - output contains Recent Commits section"
73+
else
74+
echo "not ok $((test_number+=1)) - output contains Recent Commits section"
75+
echo "# Output did not contain Recent Commits section"
76+
failures=$((failures + 1))
77+
fi
78+
79+
# Test 5: Check if output is in markdown format
80+
if echo "$output" | grep -q "^#" && echo "$output" | grep -q "^```"; then
81+
echo "ok $((test_number+=1)) - output is in markdown format"
82+
else
83+
echo "not ok $((test_number+=1)) - output is in markdown format"
84+
echo "# Output doesn't appear to be in markdown format"
85+
failures=$((failures + 1))
86+
fi
87+
88+
# Clean up
89+
echo "# Tests completed, cleaning up"
90+
cleanup_test_dir "$test_dir"
91+
92+
# Exit with success if all tests passed
93+
exit $failures
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
3+
# test_git_prompt_handling.sh - Tests for git-context prompt handling (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+
# Setup a minimal git repository
25+
mkdir -p "$test_dir/repo"
26+
cd "$test_dir/repo"
27+
git init > /dev/null 2>&1
28+
git config --local user.email "[email protected]"
29+
git config --local user.name "Test User"
30+
31+
# Create a test file and make an initial commit
32+
echo "Initial content" > test_file.txt
33+
git add test_file.txt
34+
git commit -m "Initial commit" > /dev/null 2>&1
35+
36+
# Create test prompt files
37+
mkdir -p "$test_dir/repo/prompts"
38+
echo "Default prompt content" > "$test_dir/repo/prompts/commit_prompt.txt"
39+
echo "Conventional commit content" > "$test_dir/repo/prompts/conventional_commit.txt"
40+
echo "Custom prompt content" > "$test_dir/repo/custom_prompt.txt"
41+
42+
# Ensure git-context script is executable
43+
chmod +x "$PROJECT_ROOT/git-context"
44+
45+
# Test 1: Default prompt
46+
output=$("$PROJECT_ROOT/git-context" 2>&1)
47+
if echo "$output" | grep -q "Default prompt content"; then
48+
echo "ok $((test_number+=1)) - default prompt is included"
49+
else
50+
echo "not ok $((test_number+=1)) - default prompt is included"
51+
echo "# Output did not contain default prompt content"
52+
echo "# Output: $(echo "$output" | grep -A 2 -B 2 "Commit Message Guidance" || echo "No guidance section found")"
53+
failures=$((failures + 1))
54+
fi
55+
56+
# Test 2: Custom prompt file
57+
custom_output=$("$PROJECT_ROOT/git-context" --prompt=custom_prompt.txt 2>&1)
58+
if echo "$custom_output" | grep -q "Custom prompt content"; then
59+
echo "ok $((test_number+=1)) - custom prompt file is used"
60+
else
61+
echo "not ok $((test_number+=1)) - custom prompt file is used"
62+
echo "# Output did not contain custom prompt content"
63+
echo "# Output: $(echo "$custom_output" | grep -A 2 -B 2 "Commit Message Guidance" || echo "No guidance section found")"
64+
failures=$((failures + 1))
65+
fi
66+
67+
# Test 3: Conventional commit prompt
68+
conventional_output=$("$PROJECT_ROOT/git-context" --prompt=prompts/conventional_commit.txt 2>&1)
69+
if echo "$conventional_output" | grep -q "Conventional commit content"; then
70+
echo "ok $((test_number+=1)) - conventional commit prompt is used"
71+
else
72+
echo "not ok $((test_number+=1)) - conventional commit prompt is used"
73+
echo "# Output did not contain conventional commit content"
74+
echo "# Output: $(echo "$conventional_output" | grep -A 2 -B 2 "Commit Message Guidance" || echo "No guidance section found")"
75+
failures=$((failures + 1))
76+
fi
77+
78+
# Clean up
79+
echo "# Tests completed, cleaning up"
80+
cleanup_test_dir "$test_dir"
81+
82+
# Exit with success if all tests passed
83+
exit $failures

0 commit comments

Comments
 (0)