Back to Repositories

Testing Git Flag Placement Correction in TheFuck

This test suite validates the git_flag_after_filename rule in TheFuck project, which handles Git command flag ordering issues. The tests ensure proper correction of Git commands where flags are incorrectly placed after filenames.

Test Coverage Overview

The test suite provides comprehensive coverage of Git command flag placement scenarios.

Key areas tested include:
  • Single file commands with -p flag
  • Multiple file commands with -p flag
  • Commands with –name-only flag
  • Different Git error message formats
Edge cases cover various command patterns and error message variations to ensure robust flag reordering.

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple scenarios with minimal code duplication.

Key implementation patterns:
  • Parametrized test cases for both matching and command correction
  • Separate test functions for positive and negative matches
  • Command object usage for input/output simulation

Technical Details

Testing tools and configuration:
  • Pytest framework for test organization
  • Command class from thefuck.types for command representation
  • Parametrize decorator for test case management
  • Direct assertion testing for command matching and transformation

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable practices include:
  • Clear separation of test cases
  • Comprehensive error scenario coverage
  • Consistent test structure
  • Efficient test case organization using parametrization
  • Clear input/output mapping for command corrections

nvbn/thefuck

tests/rules/test_git_flag_after_filename.py

            
import pytest
from thefuck.rules.git_flag_after_filename import match, get_new_command
from thefuck.types import Command

command1 = Command('git log README.md -p',
                   "fatal: bad flag '-p' used after filename")
command2 = Command('git log README.md -p CONTRIBUTING.md',
                   "fatal: bad flag '-p' used after filename")
command3 = Command('git log -p README.md --name-only',
                   "fatal: bad flag '--name-only' used after filename")
command4 = Command('git log README.md -p',
                   "fatal: option '-p' must come before non-option arguments")
command5 = Command('git log README.md -p CONTRIBUTING.md',
                   "fatal: option '-p' must come before non-option arguments")
command6 = Command('git log -p README.md --name-only',
                   "fatal: option '--name-only' must come before non-option arguments")


@pytest.mark.parametrize('command', [
    command1, command2, command3, command4, command5, command6])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('git log README.md', ''),
    Command('git log -p README.md', '')])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, result', [
    (command1, "git log -p README.md"),
    (command2, "git log -p README.md CONTRIBUTING.md"),
    (command3, "git log -p --name-only README.md"),
    (command4, "git log -p README.md"),
    (command5, "git log -p README.md CONTRIBUTING.md"),
    (command6, "git log -p --name-only README.md")])
def test_get_new_command(command, result):
    assert get_new_command(command) == result