Back to Repositories

Testing Pip Install Command Corrections in thefuck

This test suite validates the pip_install rule functionality in the thefuck project, focusing on permission-related command corrections and user installation options. The tests ensure proper handling of pip install commands and their automated fixes.

Test Coverage Overview

The test suite provides comprehensive coverage of pip install command scenarios.

Key areas tested include:
  • Permission denied errors and –user flag addition
  • Successful installation verification
  • Command transformation between user and sudo installations
Integration points focus on the Command type interface and environment error handling.

Implementation Analysis

The testing approach uses pytest fixtures to validate both the match and get_new_command functions. The implementation follows a pattern of testing both positive and negative cases, with specific attention to command string manipulation and error response parsing.

Technical patterns include:
  • Mock command response validation
  • String pattern matching verification
  • Command transformation logic testing

Technical Details

Testing tools and configuration:
  • Python unittest framework
  • Command object from thefuck.types
  • Mock response strings for environment errors
  • Assertion-based validation

Best Practices Demonstrated

The test suite exhibits strong testing practices through clear separation of concerns and comprehensive edge case coverage.

Notable practices include:
  • Isolated test functions for match and command generation
  • Representative error message testing
  • Multiple command variation coverage
  • Clear test case organization

nvbn/thefuck

tests/rules/test_pip_install.py

            
# -*- coding: UTF-8 -*-
from thefuck.rules.pip_install import match, get_new_command
from thefuck.types import Command


def test_match():
    response1 = """
    Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/entrypoints.pyc'
Consider using the `--user` option or check the permissions.
"""
    assert match(Command('pip install -r requirements.txt', response1))

    response2 = """
Collecting bacon
  Downloading https://files.pythonhosted.org/packages/b2/81/19fb79139ee71c8bc4e5a444546f318e2b87253b8939ec8a7e10d63b7341/bacon-0.3.1.zip (11.0MB)
    100% |████████████████████████████████| 11.0MB 3.0MB/s
Installing collected packages: bacon
  Running setup.py install for bacon ... done
Successfully installed bacon-0.3.1
"""
    assert not match(Command('pip install bacon', response2))


def test_get_new_command():
    assert get_new_command(Command('pip install -r requirements.txt', '')) == 'pip install --user -r requirements.txt'
    assert get_new_command(Command('pip install bacon', '')) == 'pip install --user bacon'
    assert get_new_command(Command('pip install --user -r requirements.txt', '')) == 'sudo pip install -r requirements.txt'