Back to Repositories

Testing Git Branch Flag Correction Implementation in thefuck

A comprehensive test suite for validating Git branch flag handling in the thefuck project, focusing on correcting mistyped branch commands with leading zeros. The tests verify the automatic correction of common Git branch command mistakes where users accidentally prefix flags with ‘0’.

Test Coverage Overview

The test suite provides extensive coverage of Git branch command corrections, specifically targeting scenarios where users mistakenly use ‘0’ instead of ‘-‘ for flags. Key test cases include:
  • Branch creation and deletion commands (0a, 0d, 0D)
  • Branch viewing and listing operations (0v, 0l)
  • Branch upstream configurations (0u)
  • Different error scenarios and outputs

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for comprehensive test case coverage. The implementation follows a fixture-based pattern with two main error scenarios:
  • Existing branch conflicts
  • Invalid object name errors
Each test case validates both the command matching logic and the correct command transformation.

Technical Details

Testing infrastructure includes:
  • pytest as the primary testing framework
  • Command object mocking for Git operations
  • Parametrized test cases for multiple command variations
  • Fixture-based test data management
  • Assertion-based verification of command matching and transformation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Comprehensive error case coverage
  • Efficient test case organization using pytest parametrize
  • Clear separation of test data using fixtures
  • Explicit test case naming and organization
  • Focused unit tests for specific functionality

nvbn/thefuck

tests/rules/test_git_branch_0flag.py

            
import pytest

from thefuck.rules.git_branch_0flag import get_new_command, match
from thefuck.types import Command


@pytest.fixture
def output_branch_exists():
    return "fatal: A branch named 'bar' already exists."


@pytest.mark.parametrize(
    "script",
    [
        "git branch 0a",
        "git branch 0d",
        "git branch 0f",
        "git branch 0r",
        "git branch 0v",
        "git branch 0d foo",
        "git branch 0D foo",
    ],
)
def test_match(script, output_branch_exists):
    assert match(Command(script, output_branch_exists))


@pytest.mark.parametrize(
    "script",
    [
        "git branch -a",
        "git branch -r",
        "git branch -v",
        "git branch -d foo",
        "git branch -D foo",
    ],
)
def test_not_match(script, output_branch_exists):
    assert not match(Command(script, ""))


@pytest.mark.parametrize(
    "script, new_command",
    [
        ("git branch 0a", "git branch -D 0a && git branch -a"),
        ("git branch 0v", "git branch -D 0v && git branch -v"),
        ("git branch 0d foo", "git branch -D 0d && git branch -d foo"),
        ("git branch 0D foo", "git branch -D 0D && git branch -D foo"),
        ("git branch 0l 'maint-*'", "git branch -D 0l && git branch -l 'maint-*'"),
        ("git branch 0u upstream", "git branch -D 0u && git branch -u upstream"),
    ],
)
def test_get_new_command_branch_exists(script, output_branch_exists, new_command):
    assert get_new_command(Command(script, output_branch_exists)) == new_command


@pytest.fixture
def output_not_valid_object():
    return "fatal: Not a valid object name: 'bar'."


@pytest.mark.parametrize(
    "script, new_command",
    [
        ("git branch 0l 'maint-*'", "git branch -l 'maint-*'"),
        ("git branch 0u upstream", "git branch -u upstream"),
    ],
)
def test_get_new_command_not_valid_object(script, output_not_valid_object, new_command):
    assert get_new_command(Command(script, output_not_valid_object)) == new_command