Back to Repositories

Testing CD Parent Command Correction in TheFuck

This test suite validates the functionality of the ‘cd_parent’ rule in TheFuck, which handles the common typo of typing ‘cd..’ instead of ‘cd ..’. The tests ensure proper command correction and error handling for directory navigation commands.

Test Coverage Overview

The test suite provides comprehensive coverage for the cd_parent rule functionality.

Key areas tested include:
  • Command pattern matching for ‘cd..’ syntax
  • Error message validation
  • Command transformation accuracy
Edge cases covered include empty command handling and proper space insertion between ‘cd’ and ‘..’.

Implementation Analysis

The testing approach utilizes pytest’s assertion-based testing pattern to verify both the matching logic and command transformation.

Technical implementation details:
  • Direct function testing of match() and get_new_command()
  • Command object usage for input simulation
  • Boolean assertion patterns for match validation

Technical Details

Testing infrastructure includes:
  • Python unittest framework
  • Custom Command type from thefuck.types
  • Direct module imports for isolated testing
  • Minimal test fixtures and setup requirements

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable implementations include:
  • Isolated test functions for specific behaviors
  • Clear test case separation for matching and transformation
  • Explicit assertion messages
  • Efficient test organization with focused scope

nvbn/thefuck

tests/rules/test_cd_parent.py

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


def test_match():
    assert match(Command('cd..', 'cd..: command not found'))
    assert not match(Command('', ''))


def test_get_new_command():
    assert get_new_command(Command('cd..', '')) == 'cd ..'