Back to Repositories

Testing Fabric Command Correction Implementation in TheFuck

This test suite validates the fab_command_not_found rule in TheFuck project, which handles Fabric command typos and suggests corrections. The tests ensure proper command matching and replacement functionality for mistyped Fabric commands.

Test Coverage Overview

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

Key areas tested include:
  • Command matching for single and multiple typos
  • Negative test cases for non-matching scenarios
  • Command correction with various parameter combinations
  • Complex command chains with multiple arguments

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple scenarios with minimal code duplication.

Notable patterns include:
  • Structured test data using Command objects
  • Parametrized test cases for both positive and negative scenarios
  • Comprehensive validation of command parsing and correction logic

Technical Details

Testing infrastructure includes:
  • Pytest framework for test organization
  • Command class from thefuck.types for input handling
  • Mock command output strings for consistent testing
  • Parametrized test fixtures for extensive coverage

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Separation of concerns between match and command generation tests
  • Comprehensive edge case coverage
  • Clear test case organization using pytest fixtures
  • Maintainable and scalable test structure

nvbn/thefuck

tests/rules/test_fab_command_not_found.py

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

output = '''
Warning: Command(s) not found:
    extenson
    deloyp

Available commands:

    update_config
    prepare_extension
    Template               A string class for supporting $-substitutions.
    deploy
    glob                   Return a list of paths matching a pathname pattern.
    install_web
    set_version
'''


@pytest.mark.parametrize('command', [
    Command('fab extenson', output),
    Command('fab deloyp', output),
    Command('fab extenson deloyp', output)])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('gulp extenson', output),
    Command('fab deloyp', '')])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('script, result', [
    ('fab extenson', 'fab prepare_extension'),
    ('fab extenson:version=2016',
     'fab prepare_extension:version=2016'),
    ('fab extenson:version=2016 install_web set_version:val=0.5.0',
     'fab prepare_extension:version=2016 install_web set_version:val=0.5.0'),
    ('fab extenson:version=2016 deloyp:beta=true -H the.fuck',
     'fab prepare_extension:version=2016 deploy:beta=true -H the.fuck'),
])
def test_get_new_command(script, result):
    command = Command(script, output)
    assert get_new_command(command) == result