Back to Repositories

Testing Alt+Space Character Correction in TheFuck Command-Line Tool

This test suite validates the handling of Alt+Space character issues in command-line inputs, specifically focusing on Mac-related typing scenarios where Alt+Space can inadvertently replace regular spaces.

Test Coverage Overview

The test suite covers the detection and correction of Alt+Space characters in shell commands, with particular focus on the common Mac keyboard issue when using pipe operations.

Key functionality tested includes:
  • Detection of Alt+Space before ‘grep’ command
  • Verification of error message matching
  • Command string transformation
Edge cases include empty commands and valid space characters.

Implementation Analysis

The testing approach uses Python’s unit testing framework to validate the fix_alt_space rule implementation. The tests verify both the match() and get_new_command() functions using the Command type from thefuck.types.

Testing patterns include:
  • Unicode string handling
  • Command object manipulation
  • Error message validation

Technical Details

Testing tools and configuration:
  • Python unittest framework
  • Unicode string encoding (utf-8)
  • Custom Command type implementation
  • Assertion-based validation
  • Mock shell command inputs and outputs

Best Practices Demonstrated

The test suite demonstrates several testing best practices including isolated test cases, clear test documentation, and comprehensive error scenario coverage.

Notable practices:
  • Descriptive docstrings explaining test scenarios
  • Separate positive and negative test cases
  • Platform-specific issue consideration
  • Clean command string handling

nvbn/thefuck

tests/rules/test_fix_alt_space.py

            
# -*- encoding: utf-8 -*-

from thefuck.rules.fix_alt_space import match, get_new_command
from thefuck.types import Command


def test_match():
    """The character before 'grep' is Alt+Space, which happens frequently
    on the Mac when typing the pipe character (Alt+7), and holding the Alt
    key pressed for longer than necessary.

    """
    assert match(Command(u'ps -ef | grep foo',
                         u'-bash:  grep: command not found'))
    assert not match(Command('ps -ef | grep foo', ''))
    assert not match(Command('', ''))


def test_get_new_command():
    """ Replace the Alt+Space character by a simple space """
    assert (get_new_command(Command(u'ps -ef | grep foo', ''))
            == 'ps -ef | grep foo')