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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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))