Back to Repositories

Testing Git Clone Command Correction Implementation in TheFuck

This test suite validates the git_clone_missing rule in TheFuck project, which handles cases where users attempt to clone Git repositories without the ‘git clone’ prefix. The tests ensure proper detection and correction of incomplete git clone commands across various URL formats.

Test Coverage Overview

The test suite provides comprehensive coverage of Git repository URL patterns and error scenarios.

Key areas tested include:
  • Valid Git URLs (HTTPS, SSH, and git protocols)
  • Invalid command formats and malformed URLs
  • Various error message outputs
  • Edge cases like missing protocols and duplicate commands

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple combinations of inputs and outputs. The implementation leverages fixture-based testing to validate both the matching logic and command generation functionality.

Technical patterns include:
  • Parameterized test cases for URL formats
  • Command object usage for input/output simulation
  • Separate positive and negative test cases

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Command class from thefuck.types for command simulation
  • Parametrized test fixtures for input variation
  • Custom test data sets for valid and invalid URLs
  • Multiple error message patterns

Best Practices Demonstrated

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

Notable practices include:
  • Comprehensive input validation
  • Separation of concerns between matching and command generation
  • Clear test data organization
  • Efficient test case multiplication using parametrize
  • Explicit positive and negative test scenarios

nvbn/thefuck

tests/rules/test_git_clone_missing.py

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

valid_urls = [
    'https://github.com/nvbn/thefuck.git',
    'https://github.com/nvbn/thefuck',
    'http://github.com/nvbn/thefuck.git',
    '[email protected]:nvbn/thefuck.git',
    '[email protected]:nvbn/thefuck',
    'ssh://[email protected]:nvbn/thefuck.git',
]
invalid_urls = [
    '',  # No command
    'notacommand',  # Command not found
    'ssh [email protected]:nvbn/thefrick.git',  # ssh command, not a git clone
    'git clone foo',  # Valid clone
    'git clone https://github.com/nvbn/thefuck.git',  # Full command
    'github.com/nvbn/thefuck.git',  # Missing protocol
    'github.com:nvbn/thefuck.git',  # SSH missing username
    'git clone git clone ssh://[email protected]:nvbn/thefrick.git',  # 2x clone
    'https:/github.com/nvbn/thefuck.git'  # Bad protocol
]
outputs = [
    'No such file or directory',
    'not found',
    'is not recognised as',
]


@pytest.mark.parametrize('cmd', valid_urls)
@pytest.mark.parametrize('output', outputs)
def test_match(cmd, output):
    c = Command(cmd, output)
    assert match(c)


@pytest.mark.parametrize('cmd', invalid_urls)
@pytest.mark.parametrize('output', outputs + ["some other output"])
def test_not_match(cmd, output):
    c = Command(cmd, output)
    assert not match(c)


@pytest.mark.parametrize('script', valid_urls)
@pytest.mark.parametrize('output', outputs)
def test_get_new_command(script, output):
    command = Command(script, output)
    new_command = 'git clone ' + script
    assert get_new_command(command) == new_command