From 4f95b3365acee4b7ddcc4081a986472021d77f94 Mon Sep 17 00:00:00 2001 From: nvbn Date: Mon, 8 Feb 2016 11:04:19 +0300 Subject: [PATCH] #N/A: Add `grep_arguments_order` rule --- README.md | 3 +- tests/rules/test_grep_arguments_order.py | 40 ++++++++++++++++++++++++ thefuck/rules/grep_arguments_order.py | 23 ++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/rules/test_grep_arguments_order.py create mode 100644 thefuck/rules/grep_arguments_order.py diff --git a/README.md b/README.md index 8a1cae57..2ff554fa 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,8 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_push_pull` – runs `git pull` when `push` was rejected; * `git_stash` – stashes you local modifications before rebasing or switching branch; * `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_run` – appends `.go` extension when compiling/running Go programs; +* `grep_arguments_order` – fixes grep arguments order for situations like `grep -lir . test`; * `grep_recursive` – adds `-r` when you trying to `grep` directory; * `gulp_not_task` – fixes misspelled `gulp` tasks; * `has_exists_script` – prepends `./` when script/binary exists; diff --git a/tests/rules/test_grep_arguments_order.py b/tests/rules/test_grep_arguments_order.py new file mode 100644 index 00000000..b2896493 --- /dev/null +++ b/tests/rules/test_grep_arguments_order.py @@ -0,0 +1,40 @@ +import pytest +from thefuck.rules.grep_arguments_order import get_new_command, match +from tests.utils import Command + +stderr = 'grep: {}: No such file or directory'.format + + +@pytest.fixture(autouse=True) +def os_path(monkeypatch): + monkeypatch.setattr('os.path.isfile', lambda x: not x.startswith('-')) + + +@pytest.mark.parametrize('script, file', [ + ('grep test.py test', 'test'), + ('grep -lir . test', 'test'), + ('egrep test.py test', 'test'), + ('egrep -lir . test', 'test')]) +def test_match(script, file): + assert match(Command(script, stderr=stderr(file))) + + +@pytest.mark.parametrize('script, stderr', [ + ('cat test.py', stderr('test')), + ('grep test test.py', ''), + ('grep -lir test .', ''), + ('egrep test test.py', ''), + ('egrep -lir test .', '')]) +def test_not_match(script, stderr): + assert not match(Command(script, stderr=stderr)) + + +@pytest.mark.parametrize('script, stderr, result', [ + ('grep test.py test', stderr('test'), 'grep test test.py'), + ('grep -lir . test', stderr('test'), 'grep -lir test .'), + ('grep . test -lir', stderr('test'), 'grep test -lir .'), + ('egrep test.py test', stderr('test'), 'egrep test test.py'), + ('egrep -lir . test', stderr('test'), 'egrep -lir test .'), + ('egrep . test -lir', stderr('test'), 'egrep test -lir .')]) +def test_get_new_command(script, stderr, result): + assert get_new_command(Command(script, stderr=stderr)) == result diff --git a/thefuck/rules/grep_arguments_order.py b/thefuck/rules/grep_arguments_order.py new file mode 100644 index 00000000..1409ba22 --- /dev/null +++ b/thefuck/rules/grep_arguments_order.py @@ -0,0 +1,23 @@ +import os +from thefuck.utils import for_app + + +def _get_actual_file(parts): + for part in parts[1:]: + if os.path.isfile(part) or os.path.isdir(part): + return part + + +@for_app('grep', 'egrep') +def match(command): + return ': No such file or directory' in command.stderr \ + and _get_actual_file(command.script_parts) + + +def get_new_command(command): + actual_file = _get_actual_file(command.script_parts) + parts = command.script_parts[::] + # Moves file to the end of the script: + parts.remove(actual_file) + parts.append(actual_file) + return ' '.join(parts)