Back to Repositories

Testing Git Command Correction Implementation in TheFuck

This test suite validates the git_not_command rule functionality in TheFuck, which handles incorrect Git command inputs and suggests corrections. The tests verify command matching and suggestion generation for mistyped Git commands.

Test Coverage Overview

The test suite provides comprehensive coverage of Git command correction scenarios:

  • Single command suggestions (e.g. ‘brnch’ → ‘branch’)
  • Multiple similar command suggestions (e.g. ‘st’ → ‘status’, ‘stage’, etc.)
  • Command matching validation
  • Non-Git command handling

Implementation Analysis

The testing approach uses pytest fixtures to provide test data for different Git command scenarios. The implementation focuses on two main functions: match() for identifying incorrect Git commands and get_new_command() for generating suggestions.

Pytest fixtures effectively manage test data reusability and maintain clean test organization.

Technical Details

  • Testing Framework: pytest
  • Test Fixtures: git_not_command, git_command, git_not_command_one_of_this, git_not_command_closest
  • Key Functions Tested: match(), get_new_command()
  • Command Class Usage: Implements TheFuck’s Command type for input handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Fixture-based test data management
  • Clear test case separation
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Readable test naming conventions

nvbn/thefuck

tests/rules/test_git_not_command.py

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


@pytest.fixture
def git_not_command():
    return """git: 'brnch' is not a git command. See 'git --help'.

The most similar command is
branch
"""


@pytest.fixture
def git_not_command_one_of_this():
    return """git: 'st' is not a git command. See 'git --help'.

The most similar commands are
status
reset
stage
stash
stats
"""


@pytest.fixture
def git_not_command_closest():
    return '''git: 'tags' is not a git command. See 'git --help'.

The most similar commands are
\tstage
\ttag
'''


@pytest.fixture
def git_command():
    return "* master"


def test_match(git_not_command, git_command, git_not_command_one_of_this):
    assert match(Command('git brnch', git_not_command))
    assert match(Command('git st', git_not_command_one_of_this))
    assert not match(Command('ls brnch', git_not_command))
    assert not match(Command('git branch', git_command))


def test_get_new_command(git_not_command, git_not_command_one_of_this,
                         git_not_command_closest):
    assert (get_new_command(Command('git brnch', git_not_command))
            == ['git branch'])
    assert (get_new_command(Command('git st', git_not_command_one_of_this))
            == ['git stats', 'git stash', 'git stage'])
    assert (get_new_command(Command('git tags', git_not_command_closest))
            == ['git tag', 'git stage'])