Back to Repositories

Testing Yarn Command Replacement Logic in thefuck

This test suite validates the yarn command replacement functionality in thefuck, specifically testing the conversion of deprecated ‘yarn install’ commands to the newer ‘yarn add’ syntax. The tests ensure proper handling of package installation commands and their automated correction.

Test Coverage Overview

The test suite provides comprehensive coverage of yarn command replacements, focusing on package installation scenarios.

Key areas tested include:
  • Matching deprecated ‘yarn install’ commands
  • Verifying correct transformation to ‘yarn add’ commands
  • Handling various package names (redux, moment, lodash)
  • Edge case validation for bare ‘yarn install’ command

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator to efficiently test multiple input scenarios with minimal code duplication.

Key implementation patterns:
  • Parametrized test cases for both matching and command transformation
  • Separate test functions for positive and negative cases
  • Command object usage for input/output handling

Technical Details

Testing infrastructure includes:
  • pytest as the primary testing framework
  • Custom Command type from thefuck.types
  • Parametrized test decorators for test case multiplication
  • String formatting for error message validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear separation of matching and transformation logic
  • Comprehensive test case coverage
  • DRY principle through parametrized tests
  • Explicit assertion statements
  • Well-structured test organization

nvbn/thefuck

tests/rules/test_yarn_command_replaced.py

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


output = ('error `install` has been replaced with `add` to add new '
          'dependencies. Run "yarn add {}" instead.').format


@pytest.mark.parametrize('command', [
    Command('yarn install redux', output('redux')),
    Command('yarn install moment', output('moment')),
    Command('yarn install lodash', output('lodash'))])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('yarn install', '')])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('yarn install redux', output('redux')),
     'yarn add redux'),
    (Command('yarn install moment', output('moment')),
     'yarn add moment'),
    (Command('yarn install lodash', output('lodash')),
     'yarn add lodash')])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command