Back to Repositories

Testing Grep Argument Order Correction in TheFuck

This test suite validates the functionality of grep argument order correction in TheFuck, a command-line tool that fixes mistyped console commands. It ensures proper handling of grep pattern and file arguments when users accidentally swap their positions.

Test Coverage Overview

The test suite provides comprehensive coverage of grep and egrep command argument order scenarios.

Key areas tested include:
  • Basic grep pattern and file argument swapping
  • Complex grep commands with multiple options (-lir)
  • Edge cases with different file paths and patterns
  • Integration with both grep and egrep variants

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple input scenarios with minimal code duplication.

Technical implementation details:
  • Fixture-based path validation mocking
  • Parametrized test cases for match and command generation
  • Structured Command object usage for input handling

Technical Details

Testing infrastructure includes:
  • pytest as the primary testing framework
  • Monkeypatch fixture for file system operations
  • Custom Command type for input processing
  • Automated test fixtures for environment setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Comprehensive positive and negative test cases
  • Clean separation of test scenarios
  • Efficient use of pytest fixtures and parametrization
  • Clear test function naming and organization

nvbn/thefuck

tests/rules/test_grep_arguments_order.py

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

output = 'grep: {}: No such file or directory'.format


@pytest.fixture(autouse=True)
def os_path(monkeypatch):
    monkeypatch.setattr('os.path.isfile', lambda x: not x.startswith('-'))


@pytest.mark.parametrize('script, file', [
    ('grep test.py test', 'test'),
    ('grep -lir . test', 'test'),
    ('egrep test.py test', 'test'),
    ('egrep -lir . test', 'test')])
def test_match(script, file):
    assert match(Command(script, output(file)))


@pytest.mark.parametrize('script, output', [
    ('cat test.py', output('test')),
    ('grep test test.py', ''),
    ('grep -lir test .', ''),
    ('egrep test test.py', ''),
    ('egrep -lir test .', '')])
def test_not_match(script, output):
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, output, result', [
    ('grep test.py test', output('test'), 'grep test test.py'),
    ('grep -lir . test', output('test'), 'grep -lir test .'),
    ('grep . test -lir', output('test'), 'grep test -lir .'),
    ('egrep test.py test', output('test'), 'egrep test test.py'),
    ('egrep -lir . test', output('test'), 'egrep -lir test .'),
    ('egrep . test -lir', output('test'), 'egrep test -lir .')])
def test_get_new_command(script, output, result):
    assert get_new_command(Command(script, output)) == result