Back to Repositories

Testing Directory Navigation Command Correction in TheFuck

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

Test Coverage Overview

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

Key areas tested include:
  • Command matching for bare ‘cs’ command
  • Command matching with path arguments
  • Error message detection
  • Command transformation from ‘cs’ to ‘cd’

Implementation Analysis

The testing approach uses straightforward unit tests to verify both the match and command transformation functions. The implementation leverages Python’s unit testing patterns with discrete test methods for each functionality aspect.

Technical patterns include:
  • Command object usage for input handling
  • Direct assertion testing
  • Isolated function testing

Technical Details

Testing infrastructure includes:
  • Python’s built-in testing framework
  • Custom Command type for input handling
  • Mock command inputs and error messages
  • Standalone test functions for isolation

Best Practices Demonstrated

The test suite demonstrates several testing best practices including separation of concerns, clear test case isolation, and explicit test naming. Each test function focuses on a single aspect of functionality, making the tests maintainable and readable.

Notable practices:
  • Discrete test methods for match and transform operations
  • Clear test case scenarios
  • Consistent assertion patterns
  • Focused scope per test

nvbn/thefuck

tests/rules/test_cd_cs.py

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


def test_match():
    assert match(Command('cs', 'cs: command not found'))
    assert match(Command('cs /etc/', 'cs: command not found'))


def test_get_new_command():
    assert get_new_command(Command('cs /etc/', 'cs: command not found')) == 'cd /etc/'