Back to Repositories

Testing PyEnv Command Correction Logic in TheFuck

This test suite validates the functionality of the omnienv_no_such_command rule in TheFuck project, focusing on handling invalid pyenv commands and providing correct command suggestions. The tests ensure proper error handling and command correction behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of pyenv command validation and correction scenarios.

Key areas tested include:
  • Invalid command detection and matching
  • Command suggestion generation
  • Special case handling for goenv output
  • Verification of non-matching scenarios

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management and fixture-based setup. The implementation leverages mock objects to simulate command output and system interactions, ensuring isolated and reliable test execution.

Technical patterns include:
  • Fixture-based test setup
  • Parametrized test cases
  • Mock object utilization

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest-mock for mocking system calls
  • Fixtures for command output simulation
  • Parametrized test functions for multiple test cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, comprehensive edge case coverage, and efficient test organization.

Notable practices:
  • Clear test case organization
  • Proper mock usage for external dependencies
  • Comprehensive command matching scenarios
  • Explicit test case documentation

nvbn/thefuck

tests/rules/test_omnienv_no_such_command.py

            
import pytest

from thefuck.rules.omnienv_no_such_command import get_new_command, match
from thefuck.types import Command


@pytest.fixture
def output(pyenv_cmd):
    return "pyenv: no such command `{}'".format(pyenv_cmd)


@pytest.fixture(autouse=True)
def Popen(mocker):
    mock = mocker.patch('thefuck.rules.omnienv_no_such_command.Popen')
    mock.return_value.stdout.readlines.return_value = (
        b'--version
activate
commands
completions
deactivate
exec_
'
        b'global
help
hooks
init
install
local
prefix_
'
        b'realpath.dylib
rehash
root
shell
shims
uninstall
version_
'
        b'version-file
version-file-read
version-file-write
version-name_
'
        b'version-origin
versions
virtualenv
virtualenv-delete_
'
        b'virtualenv-init
virtualenv-prefix
virtualenvs_
'
        b'virtualenvwrapper
virtualenvwrapper_lazy
whence
which_
'
    ).split()
    return mock


@pytest.mark.parametrize('script, pyenv_cmd', [
    ('pyenv globe', 'globe'),
    ('pyenv intall 3.8.0', 'intall'),
    ('pyenv list', 'list'),
])
def test_match(script, pyenv_cmd, output):
    assert match(Command(script, output=output))


def test_match_goenv_output_quote():
    """test goenv's specific output with quotes (')"""
    assert match(Command('goenv list', output="goenv: no such command 'list'"))


@pytest.mark.parametrize('script, output', [
    ('pyenv global', 'system'),
    ('pyenv versions', '  3.7.0
  3.7.1
* 3.7.2
'),
    ('pyenv install --list', '  3.7.0
  3.7.1
  3.7.2
'),
])
def test_not_match(script, output):
    assert not match(Command(script, output=output))


@pytest.mark.parametrize('script, pyenv_cmd, result', [
    ('pyenv globe', 'globe', 'pyenv global'),
    ('pyenv intall 3.8.0', 'intall', 'pyenv install 3.8.0'),
    ('pyenv list', 'list', 'pyenv install --list'),
    ('pyenv remove 3.8.0', 'remove', 'pyenv uninstall 3.8.0'),
])
def test_get_new_command(script, pyenv_cmd, output, result):
    assert result in get_new_command(Command(script, output))