Back to Repositories

Testing Command Retrieval Implementation in TheFuck

This test suite validates the command retrieval functionality in TheFuck’s fix_command module, specifically focusing on the _get_raw_command implementation. The tests ensure proper command extraction from different sources including force commands, command arguments, and command history.

Test Coverage Overview

The test suite provides comprehensive coverage of the _get_raw_command function with multiple test cases.

Key areas tested include:
  • Force command argument handling
  • Standard command argument processing
  • Command history extraction with various formats
  • Environment variable integration (TF_HISTORY)

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient testing of multiple history scenarios. The implementation employs Mock objects for argument simulation and environment variable control.

Notable patterns include:
  • Class-based test organization
  • Parametrized test cases for history parsing
  • Environment variable mocking
  • Isolated test methods for each command source

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • Mock library for object simulation
  • os_environ fixture for environment manipulation
  • Parametrize decorator for multiple test cases
  • Class-based test organization with TestGetRawCommand

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Isolation of test cases
  • Clear test method naming
  • Efficient use of pytest features
  • Comprehensive edge case coverage
  • Proper mocking of dependencies
  • Organized test structure

nvbn/thefuck

tests/entrypoints/test_fix_command.py

            
import pytest
from mock import Mock
from thefuck.entrypoints.fix_command import _get_raw_command


class TestGetRawCommand(object):
    def test_from_force_command_argument(self):
        known_args = Mock(force_command='git brunch')
        assert _get_raw_command(known_args) == ['git brunch']

    def test_from_command_argument(self, os_environ):
        os_environ['TF_HISTORY'] = None
        known_args = Mock(force_command=None,
                          command=['sl'])
        assert _get_raw_command(known_args) == ['sl']

    @pytest.mark.parametrize('history, result', [
        ('git br', 'git br'),
        ('git br
fcuk', 'git br'),
        ('git br
fcuk
ls', 'ls'),
        ('git br
fcuk
ls
fuk', 'ls')])
    def test_from_history(self, os_environ, history, result):
        os_environ['TF_HISTORY'] = history
        known_args = Mock(force_command=None,
                          command=None)
        assert _get_raw_command(known_args) == [result]