Back to Repositories

Testing PHP Server Flag Correction in TheFuck

This test suite validates the PHP built-in server command correction functionality in TheFuck, focusing on the automatic conversion of lowercase ‘-s’ to uppercase ‘-S’ flag. The tests ensure proper handling of PHP’s development server initialization commands.

Test Coverage Overview

The test suite provides comprehensive coverage for PHP server command corrections, specifically targeting the common mistake of using lowercase ‘-s’ instead of the required uppercase ‘-S’ flag. Key test scenarios include:

  • Basic server initialization with host and port
  • Server initialization with document root specification
  • Invalid command patterns that should not trigger corrections

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator to implement data-driven tests, allowing multiple test cases to be executed with different input parameters. The implementation follows a clear pattern of testing both matching conditions and command transformations separately.

The tests validate two main functions: match() for identifying applicable commands and get_new_command() for generating corrected commands.

Technical Details

Testing tools and configuration:

  • pytest framework for test execution
  • Command class from thefuck.types for command representation
  • Parametrized test cases for efficient test case management
  • Mock command inputs with empty stderr outputs

Best Practices Demonstrated

The test suite demonstrates several testing best practices including:

  • Separation of concerns between matching and command generation
  • Comprehensive positive and negative test cases
  • Clear test case organization using pytest fixtures
  • Efficient test case management through parametrization

nvbn/thefuck

tests/rules/test_php_s.py

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


@pytest.mark.parametrize('command', [
    Command('php -s localhost:8000', ''),
    Command('php -t pub -s 0.0.0.0:8080', '')
])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('php -S localhost:8000', ''),
    Command('vim php -s', '')
])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('php -s localhost:8000', ''), 'php -S localhost:8000'),
    (Command('php -t pub -s 0.0.0.0:8080', ''), 'php -t pub -S 0.0.0.0:8080')
])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command