Back to Repositories

Testing Git Branch Existence Validation in TheFuck

This test suite validates the git_branch_exists rule in TheFuck project, focusing on handling duplicate Git branch scenarios and providing appropriate command corrections.

Test Coverage Overview

The test suite comprehensively covers Git branch creation and checkout scenarios where a branch already exists. Key test cases include:
  • Basic branch creation validation
  • Branch checkout verification
  • Special character handling in branch names
  • Multiple command correction options

Implementation Analysis

The implementation uses pytest’s parametrize feature to test multiple scenarios efficiently. The testing approach focuses on two main functions: match() for detecting branch conflicts and get_new_command() for generating alternative commands.

The test structure employs fixtures for reusable test data and command generation.

Technical Details

Testing components include:
  • pytest framework for test organization
  • Command class from thefuck.types
  • Fixtures for output and command generation
  • Parametrized test cases for various branch scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Separation of test cases using fixtures
  • Comprehensive negative testing
  • Parametrized testing for multiple scenarios
  • Clear test case organization and naming

nvbn/thefuck

tests/rules/test_git_branch_exists.py

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


@pytest.fixture
def output(src_branch_name):
    return "fatal: A branch named '{}' already exists.".format(src_branch_name)


@pytest.fixture
def new_command(branch_name):
    return [cmd.format(branch_name) for cmd in [
        'git branch -d {0} && git branch {0}',
        'git branch -d {0} && git checkout -b {0}',
        'git branch -D {0} && git branch {0}',
        'git branch -D {0} && git checkout -b {0}', 'git checkout {0}']]


@pytest.mark.parametrize('script, src_branch_name, branch_name', [
    ('git branch foo', 'foo', 'foo'),
    ('git checkout bar', 'bar', 'bar'),
    ('git checkout -b "let\'s-push-this"', '"let\'s-push-this"', '"let\'s-push-this"')])
def test_match(output, script, branch_name):
    assert match(Command(script, output))


@pytest.mark.parametrize('script', [
    'git branch foo',
    'git checkout bar',
    'git checkout -b "let\'s-push-this"'])
def test_not_match(script):
    assert not match(Command(script, ''))


@pytest.mark.parametrize('script, src_branch_name, branch_name', [
    ('git branch foo', 'foo', 'foo'),
    ('git checkout bar', 'bar', 'bar'),
    ('git checkout -b "let\'s-push-this"', "let's-push-this", "let\\'s-push-this")])
def test_get_new_command(output, new_command, script, src_branch_name, branch_name):
    assert get_new_command(Command(script, output)) == new_command