From 2a166a7dec3c872ef6be3870b50889500ca6a212 Mon Sep 17 00:00:00 2001 From: Yotam Salmon Date: Sun, 8 Aug 2021 00:26:36 +0300 Subject: [PATCH] #977: Add `wrong_hyphen_before_subcommand` rule Co-authored-by: user Co-authored-by: Pablo Santiago Blum de Aguiar --- README.md | 1 + .../test_wrong_hyphen_before_subcommand.py | 30 +++++++++++++++++++ .../rules/wrong_hyphen_before_subcommand.py | 20 +++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tests/rules/test_wrong_hyphen_before_subcommand.py create mode 100644 thefuck/rules/wrong_hyphen_before_subcommand.py diff --git a/README.md b/README.md index 35ed44ae..0fa3f4c0 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,7 @@ following rules are enabled by default: * `vagrant_up` – starts up the vagrant instance; * `whois` – fixes `whois` command; * `workon_doesnt_exists` – fixes `virtualenvwrapper` env name os suggests to create new. +* `wrong_hyphen_before_subcommand` – removes an improperly placed hyphen (`apt-install` -> `apt install`, `git-log` -> `git log`, etc.) * `yarn_alias` – fixes aliased `yarn` commands like `yarn ls`; * `yarn_command_not_found` – fixes misspelled `yarn` commands; * `yarn_command_replaced` – fixes replaced `yarn` commands; diff --git a/tests/rules/test_wrong_hyphen_before_subcommand.py b/tests/rules/test_wrong_hyphen_before_subcommand.py new file mode 100644 index 00000000..30c91ab1 --- /dev/null +++ b/tests/rules/test_wrong_hyphen_before_subcommand.py @@ -0,0 +1,30 @@ +import pytest + +from thefuck.rules.wrong_hyphen_before_subcommand import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture(autouse=True) +def get_all_executables(mocker): + mocker.patch( + "thefuck.rules.wrong_hyphen_before_subcommand.get_all_executables", + return_value=["git", "apt", "apt-get", "ls", "pwd"], + ) + + +@pytest.mark.parametrize("script", ["git-log", "apt-install python"]) +def test_match(script): + assert match(Command(script, "")) + + +@pytest.mark.parametrize("script", ["ls -la", "git2-make", "apt-get install python"]) +def test_not_match(script): + assert not match(Command(script, "")) + + +@pytest.mark.parametrize( + "script, new_command", + [("git-log", "git log"), ("apt-install python", "apt install python")], +) +def test_get_new_command(script, new_command): + assert get_new_command(Command(script, "")) == new_command diff --git a/thefuck/rules/wrong_hyphen_before_subcommand.py b/thefuck/rules/wrong_hyphen_before_subcommand.py new file mode 100644 index 00000000..4fbaf64f --- /dev/null +++ b/thefuck/rules/wrong_hyphen_before_subcommand.py @@ -0,0 +1,20 @@ +from thefuck.utils import get_all_executables +from thefuck.specific.sudo import sudo_support + + +@sudo_support +def match(command): + first_part = command.script_parts[0] + if "-" not in first_part or first_part in get_all_executables(): + return False + cmd, _ = first_part.split("-", 1) + return cmd in get_all_executables() + + +@sudo_support +def get_new_command(command): + return command.script.replace("-", " ", 1) + + +priority = 4500 +requires_output = False