Back to Repositories

Testing Sudo Command Path Resolution in TheFuck

This test suite validates the functionality of the sudo command path resolution in TheFuck, focusing on handling commands not found in root’s PATH but available in the user’s PATH. The tests ensure proper command execution with environment path preservation.

Test Coverage Overview

The test suite provides comprehensive coverage for sudo command path resolution scenarios:

  • Tests command matching for npm and appcfg installations
  • Validates path resolution for sudo commands not found in root PATH
  • Covers edge cases with different command outputs and path configurations
  • Tests integration with system PATH environment variables

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management. The implementation leverages pytest fixtures for mocking the ‘which’ command functionality, enabling controlled testing of path resolution scenarios.

  • Uses pytest.mark.parametrize for multiple test cases
  • Implements fixture-based mocking strategy
  • Tests both positive and negative matching scenarios

Technical Details

  • Testing Framework: pytest
  • Mocking Library: pytest-mock
  • Test Components: Command matching, path resolution, command transformation
  • Configuration: Automated fixture setup for path resolution

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing:

  • Separation of concerns between matching and command generation
  • Comprehensive fixture usage for dependency injection
  • Parametrized testing for multiple scenarios
  • Clear test case organization and naming

nvbn/thefuck

tests/rules/test_sudo_command_from_user_path.py

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


output = 'sudo: {}: command not found'


@pytest.fixture(autouse=True)
def which(mocker):
    return mocker.patch('thefuck.rules.sudo_command_from_user_path.which',
                        return_value='/usr/bin/app')


@pytest.mark.parametrize('script, output', [
    ('sudo npm install -g react-native-cli', output.format('npm')),
    ('sudo -u app appcfg update .', output.format('appcfg'))])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, output, which_result', [
    ('npm --version', output.format('npm'), '/usr/bin/npm'),
    ('sudo npm --version', '', '/usr/bin/npm'),
    ('sudo npm --version', output.format('npm'), None)])
def test_not_match(which, script, output, which_result):
    which.return_value = which_result
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, output, result', [
    ('sudo npm install -g react-native-cli',
     output.format('npm'),
     'sudo env "PATH=$PATH" npm install -g react-native-cli'),
    ('sudo -u app appcfg update .',
     output.format('appcfg'),
     'sudo -u app env "PATH=$PATH" appcfg update .')])
def test_get_new_command(script, output, result):
    assert get_new_command(Command(script, output)) == result