Back to Repositories

Testing Git Branch Name Conversion Implementation in thefuck

A comprehensive test suite for validating git branch name conversion between ‘main’ and ‘master’ in the thefuck command-line tool. The tests ensure correct handling of branch name switching and error scenarios.

Test Coverage Overview

The test suite provides thorough coverage of git branch name handling between ‘main’ and ‘master’ branches.

Key areas tested include:
  • Branch name matching for ‘main’ and ‘master’
  • Command pattern recognition for checkout and show operations
  • Error handling for non-existent branches
  • Edge cases with empty branch names and invalid inputs

Implementation Analysis

The implementation uses pytest’s parametrize feature for comprehensive test case coverage. The testing approach focuses on two main functions: match() for command pattern recognition and get_new_command() for branch name conversion logic.

Notable patterns include:
  • Fixture-based output generation
  • Parameterized test cases for multiple scenarios
  • Command object usage for input handling

Technical Details

Testing tools and configuration:
  • pytest framework for test organization
  • pytest.fixture for test data setup
  • pytest.mark.parametrize for test case variations
  • Command type from thefuck.types for input handling
  • Unicode string handling for error messages

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing.

Notable practices include:
  • Separation of positive and negative test cases
  • Clear test function naming conventions
  • Efficient test case parameterization
  • Modular fixture usage
  • Comprehensive edge case coverage

nvbn/thefuck

tests/rules/test_git_main_master.py

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


@pytest.fixture
def output(branch_name):
    if not branch_name:
        return ""
    output_str = u"error: pathspec '{}' did not match any file(s) known to git"
    return output_str.format(branch_name)


@pytest.mark.parametrize(
    "script, branch_name",
    [
        ("git checkout main", "main"),
        ("git checkout master", "master"),
        ("git show main", "main"),
    ],
)
def test_match(script, branch_name, output):
    assert match(Command(script, output))


@pytest.mark.parametrize(
    "script, branch_name",
    [
        ("git checkout master", ""),
        ("git checkout main", ""),
        ("git checkout wibble", "wibble"),
    ],
)
def test_not_match(script, branch_name, output):
    assert not match(Command(script, output))


@pytest.mark.parametrize(
    "script, branch_name, new_command",
    [
        ("git checkout main", "main", "git checkout master"),
        ("git checkout master", "master", "git checkout main"),
        ("git checkout wibble", "wibble", "git checkout wibble"),
    ],
)
def test_get_new_command(script, branch_name, new_command, output):
    assert get_new_command(Command(script, output)) == new_command