Back to Repositories

Testing Directory Navigation Error Detection in TheFuck

This test suite validates the cd_correction rule functionality in TheFuck, a command-line tool that corrects mistyped console commands. The tests specifically focus on verifying the pattern matching behavior for invalid directory navigation attempts.

Test Coverage Overview

The test suite provides comprehensive coverage of the cd_correction rule’s matching functionality.

Key areas tested include:
  • Basic directory not found scenarios
  • Nested path navigation errors
  • Different error message formats across systems
  • Edge cases with empty commands

Implementation Analysis

The implementation uses pytest’s parametrize decorator to efficiently test multiple scenarios with minimal code duplication. The testing approach separates positive and negative test cases clearly, with parametrized test functions handling different command patterns and error messages.

Notable patterns include:
  • Command object usage for input/output simulation
  • Explicit test case separation for match and non-match scenarios
  • Deliberate exclusion of filesystem-dependent tests

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type for test input structure
  • Parametrized test cases for efficient test execution
  • Integration with TheFuck’s rule matching system

Best Practices Demonstrated

The test suite exemplifies several testing best practices including separation of concerns, clear test case organization, and proper handling of environmental dependencies.

Notable practices:
  • Clear documentation of test limitations
  • Efficient use of parametrization
  • Explicit test case grouping
  • Proper handling of filesystem-dependent functionality

nvbn/thefuck

tests/rules/test_cd_correction.py

            
import pytest
from thefuck.rules.cd_correction import match
from thefuck.types import Command


@pytest.mark.parametrize('command', [
    Command('cd foo', 'cd: foo: No such file or directory'),
    Command('cd foo/bar/baz',
            'cd: foo: No such file or directory'),
    Command('cd foo/bar/baz', 'cd: can\'t cd to foo/bar/baz'),
    Command('cd /foo/bar/', 'cd: The directory "/foo/bar/" does not exist')])
def test_match(command):
    assert match(command)


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


# Note that get_new_command uses local filesystem, so not testing it here.
# Instead, see the functional test `functional.test_cd_correction`