Back to Repositories

Testing Directory Removal Command Corrections in thefuck

This test suite validates the directory removal functionality in thefuck’s rm_dir rule, focusing on handling directory deletion errors and command corrections for both local and HDFS filesystems.

Test Coverage Overview

The test suite provides comprehensive coverage for the rm_dir rule, testing both successful matches and non-matches for directory removal commands.

Key areas tested include:
  • Local filesystem directory removal errors
  • HDFS directory removal scenarios
  • Various command formats and error messages
  • Edge cases with empty commands and responses

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator to efficiently test multiple scenarios with minimal code duplication.

Testing patterns include:
  • Separate test functions for matching and non-matching cases
  • Command object usage for input/output validation
  • Consistent assertion patterns for command transformation

Technical Details

Testing infrastructure includes:
  • pytest framework for test organization
  • Command class from thefuck.types for command representation
  • Parametrized test cases for multiple scenarios
  • Mock command outputs for error simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear separation of test cases
  • Comprehensive error message validation
  • Efficient test parameterization
  • Consistent assertion patterns
  • Clean test organization and naming

nvbn/thefuck

tests/rules/test_rm_dir.py

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


@pytest.mark.parametrize('command', [
    Command('rm foo', 'rm: foo: is a directory'),
    Command('rm foo', 'rm: foo: Is a directory'),
    Command('hdfs dfs -rm foo', 'rm: `foo`: Is a directory'),
    Command('./bin/hdfs dfs -rm foo', 'rm: `foo`: Is a directory'),
])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('rm foo', ''),
    Command('hdfs dfs -rm foo', ''),
    Command('./bin/hdfs dfs -rm foo', ''),
    Command('', ''),
])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('rm foo', ''), 'rm -rf foo'),
    (Command('hdfs dfs -rm foo', ''), 'hdfs dfs -rm -r foo'),
])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command