Back to Repositories

Testing Sed Command Correction Implementation in TheFuck

This test suite validates the functionality of the sed_unterminated_s rule in TheFuck, which handles unterminated sed substitution commands. The tests ensure proper detection and correction of missing trailing slashes in sed expressions.

Test Coverage Overview

The test suite provides comprehensive coverage of the sed_unterminated_s rule functionality.

Key areas tested include:
  • Matching unterminated sed substitution patterns
  • Handling single and multiple sed expressions
  • Processing escaped forward slashes
  • Various command formats (-e and -es flags)

Implementation Analysis

The testing approach uses pytest fixtures to simulate sed error messages and validate command corrections. The implementation follows a two-function pattern:

  • match() – Validates error detection
  • get_new_command() – Verifies correct command transformation
Tests utilize pytest’s assertion framework for validation.

Technical Details

Testing infrastructure includes:
  • pytest framework for test organization
  • Command class from thefuck.types for command representation
  • Fixture-based test data management
  • Multiple assertion patterns for varied test cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear purpose
  • Comprehensive positive and negative test scenarios
  • Efficient test data management through fixtures
  • Clear separation of matching and command generation logic

nvbn/thefuck

tests/rules/test_sed_unterminated_s.py

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


@pytest.fixture
def sed_unterminated_s():
    return "sed: -e expression #1, char 9: unterminated `s' command"


def test_match(sed_unterminated_s):
    assert match(Command('sed -e s/foo/bar', sed_unterminated_s))
    assert match(Command('sed -es/foo/bar', sed_unterminated_s))
    assert match(Command('sed -e s/foo/bar -e s/baz/quz', sed_unterminated_s))
    assert not match(Command('sed -e s/foo/bar', ''))
    assert not match(Command('sed -es/foo/bar', ''))
    assert not match(Command('sed -e s/foo/bar -e s/baz/quz', ''))


def test_get_new_command(sed_unterminated_s):
    assert (get_new_command(Command('sed -e s/foo/bar', sed_unterminated_s))
            == 'sed -e s/foo/bar/')
    assert (get_new_command(Command('sed -es/foo/bar', sed_unterminated_s))
            == 'sed -es/foo/bar/')
    assert (get_new_command(Command(r"sed -e 's/\/foo/bar'", sed_unterminated_s))
            == r"sed -e 's/\/foo/bar/'")
    assert (get_new_command(Command(r"sed -e s/foo/bar -es/baz/quz", sed_unterminated_s))
            == r"sed -e s/foo/bar/ -es/baz/quz/")