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