diff --git a/README.md b/README.md index 39bfa741..069e0d69 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,16 @@ To push the current branch and set the remote as upstream, use ➜ fuck Counting objects: 9, done. ... + +➜ puthon +No command 'puthon' found, did you mean: + Command 'python' from package 'python-minimal' (main) + Command 'python' from package 'python3' (main) +zsh: command not found: puthon + +➜ fuck +Python 3.4.2 (default, Oct 8 2014, 13:08:17) +... ``` ## Installation diff --git a/setup.py b/setup.py index 783d9037..fb680ea7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup(name='thefuck', - version=1.0, + version=1.1, description="Magnificent app which corrects your previous console command", author='Vladimir Iakovlev', author_email='nvbn.rm@gmail.com', diff --git a/tests/rules/test_no_command.py b/tests/rules/test_no_command.py new file mode 100644 index 00000000..a24d9bbb --- /dev/null +++ b/tests/rules/test_no_command.py @@ -0,0 +1,44 @@ +from unittest.mock import patch +import pytest +from subprocess import PIPE +from thefuck.rules.no_command import match, get_new_command +from thefuck.main import Command + + +@pytest.fixture +def command_found(): + return b'''No command 'aptget' found, did you mean: + Command 'apt-get' from package 'apt' (main) +aptget: command not found +''' + +@pytest.fixture +def command_not_found(): + return b'''No command 'vom' found, but there are 19 similar ones +vom: command not found +''' + + +def test_match(command_found, command_not_found): + with patch('thefuck.rules.no_command.Popen') as Popen: + Popen.return_value.stderr.read.return_value = command_found + assert match(Command('aptget install vim', '', '')) + Popen.assert_called_once_with('/usr/lib/command-not-found aptget', + shell=True, stderr=PIPE) + Popen.return_value.stderr.read.return_value = command_not_found + assert not match(Command('ls', '', '')) + + with patch('thefuck.rules.no_command.Popen') as Popen: + Popen.return_value.stderr.read.return_value = command_found + assert match(Command('sudo aptget install vim', '', '')) + Popen.assert_called_once_with('/usr/lib/command-not-found aptget', + shell=True, stderr=PIPE) + + +def test_get_new_command(command_found): + with patch('thefuck.rules.no_command._get_output', + return_value=command_found.decode()): + assert get_new_command(Command('aptget install vim', '', ''))\ + == 'apt-get install vim' + assert get_new_command(Command('sudo aptget install vim', '', '')) \ + == 'sudo apt-get install vim' diff --git a/thefuck/main.py b/thefuck/main.py index d73cf424..c1c0775e 100644 --- a/thefuck/main.py +++ b/thefuck/main.py @@ -2,6 +2,7 @@ from collections import namedtuple from imp import load_source from pathlib import Path from os.path import expanduser +from os import environ from subprocess import Popen, PIPE import sys diff --git a/thefuck/rules/no_command.py b/thefuck/rules/no_command.py new file mode 100644 index 00000000..f7ded058 --- /dev/null +++ b/thefuck/rules/no_command.py @@ -0,0 +1,23 @@ +from subprocess import Popen, PIPE +import re + + +def _get_output(command): + name = command.script.split(' ')[command.script.startswith('sudo')] + check_script = '/usr/lib/command-not-found {}'.format(name) + result = Popen(check_script, shell=True, stderr=PIPE) + return result.stderr.read().decode() + + +def match(command): + output = _get_output(command) + return "No command" in output and "from package" in output + + +def get_new_command(command): + output = _get_output(command) + broken_name = re.findall(r"No command '([^']*)' found", + output)[0] + fixed_name = re.findall(r"Command '([^']*)' from package", + output)[0] + return command.script.replace(broken_name, fixed_name)