Back to Repositories

Testing Path Resolution from Shell History in TheFuck

This test suite validates the path_from_history rule functionality in TheFuck project, focusing on command path resolution from shell history. It ensures the rule can correctly suggest commands based on previously used paths and directories.

Test Coverage Overview

The test suite provides comprehensive coverage for path resolution from command history, testing both matching and command generation functionality.

  • Tests path existence validation
  • Covers command matching for ‘ls’ and ‘cd’ operations
  • Validates history-based path suggestions
  • Tests negative scenarios for non-matching cases

Implementation Analysis

The implementation uses pytest fixtures to mock history retrieval and path existence checking.

Key patterns include:
  • Parametrized test cases for different command scenarios
  • Mocked filesystem interactions
  • Isolated test cases for match and command generation logic

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest.fixture for test setup and dependency injection
  • mocker for mocking filesystem and history operations
  • parametrize decorator for multiple test scenarios

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Clear separation of test scenarios
  • Effective use of fixtures for setup
  • Comprehensive positive and negative test cases
  • Isolated component testing
  • Maintainable parametrized tests

nvbn/thefuck

tests/rules/test_path_from_history.py

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


@pytest.fixture(autouse=True)
def history(mocker):
    return mocker.patch(
        'thefuck.rules.path_from_history.get_valid_history_without_current',
        return_value=['cd /opt/java', 'ls ~/work/project/'])


@pytest.fixture(autouse=True)
def path_exists(mocker):
    path_mock = mocker.patch('thefuck.rules.path_from_history.Path')
    exists_mock = path_mock.return_value.expanduser.return_value.exists
    exists_mock.return_value = True
    return exists_mock


@pytest.mark.parametrize('script, output', [
    ('ls project', 'no such file or directory: project'),
    ('cd project', "can't cd to project"),
])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, output', [
    ('myapp cats', 'no such file or directory: project'),
    ('cd project', ""),
])
def test_not_match(script, output):
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, output, result', [
    ('ls project', 'no such file or directory: project', 'ls ~/work/project'),
    ('cd java', "can't cd to java", 'cd /opt/java'),
])
def test_get_new_command(script, output, result):
    new_command = get_new_command(Command(script, output))
    assert new_command[0] == result