Back to Repositories

Testing AG Command Literal Search Transformation in thefuck

This test suite validates the ag_literal rule functionality in thefuck project, focusing on handling literal string searches with the ag command when regex patterns fail. The tests ensure proper command transformation for escaped characters.

Test Coverage Overview

The test suite provides comprehensive coverage for the ag_literal rule implementation.

Key areas tested include:
  • Regex failure detection with escaped parentheses
  • Command matching for invalid regex patterns
  • Transformation of commands to use literal search (-Q flag)
  • Negative test cases for non-matching scenarios

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case handling. The implementation follows fixture-based testing patterns with parameterized inputs to validate both matching and command transformation logic.

Framework features utilized:
  • pytest.fixture for output setup
  • pytest.mark.parametrize for test case variations
  • Command object usage for input/output handling

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type from thefuck.types
  • Fixture-based test setup
  • Parameterized test execution
  • Direct assertion testing

Best Practices Demonstrated

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

Notable practices include:
  • Separation of test cases into match and transformation scenarios
  • Clear test function naming conventions
  • Efficient test case organization using parametrization
  • Proper fixture usage for common test data
  • Explicit positive and negative test cases

nvbn/thefuck

tests/rules/test_ag_literal.py

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


@pytest.fixture
def output():
    return ('ERR: Bad regex! pcre_compile() failed at position 1: missing )
'
            'If you meant to search for a literal string, run ag with -Q
')


@pytest.mark.parametrize('script', ['ag \\('])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script', ['ag foo'])
def test_not_match(script):
    assert not match(Command(script, ''))


@pytest.mark.parametrize('script, new_cmd', [
    ('ag \\(', 'ag -Q \\(')])
def test_get_new_command(script, new_cmd, output):
    assert get_new_command((Command(script, output))) == new_cmd