Back to Repositories

Testing Command Space Correction Logic in TheFuck

This test suite validates the missing space before subcommand functionality in TheFuck, a command-line tool that corrects mistyped console commands. It ensures proper handling of command parsing and space insertion between command and subcommand.

Test Coverage Overview

The test suite provides comprehensive coverage for the missing space before subcommand rule.

Key areas tested include:
  • Command pattern matching for various CLI tools (git, ls, npm, watch)
  • Validation of correct space insertion between commands and subcommands
  • Edge cases with different command combinations
  • Integration with the command parsing system

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management.

Technical implementation includes:
  • Fixture-based executable mocking
  • Parametrized test cases for both matching and non-matching scenarios
  • Command object validation
  • Direct testing of command transformation logic

Technical Details

Testing infrastructure includes:
  • Pytest as the primary testing framework
  • Mocker fixture for dependency isolation
  • Command type from thefuck.types
  • Parametrized test decorators for multiple test cases
  • Automated fixture setup with autouse=True

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable implementations include:
  • Separation of matching and transformation logic tests
  • Comprehensive positive and negative test cases
  • Clean fixture management
  • Efficient test parameterization
  • Clear test case organization

nvbn/thefuck

tests/rules/test_missing_space_before_subcommand.py

            
import pytest
from thefuck.rules.missing_space_before_subcommand import (
    match, get_new_command)
from thefuck.types import Command


@pytest.fixture(autouse=True)
def all_executables(mocker):
    return mocker.patch(
        'thefuck.rules.missing_space_before_subcommand.get_all_executables',
        return_value=['git', 'ls', 'npm', 'w', 'watch'])


@pytest.mark.parametrize('script', [
    'gitbranch', 'ls-la', 'npminstall', 'watchls'])
def test_match(script):
    assert match(Command(script, ''))


@pytest.mark.parametrize('script', ['git branch', 'vimfile'])
def test_not_match(script):
    assert not match(Command(script, ''))


@pytest.mark.parametrize('script, result', [
    ('gitbranch', 'git branch'),
    ('ls-la', 'ls -la'),
    ('npminstall webpack', 'npm install webpack'),
    ('watchls', 'watch ls')])
def test_get_new_command(script, result):
    assert get_new_command(Command(script, '')) == result