Back to Repositories

Testing Git Command Alias Expansion and Detection in thefuck

A comprehensive test suite for validating Git command alias expansion and Git-related command detection in the ‘thefuck’ project. The tests verify the correct interpretation of Git aliases and proper identification of Git commands versus non-Git commands.

Test Coverage Overview

The test suite provides extensive coverage of Git command handling functionality.

Key areas tested include:
  • Git alias expansion for common commands (checkout, commit, branch)
  • Command identification for both git and hub commands
  • Various command formats including simple aliases and complex commands with parameters
  • Edge cases with empty and null command outputs

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator for comprehensive test case coverage.

Key implementation patterns include:
  • Decorator-based test organization using @git_support
  • Command object validation using custom types
  • Parametrized test cases for multiple scenarios
  • Separation of alias expansion and command matching tests

Technical Details

Testing infrastructure includes:
  • pytest framework for test execution
  • Custom Command type for input handling
  • git_support decorator for Git-specific functionality
  • Parametrized test fixtures for varied test cases
  • Mock command outputs simulating Git trace messages

Best Practices Demonstrated

The test suite exemplifies several testing best practices for command-line tool validation.

Notable practices include:
  • Comprehensive parameter combinations using pytest.mark.parametrize
  • Clear separation of test cases for different functionalities
  • Explicit test case documentation through descriptive parameters
  • Efficient test organization using decorators
  • Proper handling of edge cases and command variations

nvbn/thefuck

tests/specific/test_git.py

            
import pytest
from thefuck.specific.git import git_support
from thefuck.types import Command


@pytest.mark.parametrize('called, command, output', [
    ('git co', 'git checkout', "19:22:36.299340 git.c:282   trace: alias expansion: co => 'checkout'"),
    ('git com file', 'git commit --verbose file',
     "19:23:25.470911 git.c:282   trace: alias expansion: com => 'commit' '--verbose'"),
    ('git com -m "Initial commit"', 'git commit -m "Initial commit"',
     "19:22:36.299340 git.c:282   trace: alias expansion: com => 'commit'"),
    ('git br -d some_branch', 'git branch -d some_branch',
     "19:22:36.299340 git.c:282   trace: alias expansion: br => 'branch'")])
def test_git_support(called, command, output):
    @git_support
    def fn(command):
        return command.script

    assert fn(Command(called, output)) == command


@pytest.mark.parametrize('command, is_git', [
    ('git pull', True),
    ('hub pull', True),
    ('git push --set-upstream origin foo', True),
    ('hub push --set-upstream origin foo', True),
    ('ls', False),
    ('cat git', False),
    ('cat hub', False)])
@pytest.mark.parametrize('output', ['', None])
def test_git_support_match(command, is_git, output):
    @git_support
    def fn(command):
        return True

    assert fn(Command(command, output)) == is_git