Back to Repositories

Testing Directory Creation and Navigation Commands in thefuck

This test suite validates the cd_mkdir functionality in The Fuck tool, which automatically creates directories when attempting to cd into non-existent paths. The tests ensure proper handling of directory creation and navigation commands.

Test Coverage Overview

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

Key areas tested include:
  • Directory navigation failures detection
  • Various error message formats across different systems
  • Nested directory path handling
  • Command matching logic for valid and invalid scenarios

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple input scenarios. The implementation follows a clear pattern of testing both positive and negative cases for command matching and new command generation.

Technical implementation details:
  • Parametrized test cases for different error messages
  • Separate test functions for matching and non-matching scenarios
  • Command object usage for input/output validation

Technical Details

Testing infrastructure includes:
  • Pytest as the primary testing framework
  • Custom Command type for test data structuring
  • Parametrized test decorators for multiple test cases
  • Mock command inputs and error messages

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear separation of test cases for different functionalities
  • Comprehensive error message pattern testing
  • Efficient test case organization using parametrize
  • Explicit assertion statements for clarity

nvbn/thefuck

tests/rules/test_cd_mkdir.py

            
import pytest
from thefuck.rules.cd_mkdir import match, get_new_command
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)


@pytest.mark.parametrize('command, new_command', [
    (Command('cd foo', ''), 'mkdir -p foo && cd foo'),
    (Command('cd foo/bar/baz', ''), 'mkdir -p foo/bar/baz && cd foo/bar/baz')])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command