Back to Repositories

Testing Homebrew Install Command Correction in TheFuck

This test suite validates the Homebrew package installation correction functionality in TheFuck, focusing on handling invalid formula names and suggesting alternatives. The tests verify the pattern matching and command correction for brew install commands.

Test Coverage Overview

The test suite provides comprehensive coverage of Homebrew formula installation scenarios, particularly focusing on error handling and suggestion generation.

Key areas tested include:
  • Formula name suggestion matching
  • Multiple suggestion handling
  • Already installed package detection
  • Missing argument validation

Implementation Analysis

The testing approach utilizes pytest fixtures to simulate various Homebrew installation scenarios and error messages. The implementation follows a modular pattern with separate test functions for suggestion parsing, command matching, and new command generation.

Technical implementation features:
  • Pytest fixture-based test data management
  • Command object pattern usage
  • String parsing and pattern matching validation

Technical Details

Testing tools and configuration:
  • Framework: pytest
  • Custom fixtures for error message simulation
  • Command type implementation for input handling
  • Pattern matching for formula suggestions
  • String parsing utilities for command generation

Best Practices Demonstrated

The test suite demonstrates several testing best practices for command-line tool validation.

Notable practices include:
  • Isolated test cases with clear separation of concerns
  • Comprehensive edge case coverage
  • Reusable test fixtures
  • Negative test scenarios
  • Clear test function naming conventions

nvbn/thefuck

tests/rules/test_brew_install.py

            
import pytest
from thefuck.rules.brew_install import match, get_new_command, _get_suggestions
from thefuck.types import Command


@pytest.fixture
def brew_no_available_formula_one():
    return '''Warning: No available formula with the name "giss". Did you mean gist?'''


@pytest.fixture
def brew_no_available_formula_two():
    return '''Warning: No available formula with the name "elasticserar". Did you mean elasticsearch or elasticsearch@6?'''


@pytest.fixture
def brew_no_available_formula_three():
    return '''Warning: No available formula with the name "gitt". Did you mean git, gitg or gist?'''


@pytest.fixture
def brew_install_no_argument():
    return '''Install a formula or cask. Additional options specific to a formula may be'''


@pytest.fixture
def brew_already_installed():
    return '''Warning: git-2.3.5 already installed'''


def test_suggestions():
    assert _get_suggestions("one") == ['one']
    assert _get_suggestions("one or two") == ['one', 'two']
    assert _get_suggestions("one, two or three") == ['one', 'two', 'three']


def test_match(brew_no_available_formula_one, brew_no_available_formula_two,
               brew_no_available_formula_three, brew_already_installed,
               brew_install_no_argument):
    assert match(Command('brew install giss',
                         brew_no_available_formula_one))
    assert match(Command('brew install elasticserar',
                         brew_no_available_formula_two))
    assert match(Command('brew install gitt',
                         brew_no_available_formula_three))
    assert not match(Command('brew install git',
                             brew_already_installed))
    assert not match(Command('brew install', brew_install_no_argument))


def test_get_new_command(brew_no_available_formula_one, brew_no_available_formula_two,
                         brew_no_available_formula_three):
    assert get_new_command(Command('brew install giss',
                                   brew_no_available_formula_one))\
        == ['brew install gist']
    assert get_new_command(Command('brew install elasticsear',
                                   brew_no_available_formula_two))\
        == ['brew install elasticsearch', 'brew install elasticsearch@6']
    assert get_new_command(Command('brew install gitt',
                                   brew_no_available_formula_three))\
        == ['brew install git', 'brew install gitg', 'brew install gist']

    assert get_new_command(Command('brew install aa',
                                   brew_no_available_formula_one))\
        != 'brew install aha'