Back to Repositories

Testing Prove Command Recursive Flag Correction in thefuck

This test suite validates the prove_recursively rule functionality in the thefuck project, focusing on command correction for Perl’s prove testing tool when recursive directory testing is needed.

Test Coverage Overview

The test suite provides comprehensive coverage for the prove_recursively rule implementation, ensuring correct command transformation for Perl test execution.

  • Tests command matching logic for prove commands without recursive flag
  • Verifies directory existence checks
  • Validates command transformation accuracy
  • Covers multiple command variations and formats

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple input scenarios and expected outputs. The implementation leverages fixture-based mocking to control directory existence checks.

  • Uses pytest.mark.parametrize for data-driven testing
  • Implements mocker fixtures for filesystem operations
  • Validates both positive and negative test cases

Technical Details

  • Testing Framework: pytest
  • Mocking: pytest-mock
  • Test Types: Unit tests
  • Key Components: Command matching, command transformation
  • Mock Objects: os.path.isdir

Best Practices Demonstrated

The test suite demonstrates several testing best practices including isolation of filesystem operations and comprehensive input validation.

  • Proper test isolation through mocking
  • Parametrized test cases for better maintainability
  • Clear test case organization
  • Focused test methods with single responsibilities

nvbn/thefuck

tests/rules/test_prove_recursively.py

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


output = '''Files=0, Tests=0,  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
Result: NOTESTS'''


@pytest.fixture
def isdir(mocker):
    return mocker.patch('thefuck.rules.prove_recursively'
                        '.os.path.isdir')


@pytest.mark.parametrize('script, output', [
    ('prove -lv t', output),
    ('prove app/t', output)])
def test_match(isdir, script, output):
    isdir.return_value = True
    command = Command(script, output)
    assert match(command)


@pytest.mark.parametrize('script, output, isdir_result', [
    ('prove -lv t', output, False),
    ('prove -r t', output, True),
    ('prove --recurse t', output, True)])
def test_not_match(isdir, script, output, isdir_result):
    isdir.return_value = isdir_result
    command = Command(script, output)
    assert not match(command)


@pytest.mark.parametrize('before, after', [
    ('prove -lv t', 'prove -r -lv t'),
    ('prove t', 'prove -r t')])
def test_get_new_command(before, after):
    command = Command(before, output)
    assert get_new_command(command) == after