Back to Repositories

Testing Command History Correction Feature in TheFuck

This test suite validates the history correction functionality in TheFuck, focusing on command matching and suggestions based on command history. The tests ensure accurate detection and correction of mistyped commands by comparing them with previously executed valid commands.

Test Coverage Overview

The test suite provides comprehensive coverage of the history-based command correction feature.

Key areas tested include:
  • Command matching against historical entries
  • Validation of correct command suggestions
  • Handling of both matching and non-matching scenarios
  • Edge cases with similar command patterns

Implementation Analysis

The implementation uses pytest’s parametrize feature for efficient test case handling. The testing approach employs fixture-based mocking to isolate the history functionality and control test data.

Technical patterns include:
  • Parameterized test cases for command matching
  • Mock fixtures for history data isolation
  • Command object usage for input handling

Technical Details

Testing tools and configuration:
  • pytest framework for test organization
  • pytest.fixture for test data setup
  • mocker for dependency isolation
  • Command class from thefuck.types
  • Parameterized test cases for multiple scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Isolation of test cases using fixtures
  • Comprehensive positive and negative test scenarios
  • Clear test function naming conventions
  • Efficient test parameterization
  • Proper separation of test cases by functionality

nvbn/thefuck

tests/rules/test_history.py

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


@pytest.fixture(autouse=True)
def history_without_current(mocker):
    return mocker.patch(
        'thefuck.rules.history.get_valid_history_without_current',
        return_value=['ls cat', 'diff x'])


@pytest.mark.parametrize('script', ['ls cet', 'daff x'])
def test_match(script):
    assert match(Command(script, ''))


@pytest.mark.parametrize('script', ['apt-get', 'nocommand y'])
def test_not_match(script):
    assert not match(Command(script, ''))


@pytest.mark.parametrize('script, result', [
    ('ls cet', 'ls cat'),
    ('daff x', 'diff x')])
def test_get_new_command(script, result):
    assert get_new_command(Command(script, '')) == result