Back to Repositories

Testing APT Command Correction Logic in TheFuck

This test suite validates the functionality of the apt-get search command correction feature in TheFuck, a command-line tool that corrects mistyped console commands. The tests ensure proper handling of the common mistake of using ‘apt-get search’ instead of the correct ‘apt-cache search’ command.

Test Coverage Overview

The test suite provides comprehensive coverage of the apt-get search command correction functionality.

Key areas tested include:
  • Positive matching of incorrect ‘apt-get search’ commands
  • Negative matching of valid apt-related commands
  • Command transformation logic
Edge cases are handled through parametrized tests of various apt commands to ensure proper discrimination between valid and invalid usage patterns.

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management. The implementation follows a clear pattern of separating matching logic from command transformation, with distinct test functions for each aspect.

The tests leverage pytest fixtures and decorators to organize multiple test cases, demonstrating effective use of the framework’s capabilities for handling related test scenarios.

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type for input handling
  • Parametrized test decorators for multiple test cases
  • Direct assertion patterns for verification
  • Modular test function organization

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing.

Notable practices include:
  • Clear separation of concerns between match and transformation tests
  • Efficient use of parametrized testing for multiple cases
  • Consistent assertion patterns
  • Clean and maintainable test structure
  • Focused test functions with single responsibilities

nvbn/thefuck

tests/rules/test_apt_get_search.py

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


def test_match():
    assert match(Command('apt-get search foo', ''))


@pytest.mark.parametrize('command', [
    Command('apt-cache search foo', ''),
    Command('aptitude search foo', ''),
    Command('apt search foo', ''),
    Command('apt-get install foo', ''),
    Command('apt-get source foo', ''),
    Command('apt-get clean', ''),
    Command('apt-get remove', ''),
    Command('apt-get update', '')
])
def test_not_match(command):
    assert not match(command)


def test_get_new_command():
    new_command = get_new_command(Command('apt-get search foo', ''))
    assert new_command == 'apt-cache search foo'