From d88454a6386a91b8654b8125ae12cf94b1bdb331 Mon Sep 17 00:00:00 2001 From: ik1ne <3996272+ik1ne@users.noreply.github.com> Date: Thu, 22 Aug 2019 03:34:34 +0900 Subject: [PATCH] Add: rules/go_unknown_command for misspelled go commands. (#933) * - Add: rules/go_unknown_command for misspelled go commands. - Add: tests/test_go_unknown_command which tests match and mismatch case of rules/go_unknown_command. - Change: Added description of go_unknown_command to README.md. * Add: test_get_new_command for testing rules.go_unknown_command.test_get_new_command method. * Change: go_unknown_command.match now uses for_app decorator. * Add: get_golang_commands which dynamically gets golang possible commands. * Fix: cache proper function instead of its result. --- README.md | 1 + tests/rules/test_go_unknown_command.py | 21 +++++++++++++++++++ thefuck/rules/go_unknown_command.py | 28 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/rules/test_go_unknown_command.py create mode 100644 thefuck/rules/go_unknown_command.py diff --git a/README.md b/README.md index 83830076..4b282736 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,7 @@ following rules are enabled by default: * `git_tag_force` – adds `--force` to `git tag ` when the tag already exists; * `git_two_dashes` – adds a missing dash to commands like `git commit -amend` or `git rebase -continue`; * `go_run` – appends `.go` extension when compiling/running Go programs; +* `go_unknown_command` – fixes wrong `go` commands, for example `go bulid`; * `gradle_no_task` – fixes not found or ambiguous `gradle` task; * `gradle_wrapper` – replaces `gradle` with `./gradlew`; * `grep_arguments_order` – fixes `grep` arguments order for situations like `grep -lir . test`; diff --git a/tests/rules/test_go_unknown_command.py b/tests/rules/test_go_unknown_command.py new file mode 100644 index 00000000..b2456540 --- /dev/null +++ b/tests/rules/test_go_unknown_command.py @@ -0,0 +1,21 @@ +import pytest +from thefuck.rules.go_unknown_command import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture +def build_misspelled_output(): + return '''go bulid: unknown command +Run 'go help' for usage.''' + + +def test_match(build_misspelled_output): + assert match(Command('go bulid', build_misspelled_output)) + + +def test_not_match(): + assert not match(Command('go run', 'go run: no go files listed')) + + +def test_get_new_command(build_misspelled_output): + assert get_new_command(Command('go bulid', build_misspelled_output)) == 'go build' diff --git a/thefuck/rules/go_unknown_command.py b/thefuck/rules/go_unknown_command.py new file mode 100644 index 00000000..fd662896 --- /dev/null +++ b/thefuck/rules/go_unknown_command.py @@ -0,0 +1,28 @@ +from itertools import dropwhile, islice, takewhile +import subprocess + +from thefuck.utils import get_closest, replace_argument, for_app, which, cache + + +def get_golang_commands(): + proc = subprocess.Popen('go', stderr=subprocess.PIPE) + lines = [line.decode('utf-8').strip() for line in proc.stderr.readlines()] + lines = dropwhile(lambda line: line != 'The commands are:', lines) + lines = islice(lines, 2, None) + lines = takewhile(lambda line: line, lines) + return [line.split(' ')[0] for line in lines] + + +if which('go'): + get_docker_commands = cache(which('go'))(get_golang_commands) + + +@for_app('go') +def match(command): + return 'unknown command' in command.output + + +def get_new_command(command): + closest_subcommand = get_closest(command.script_parts[1], get_golang_commands()) + return replace_argument(command.script, command.script_parts[1], + closest_subcommand)