From 2315929875ae0a4a230b24b5bf187d27164bd0a0 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Mon, 13 Mar 2017 22:21:34 +0100 Subject: [PATCH] #579: Add `missing_space_before_subcommand` rule --- README.md | 1 + .../test_missing_space_before_subcommand.py | 39 +++++++++++++++++++ .../rules/missing_space_before_subcommand.py | 18 +++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/rules/test_missing_space_before_subcommand.py create mode 100644 thefuck/rules/missing_space_before_subcommand.py diff --git a/README.md b/README.md index 52e4a90e..470b4d35 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `man` – changes manual section; * `man_no_space` – fixes man commands without spaces, for example `mandiff`; * `mercurial` – fixes wrong `hg` commands; +* `missing_space_before_subcommand` – fixes command with missing space like `npminstall`; * `mkdir_p` – adds `-p` when you trying to create directory without parent; * `mvn_no_command` – adds `clean package` to `mvn`; * `mvn_unknown_lifecycle_phase` – fixes misspelled lifecycle phases with `mvn`; diff --git a/tests/rules/test_missing_space_before_subcommand.py b/tests/rules/test_missing_space_before_subcommand.py new file mode 100644 index 00000000..be491ea0 --- /dev/null +++ b/tests/rules/test_missing_space_before_subcommand.py @@ -0,0 +1,39 @@ +import pytest +from thefuck.rules.missing_space_before_subcommand import ( + match, get_new_command) +from tests.utils import Command + + +@pytest.fixture(autouse=True) +def which(mocker): + return mocker.patch('thefuck.rules.missing_space_before_subcommand.which', + return_value=None) + + +@pytest.fixture(autouse=True) +def all_executables(mocker): + return mocker.patch( + 'thefuck.rules.missing_space_before_subcommand.get_all_executables', + return_value=['git', 'ls', 'npm']) + + +@pytest.mark.parametrize('script', [ + 'gitbranch', 'ls-la', 'npminstall']) +def test_match(script): + assert match(Command(script)) + + +@pytest.mark.parametrize('script, which_result', [ + ('git branch', '/usr/bin/git'), + ('vimfile', None)]) +def test_not_match(script, which_result, which): + which.return_value = which_result + assert not match(Command(script)) + + +@pytest.mark.parametrize('script, result', [ + ('gitbranch', 'git branch'), + ('ls-la', 'ls -la'), + ('npminstall webpack', 'npm install webpack')]) +def test_get_new_command(script, result): + assert get_new_command(Command(script)) == result diff --git a/thefuck/rules/missing_space_before_subcommand.py b/thefuck/rules/missing_space_before_subcommand.py new file mode 100644 index 00000000..41c8166d --- /dev/null +++ b/thefuck/rules/missing_space_before_subcommand.py @@ -0,0 +1,18 @@ +from thefuck.utils import get_all_executables, memoize, which + + +@memoize +def _get_executable(script_part): + for executable in get_all_executables(): + if script_part.startswith(executable): + return executable + + +def match(command): + return (not which(command.script_parts[0]) + and _get_executable(command.script_parts[0])) + + +def get_new_command(command): + executable = _get_executable(command.script_parts[0]) + return command.script.replace(executable, u'{} '.format(executable), 1)