Back to Repositories

Testing Duplicate Command Detection Rule in TheFuck

This test suite validates the ‘dry’ rule functionality in TheFuck project, which handles duplicate word detection and correction in shell commands. The tests ensure proper identification and resolution of repeated command segments like ‘cd cd’ or ‘git git’.

Test Coverage Overview

The test suite provides comprehensive coverage for the dry rule command correction functionality.

Key areas tested include:
  • Detection of duplicate command words
  • Command correction by removing redundant terms
  • Multiple command pattern validation (cd, git)
Edge cases cover different command types and structures, ensuring robust duplicate detection.

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator for data-driven testing, enabling efficient validation of multiple test cases.

Key implementation patterns include:
  • Separate test functions for matching and command generation
  • Parametrized test cases for different command scenarios
  • Command object usage for input/output validation

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • Custom Command type implementation
  • Parametrized test decorators
  • Direct assertion validation
  • Mock command input/output handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear separation of matching and command generation tests
  • Efficient use of parametrized testing
  • Consistent test case organization
  • Strong type handling with Command objects
  • Focused unit test scope

nvbn/thefuck

tests/rules/test_dry.py

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


@pytest.mark.parametrize('command', [
    Command('cd cd foo', ''),
    Command('git git push origin/master', '')])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('cd cd foo', ''), 'cd foo'),
    (Command('git git push origin/master', ''), 'git push origin/master')])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command