Back to Repositories

Testing Recursive Grep Command Transformation in thefuck

This test suite validates the recursive grep functionality in thefuck, specifically testing the behavior of grep commands when executed on directories. The tests ensure proper handling of both ASCII and Unicode input patterns, along with appropriate command transformation to include the recursive flag.

Test Coverage Overview

The test suite provides comprehensive coverage of the grep_recursive rule functionality.

Key areas tested include:
  • Directory-based grep command detection
  • Unicode character pattern handling
  • Command transformation with recursive flag
Edge cases covered include empty commands and non-matching scenarios.

Implementation Analysis

The testing approach utilizes pytest’s fixture-based testing pattern with the Command type from thefuck.types. The implementation focuses on two main functions: match() for identifying applicable scenarios and get_new_command() for generating the corrected command.

Technical patterns include:
  • Command object usage for input/output handling
  • Unicode string testing
  • Error message pattern matching

Technical Details

Testing tools and configuration:
  • pytest as the testing framework
  • Custom Command type for input structuring
  • UTF-8 encoding specification
  • Direct function unit testing without mocking

Best Practices Demonstrated

The test suite demonstrates several testing best practices including isolation of test cases, clear test function naming, and comprehensive error case coverage. Notable practices include:
  • Separate positive and negative test cases
  • Unicode handling verification
  • Explicit test case documentation
  • Focused unit test scope

nvbn/thefuck

tests/rules/test_grep_recursive.py

            
# -*- coding: utf-8 -*-

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


def test_match():
    assert match(Command('grep blah .', 'grep: .: Is a directory'))
    assert match(Command(u'grep café .', 'grep: .: Is a directory'))
    assert not match(Command('', ''))


def test_get_new_command():
    assert get_new_command(Command('grep blah .', '')) == 'grep -r blah .'
    assert get_new_command(Command(u'grep café .', '')) == u'grep -r café .'