Back to Repositories

Testing Chocolatey Package Installation Error Handling in thefuck

This test suite validates the functionality of Chocolatey package installation error handling and command correction in the thefuck project. It specifically tests the behavior of the choco_install rule when package installation fails due to package not being found.

Test Coverage Overview

The test suite provides comprehensive coverage of Chocolatey package installation scenarios.

Key areas tested include:
  • Package not found error detection
  • Various command formats (choco install, cinst)
  • Different command parameter combinations (-y, -n=test, /env)
  • Command correction for missing .install suffix

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple input scenarios with expected outputs. The implementation focuses on two main functions: match() for error detection and get_new_command() for command correction.

Technical patterns include:
  • Parametrized test cases for both positive and negative matches
  • Command object usage for input handling
  • Consistent error message validation

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Custom Command type for input handling
  • Simulated Chocolatey error messages
  • Parametrized test decorators for multiple test cases
  • Boolean assertions for match validation
  • String comparison for command correction verification

Best Practices Demonstrated

The test suite demonstrates several testing best practices in Python.

Notable practices include:
  • Separation of positive and negative test cases
  • Comprehensive error message validation
  • DRY principle through parametrized tests
  • Clear test case organization
  • Consistent naming conventions
  • Thorough edge case coverage

nvbn/thefuck

tests/rules/test_choco_install.py

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


package_not_found_error = (
    'Chocolatey v0.10.15
'
    'Installing the following packages:
'
    'logstitcher
'
    'By installing you accept licenses for the packages.
'
    'logstitcher not installed. The package was not found with the source(s) listed.
'
    ' Source(s): \'https://chocolatey.org/api/v2/\'
'
    ' NOTE: When you specify explicit sources, it overrides default sources.
'
    'If the package version is a prerelease and you didn\'t specify `--pre`,
'
    ' the package may not be found.
'
    'Please see https://chocolatey.org/docs/troubleshooting for more
'
    ' assistance.
'
    '
'
    'Chocolatey installed 0/1 packages. 1 packages failed.
'
    ' See the log for details (C:\\ProgramData\\chocolatey\\logs\\chocolatey.log).
'
    '
'
    'Failures
'
    ' - logstitcher - logstitcher not installed. The package was not found with the source(s) listed.
'
    ' Source(s): \'https://chocolatey.org/api/v2/\'
'
    ' NOTE: When you specify explicit sources, it overrides default sources.
'
    'If the package version is a prerelease and you didn\'t specify `--pre`,
'
    ' the package may not be found.
'
    'Please see https://chocolatey.org/docs/troubleshooting for more
'
    ' assistance.
'
)


@pytest.mark.parametrize('command', [
    Command('choco install logstitcher', package_not_found_error),
    Command('cinst logstitcher', package_not_found_error),
    Command('choco install logstitcher -y', package_not_found_error),
    Command('cinst logstitcher -y', package_not_found_error),
    Command('choco install logstitcher -y -n=test', package_not_found_error),
    Command('cinst logstitcher -y -n=test', package_not_found_error),
    Command('choco install logstitcher -y -n=test /env', package_not_found_error),
    Command('cinst logstitcher -y -n=test /env', package_not_found_error),
    Command('choco install chocolatey -y', package_not_found_error),
    Command('cinst chocolatey -y', package_not_found_error)])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('choco /?', ''),
    Command('choco upgrade logstitcher', ''),
    Command('cup logstitcher', ''),
    Command('choco upgrade logstitcher -y', ''),
    Command('cup logstitcher -y', ''),
    Command('choco upgrade logstitcher -y -n=test', ''),
    Command('cup logstitcher -y -n=test', ''),
    Command('choco upgrade logstitcher -y -n=test /env', ''),
    Command('cup logstitcher -y -n=test /env', ''),
    Command('choco upgrade chocolatey -y', ''),
    Command('cup chocolatey -y', ''),
    Command('choco uninstall logstitcher', ''),
    Command('cuninst logstitcher', ''),
    Command('choco uninstall logstitcher -y', ''),
    Command('cuninst logstitcher -y', ''),
    Command('choco uninstall logstitcher -y -n=test', ''),
    Command('cuninst logstitcher -y -n=test', ''),
    Command('choco uninstall logstitcher -y -n=test /env', ''),
    Command('cuninst logstitcher -y -n=test /env', ''),
    Command('choco uninstall chocolatey -y', ''),
    Command('cuninst chocolatey -y', '')])
def not_test_match(command):
    assert not match(command)


@pytest.mark.parametrize('before, after', [
    ('choco install logstitcher', 'choco install logstitcher.install'),
    ('cinst logstitcher', 'cinst logstitcher.install'),
    ('choco install logstitcher -y', 'choco install logstitcher.install -y'),
    ('cinst logstitcher -y', 'cinst logstitcher.install -y'),
    ('choco install logstitcher -y -n=test', 'choco install logstitcher.install -y -n=test'),
    ('cinst logstitcher -y -n=test', 'cinst logstitcher.install -y -n=test'),
    ('choco install logstitcher -y -n=test /env', 'choco install logstitcher.install -y -n=test /env'),
    ('cinst logstitcher -y -n=test /env', 'cinst logstitcher.install -y -n=test /env'),
    ('choco install chocolatey -y', 'choco install chocolatey.install -y'),
    ('cinst chocolatey -y', 'cinst chocolatey.install -y'), ])
def test_get_new_command(before, after):
    assert (get_new_command(Command(before, '')) == after)