Back to Repositories

Testing Root Directory Removal Safety Controls in TheFuck

This test suite validates the functionality of the rm_root rule in TheFuck, which handles dangerous root directory removal commands. It ensures proper handling of the –no-preserve-root flag when attempting to delete the root directory.

Test Coverage Overview

The test suite provides comprehensive coverage for the rm_root rule functionality, focusing on command pattern matching and transformation.

Key areas tested include:
  • Matching rm -rf / commands that require –no-preserve-root
  • Proper rejection of non-matching commands
  • Correct command transformation with safety flag
Edge cases cover various command formats and invalid inputs to ensure robust handling.

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management. The implementation follows a clear pattern of testing both positive and negative scenarios for command matching.

Technical implementation details:
  • Command object usage for input handling
  • Parametrized test cases for multiple scenarios
  • Separate match and command generation testing

Technical Details

Testing infrastructure includes:
  • Pytest framework for test execution
  • Command class from thefuck.types for command representation
  • Parametrize decorator for test case variation
  • Direct imports from the rm_root rule module

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Clear separation of concerns between matching and command generation
  • Comprehensive negative testing scenarios
  • Efficient test case organization using pytest features
  • Explicit assertion statements for clear test intentions

nvbn/thefuck

tests/rules/test_rm_root.py

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


def test_match():
    assert match(Command('rm -rf /', 'add --no-preserve-root'))


@pytest.mark.parametrize('command', [
    Command('ls', 'add --no-preserve-root'),
    Command('rm --no-preserve-root /', 'add --no-preserve-root'),
    Command('rm -rf /', '')])
def test_not_match(command):
    assert not match(command)


def test_get_new_command():
    assert (get_new_command(Command('rm -rf /', ''))
            == 'rm -rf / --no-preserve-root')