Back to Repositories

Testing Git Command Dash Correction in TheFuck

This test suite validates the git_two_dashes rule in TheFuck project, which corrects Git commands where single dashes are incorrectly used instead of double dashes for command options. The tests ensure proper command correction behavior across various Git operations.

Test Coverage Overview

The test suite provides comprehensive coverage for Git command corrections, specifically targeting the conversion of single-dash options to double-dash format. Key test scenarios include:

  • Common Git operations like add, checkout, commit, push, and rebase
  • Various command options including patch, amend, tags, and continue
  • Both matching and non-matching command patterns
  • Command transformation accuracy verification

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator to implement data-driven tests efficiently. The implementation follows a clear pattern of testing both positive and negative cases:

  • match() function validation for incorrect commands
  • get_new_command() function verification for correct transformations
  • Systematic validation of command corrections across different Git operations

Technical Details

Testing infrastructure includes:

  • pytest framework for test organization and execution
  • Custom Command class from thefuck.types
  • Parametrized test cases for efficient test case management
  • Mock output generation using string formatting

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive positive and negative test cases
  • Efficient use of parametrized testing to reduce code duplication
  • Clear separation of test cases for different functionalities
  • Consistent test naming and organization
  • Focused unit tests with single responsibility principle

nvbn/thefuck

tests/rules/test_git_two_dashes.py

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


output = 'error: did you mean `{}` (with two dashes ?)'.format


@pytest.mark.parametrize('command', [
    Command('git add -patch', output('--patch')),
    Command('git checkout -patch', output('--patch')),
    Command('git commit -amend', output('--amend')),
    Command('git push -tags', output('--tags')),
    Command('git rebase -continue', output('--continue'))])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('git add --patch', ''),
    Command('git checkout --patch', ''),
    Command('git commit --amend', ''),
    Command('git push --tags', ''),
    Command('git rebase --continue', '')])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, output', [
    (Command('git add -patch', output('--patch')),
        'git add --patch'),
    (Command('git checkout -patch', output('--patch')),
        'git checkout --patch'),
    (Command('git checkout -patch', output('--patch')),
        'git checkout --patch'),
    (Command('git init -bare', output('--bare')),
        'git init --bare'),
    (Command('git commit -amend', output('--amend')),
        'git commit --amend'),
    (Command('git push -tags', output('--tags')),
        'git push --tags'),
    (Command('git rebase -continue', output('--continue')),
        'git rebase --continue')])
def test_get_new_command(command, output):
    assert get_new_command(command) == output