Back to Repositories

Testing Git Branch Deletion Error Handling in TheFuck

This test suite validates the git branch deletion functionality in TheFuck tool, specifically handling cases where a branch cannot be deleted because it is currently checked out. The tests ensure proper error detection and command correction behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of git branch deletion scenarios, focusing on error handling when attempting to delete a checked-out branch.

Key areas tested include:
  • Branch deletion command matching (-d and -D flags)
  • Error message pattern recognition
  • Command correction generation
  • Edge case handling for successful deletion scenarios

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature to efficiently test multiple command variations with minimal code duplication. The implementation leverages pytest fixtures for consistent test data and employs command pattern matching to verify both error detection and command correction logic.

Technical patterns include:
  • Fixture-based test data management
  • Parametrized test cases
  • Command object pattern testing

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type implementation
  • Parametrized test decorators
  • Fixture-based test data
  • Boolean assertion patterns

Best Practices Demonstrated

The test suite demonstrates several testing best practices, including isolation of test cases, clear arrangement of test scenarios, and comprehensive coverage of both positive and negative cases.

Notable practices:
  • Separation of match and command generation tests
  • Clear test naming conventions
  • Efficient test data reuse
  • Comprehensive error case coverage

nvbn/thefuck

tests/rules/test_git_branch_delete_checked_out.py

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


@pytest.fixture
def output():
    return "error: Cannot delete branch 'foo' checked out at '/bar/foo'"


@pytest.mark.parametrize("script", ["git branch -d foo", "git branch -D foo"])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize("script", ["git branch -d foo", "git branch -D foo"])
def test_not_match(script):
    assert not match(Command(script, "Deleted branch foo (was a1b2c3d)."))


@pytest.mark.parametrize(
    "script, new_command",
    [
        ("git branch -d foo", "git checkout master && git branch -D foo"),
        ("git branch -D foo", "git checkout master && git branch -D foo"),
    ],
)
def test_get_new_command(script, new_command, output):
    assert get_new_command(Command(script, output)) == new_command