Back to Repositories

Testing Touch Command Correction Implementation in TheFuck

This test suite validates the ‘touch’ command correction functionality in TheFuck, focusing on handling missing directory scenarios. The tests verify automatic directory creation and proper touch command execution across different operating systems.

Test Coverage Overview

The test suite provides comprehensive coverage of the touch command correction functionality.

Key areas tested include:
  • Command matching for invalid touch operations
  • BSD and non-BSD system output handling
  • Directory creation verification
  • Command correction accuracy

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple scenarios with different system outputs. The implementation follows a clear pattern of testing both matching logic and command correction separately, ensuring robust validation of the functionality.

Framework-specific features utilized:
  • pytest.mark.parametrize for test case variations
  • Command object mocking
  • Assertion-based verification

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command class for command simulation
  • System-specific output handling
  • Parameterized test cases for various scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, comprehensive edge case coverage, and clear test organization. Notable practices include:
  • Separate positive and negative test cases
  • Cross-platform compatibility testing
  • Clear test function naming
  • Efficient test parameterization

nvbn/thefuck

tests/rules/test_touch.py

            
import pytest
from thefuck.rules.touch import match, get_new_command
from thefuck.types import Command


def output(is_bsd):
    if is_bsd:
        return "touch: /a/b/c: No such file or directory"
    return "touch: cannot touch '/a/b/c': No such file or directory"


@pytest.mark.parametrize('script, is_bsd', [
    ('touch /a/b/c', False),
    ('touch /a/b/c', True)])
def test_match(script, is_bsd):
    command = Command(script, output(is_bsd))
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('touch /a/b/c', ''),
    Command('ls /a/b/c', output(False))])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('script, is_bsd', [
    ('touch /a/b/c', False),
    ('touch /a/b/c', True)])
def test_get_new_command(script, is_bsd):
    command = Command(script, output(is_bsd))
    fixed_command = get_new_command(command)
    assert fixed_command == 'mkdir -p /a/b && touch /a/b/c'