Back to Repositories

Testing Hyphen-Subcommand Correction Logic in thefuck

This test suite validates the functionality of correcting improperly hyphenated command syntax in the thefuck command-line tool. It specifically focuses on scenarios where users mistakenly add hyphens before subcommands in common CLI operations like git and apt commands.

Test Coverage Overview

The test suite provides comprehensive coverage for the wrong_hyphen_before_subcommand rule, focusing on command pattern validation and correction.

Key areas tested include:
  • Validation of incorrect hyphen usage in common CLI commands
  • Command matching patterns for git and apt commands
  • Edge cases with similar but valid hyphenated commands
  • Integration with the command execution pipeline

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature to efficiently test multiple command scenarios with a single test definition. The implementation leverages fixture-based dependency injection for mocking executable lookups, ensuring isolated and reproducible tests.

Technical patterns include:
  • Parametrized test cases for both matching and non-matching scenarios
  • Fixture-based mock implementation for system executables
  • Command object testing with input/output validation

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest-mock for dependency mocking
  • Command type from thefuck.types for command representation
  • Fixture-based test setup
  • Parametrized test cases for multiple scenarios

Best Practices Demonstrated

The test implementation showcases several testing best practices in Python unit testing. It demonstrates clear separation of concerns, efficient test case organization, and proper use of mocking.

Notable practices include:
  • Isolated test cases with proper mocking
  • Comprehensive positive and negative test scenarios
  • Clear test function naming and organization
  • Efficient test case parameterization
  • Proper fixture usage for shared resources

nvbn/thefuck

tests/rules/test_wrong_hyphen_before_subcommand.py

            
import pytest

from thefuck.rules.wrong_hyphen_before_subcommand import match, get_new_command
from thefuck.types import Command


@pytest.fixture(autouse=True)
def get_all_executables(mocker):
    mocker.patch(
        "thefuck.rules.wrong_hyphen_before_subcommand.get_all_executables",
        return_value=["git", "apt", "apt-get", "ls", "pwd"],
    )


@pytest.mark.parametrize("script", ["git-log", "apt-install python"])
def test_match(script):
    assert match(Command(script, ""))


@pytest.mark.parametrize("script", ["ls -la", "git2-make", "apt-get install python"])
def test_not_match(script):
    assert not match(Command(script, ""))


@pytest.mark.parametrize(
    "script, new_command",
    [("git-log", "git log"), ("apt-install python", "apt install python")],
)
def test_get_new_command(script, new_command):
    assert get_new_command(Command(script, "")) == new_command