Back to Repositories

Testing Executable Permission Correction Rules in thefuck

This test suite validates the chmod_x rule functionality in thefuck project, which handles permission-related command corrections. It verifies the automatic fixing of executable permission issues when running scripts.

Test Coverage Overview

The test suite provides comprehensive coverage for the chmod_x rule, focusing on permission denied scenarios.

  • Tests permission denied errors for common script files like gradlew and install.sh
  • Validates both matching and non-matching cases
  • Covers file existence and access permission combinations

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for data-driven testing, enabling multiple test scenarios with minimal code duplication.

  • Uses pytest fixtures for mocking file system operations
  • Implements systematic test organization with separate match and non-match cases
  • Employs command transformation validation

Technical Details

  • Pytest framework with fixture decorators
  • Mock objects for file system operations (os.path.exists, os.access)
  • Command type from thefuck.types
  • Parametrized test cases for varied inputs

Best Practices Demonstrated

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

  • Clear separation of test cases using parametrize
  • Proper mocking of system calls
  • Isolated test fixtures
  • Comprehensive edge case coverage

nvbn/thefuck

tests/rules/test_chmod_x.py

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


@pytest.fixture
def file_exists(mocker):
    return mocker.patch('os.path.exists', return_value=True)


@pytest.fixture
def file_access(mocker):
    return mocker.patch('os.access', return_value=False)


@pytest.mark.usefixtures('file_exists', 'file_access')
@pytest.mark.parametrize('script, output', [
    ('./gradlew build', 'gradlew: Permission denied'),
    ('./install.sh --help', 'install.sh: permission denied')])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, output, exists, callable', [
    ('./gradlew build', 'gradlew: Permission denied', True, True),
    ('./gradlew build', 'gradlew: Permission denied', False, False),
    ('./gradlew build', 'gradlew: error', True, False),
    ('gradlew build', 'gradlew: Permission denied', True, False)])
def test_not_match(file_exists, file_access, script, output, exists, callable):
    file_exists.return_value = exists
    file_access.return_value = callable
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, result', [
    ('./gradlew build', 'chmod +x gradlew && ./gradlew build'),
    ('./install.sh --help', 'chmod +x install.sh && ./install.sh --help')])
def test_get_new_command(script, result):
    assert get_new_command(Command(script, '')) == result