Back to Repositories

Testing Git Push Command Correction Functionality in thefuck

This test suite validates the git push command functionality in the thefuck project, specifically focusing on handling upstream branch configurations and error scenarios. It ensures proper command correction behavior when users attempt git push operations without configured upstream branches.

Test Coverage Overview

The test suite provides comprehensive coverage of git push command scenarios, particularly focusing on upstream branch configuration cases.

Key areas tested include:
  • Basic git push command validation
  • Various command variations with different flags and parameters
  • Branch name handling including special characters
  • Force push scenarios
  • Bitbucket-specific output handling

Implementation Analysis

The testing approach utilizes pytest’s parametrization feature to efficiently test multiple input combinations and expected outcomes. The implementation employs fixture-based setup for test data generation, allowing for consistent and maintainable test scenarios.

Key patterns include:
  • Parametrized test cases for command variations
  • Fixture-based test output generation
  • Command object usage for input handling
  • Explicit positive and negative test cases

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type for input handling
  • Fixtures for output generation
  • Parametrize decorator for test case multiplication
  • Branch name parameterization for different scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing. It demonstrates clean separation of concerns, efficient test case organization, and thorough edge case coverage.

Notable practices include:
  • Comprehensive positive and negative test cases
  • Efficient test parameterization
  • Clear test case naming
  • Isolated test scenarios
  • Reusable test fixtures

nvbn/thefuck

tests/rules/test_git_push.py

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


@pytest.fixture
def output(branch_name):
    if not branch_name:
        return ''
    return '''fatal: The current branch {} has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin {}

'''.format(branch_name, branch_name)


@pytest.fixture
def output_bitbucket():
    return '''Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for feature/set-upstream:
remote:   https://bitbucket.org/set-upstream
remote:
To [email protected]:test.git
   e5e7fbb..700d998  feature/set-upstream -> feature/set-upstream
Branch feature/set-upstream set up to track remote branch feature/set-upstream from origin.
'''


@pytest.mark.parametrize('script, branch_name', [
    ('git push', 'master'),
    ('git push origin', 'master')])
def test_match(output, script, branch_name):
    assert match(Command(script, output))


def test_match_bitbucket(output_bitbucket):
    assert not match(Command('git push origin', output_bitbucket))


@pytest.mark.parametrize('script, branch_name', [
    ('git push master', None),
    ('ls', 'master')])
def test_not_match(output, script, branch_name):
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, branch_name, new_command', [
    ('git push', 'master',
     'git push --set-upstream origin master'),
    ('git push master', 'master',
     'git push --set-upstream origin master'),
    ('git push -u', 'master',
     'git push --set-upstream origin master'),
    ('git push -u origin', 'master',
     'git push --set-upstream origin master'),
    ('git push origin', 'master',
     'git push --set-upstream origin master'),
    ('git push --set-upstream origin', 'master',
     'git push --set-upstream origin master'),
    ('git push --quiet', 'master',
     'git push --set-upstream origin master --quiet'),
    ('git push --quiet origin', 'master',
     'git push --set-upstream origin master --quiet'),
    ('git -c test=test push --quiet origin', 'master',
     'git -c test=test push --set-upstream origin master --quiet'),
    ('git push', "test's",
     "git push --set-upstream origin test\\'s"),
    ('git push --force', 'master',
     'git push --set-upstream origin master --force'),
    ('git push --force-with-lease', 'master',
     'git push --set-upstream origin master --force-with-lease')])
def test_get_new_command(output, script, branch_name, new_command):
    assert get_new_command(Command(script, output)) == new_command