Back to Repositories

Testing Symbolic Link Command Correction in thefuck

This test suite validates the functionality of symbolic link (ln -s) command order correction in the ‘thefuck’ utility. It ensures proper handling of source and destination arguments when creating symbolic links, focusing on error detection and command reordering.

Test Coverage Overview

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

Key areas tested include:
  • Validation of incorrect symbolic link command patterns
  • Verification of file existence scenarios
  • Testing of various argument order combinations
  • Error message handling for existing files

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management. The implementation leverages fixture-based mocking to simulate file system conditions and validates both matching logic and command correction functionality.

Technical patterns include:
  • Parametrized test cases for multiple input scenarios
  • Fixture-based file existence mocking
  • Command object validation

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest-mock for mocking file system operations
  • Custom Command type implementation
  • Parametrized test decorators for test case management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Separation of test cases for matching and command generation
  • Effective use of fixtures for dependency injection
  • Comprehensive edge case coverage
  • Clear test case organization using pytest decorators
  • Isolated test scenarios with proper mocking

nvbn/thefuck

tests/rules/test_ln_s_order.py

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


@pytest.fixture
def file_exists(mocker):
    return mocker.patch('os.path.exists', return_value=True)


get_output = "ln: failed to create symbolic link '{}': File exists".format


@pytest.mark.parametrize('script, output, exists', [
    ('ln dest source', get_output('source'), True),
    ('ls -s dest source', get_output('source'), True),
    ('ln -s dest source', '', True),
    ('ln -s dest source', get_output('source'), False)])
def test_not_match(file_exists, script, output, exists):
    file_exists.return_value = exists
    assert not match(Command(script, output))


@pytest.mark.usefixtures('file_exists')
@pytest.mark.parametrize('script, result', [
    ('ln -s dest source', 'ln -s source dest'),
    ('ln dest -s source', 'ln -s source dest'),
    ('ln dest source -s', 'ln source -s dest')])
def test_match(script, result):
    output = get_output('source')
    assert match(Command(script, output))
    assert get_new_command(Command(script, output)) == result