From db4b37910d1fbab3df4294b6aa3ff896829e9150 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Mon, 3 Oct 2016 00:30:48 -0400 Subject: [PATCH 1/6] Suggest `ag -Q` when relevant This detects when `ag` suggests the `-Q` option, and adds it. --- README.md | 1 + tests/rules/test_ag_literal.py | 25 +++++++++++++++++++++++++ thefuck/rules/ag_literal.py | 11 +++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/rules/test_ag_literal.py create mode 100644 thefuck/rules/ag_literal.py diff --git a/README.md b/README.md index b2f169c..77b3064 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ sudo -H pip install thefuck --upgrade The Fuck tries to match a rule for the previous command, creates a new command using the matched rule and runs it. Rules enabled by default are as follows: +* `ag_literal` – adds `-Q` to `ag` when suggested; * `aws_cli` – fixes misspelled commands like `aws dynamdb scan` * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; diff --git a/tests/rules/test_ag_literal.py b/tests/rules/test_ag_literal.py new file mode 100644 index 0000000..660786d --- /dev/null +++ b/tests/rules/test_ag_literal.py @@ -0,0 +1,25 @@ +import pytest +from thefuck.rules.ag_literal import match, get_new_command +from tests.utils import Command + +stderr = ('ERR: Bad regex! pcre_compile() failed at position 1: missing )\n' + 'If you meant to search for a literal string, run ag with -Q\n') + +matching_command = Command(script='ag \\(', stderr=stderr) + +@pytest.mark.parametrize('command', [ + matching_command]) +def test_match(command): + assert match(matching_command) + + +@pytest.mark.parametrize('command', [ + Command(script='ag foo', stderr='')]) +def test_not_match(command): + assert not match(command) + + +@pytest.mark.parametrize('command, new_command', [ + (matching_command, 'ag -Q \\(')]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/ag_literal.py b/thefuck/rules/ag_literal.py new file mode 100644 index 0000000..4014eed --- /dev/null +++ b/thefuck/rules/ag_literal.py @@ -0,0 +1,11 @@ +from thefuck.utils import for_app +from thefuck.utils import replace_argument + + +@for_app('ag') +def match(command): + return 'run ag with -Q' in command.stderr + + +def get_new_command(command): + return command.script.replace('ag', 'ag -Q', 1) From d2e0a19aae9483906c397d21bdadbc8f3f1d6d8f Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Mon, 3 Oct 2016 00:41:34 -0400 Subject: [PATCH 2/6] Add missing semicolon to aws_cli entry in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 77b3064..e02b781 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ The Fuck tries to match a rule for the previous command, creates a new command using the matched rule and runs it. Rules enabled by default are as follows: * `ag_literal` – adds `-Q` to `ag` when suggested; -* `aws_cli` – fixes misspelled commands like `aws dynamdb scan` +* `aws_cli` – fixes misspelled commands like `aws dynamdb scan`; * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; * `cd_correction` – spellchecks and correct failed cd commands; From b2947aba8d4d220092d6ccfc8780b35830d78663 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Wed, 5 Oct 2016 10:32:14 -0400 Subject: [PATCH 3/6] test_ag_literal.py: Add blank line (PEP 8 E302) https://github.com/nvbn/thefuck/pull/561#discussion_r81892174 --- tests/rules/test_ag_literal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/rules/test_ag_literal.py b/tests/rules/test_ag_literal.py index 660786d..5237674 100644 --- a/tests/rules/test_ag_literal.py +++ b/tests/rules/test_ag_literal.py @@ -7,6 +7,7 @@ stderr = ('ERR: Bad regex! pcre_compile() failed at position 1: missing )\n' matching_command = Command(script='ag \\(', stderr=stderr) + @pytest.mark.parametrize('command', [ matching_command]) def test_match(command): From 4822ceb87aee14e5bf09f0e662f7422dd198e9f0 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Wed, 5 Oct 2016 10:34:17 -0400 Subject: [PATCH 4/6] ag_literal.py: remove unused import (Flake8 F401) https://github.com/nvbn/thefuck/pull/561#discussion_r81892699 --- thefuck/rules/ag_literal.py | 1 - 1 file changed, 1 deletion(-) diff --git a/thefuck/rules/ag_literal.py b/thefuck/rules/ag_literal.py index 4014eed..d36d698 100644 --- a/thefuck/rules/ag_literal.py +++ b/thefuck/rules/ag_literal.py @@ -1,5 +1,4 @@ from thefuck.utils import for_app -from thefuck.utils import replace_argument @for_app('ag') From 77fc021a6ca30040c3f27b17aa37377ea434d919 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Wed, 5 Oct 2016 10:52:24 -0400 Subject: [PATCH 5/6] Refactor tests/rules/test_ag_literal.py https://github.com/nvbn/thefuck/pull/561#discussion_r81894710 --- tests/rules/test_ag_literal.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/tests/rules/test_ag_literal.py b/tests/rules/test_ag_literal.py index 5237674..4040d5d 100644 --- a/tests/rules/test_ag_literal.py +++ b/tests/rules/test_ag_literal.py @@ -1,26 +1,25 @@ import pytest -from thefuck.rules.ag_literal import match, get_new_command +from thefuck.rules.ag_literal import get_new_command, match from tests.utils import Command -stderr = ('ERR: Bad regex! pcre_compile() failed at position 1: missing )\n' - 'If you meant to search for a literal string, run ag with -Q\n') -matching_command = Command(script='ag \\(', stderr=stderr) +@pytest.fixture +def stderr(): + return ('ERR: Bad regex! pcre_compile() failed at position 1: missing )\n' + 'If you meant to search for a literal string, run ag with -Q\n') -@pytest.mark.parametrize('command', [ - matching_command]) -def test_match(command): - assert match(matching_command) +@pytest.mark.parametrize('script', ['ag \(']) +def test_match(script, stderr): + assert match(Command(script=script, stderr=stderr)) -@pytest.mark.parametrize('command', [ - Command(script='ag foo', stderr='')]) -def test_not_match(command): - assert not match(command) +@pytest.mark.parametrize('script', ['ag foo']) +def test_not_match(script): + assert not match(Command(script=script)) -@pytest.mark.parametrize('command, new_command', [ - (matching_command, 'ag -Q \\(')]) -def test_get_new_command(command, new_command): - assert get_new_command(command) == new_command +@pytest.mark.parametrize('script, new_cmd', [ + ('ag \(', 'ag -Q \(')]) +def test_get_new_command(script, new_cmd, stderr): + assert get_new_command((Command(script=script, stderr=stderr))) == new_cmd From a964af7e9534edae3808acde97de054c3c6dde22 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Wed, 5 Oct 2016 10:55:49 -0400 Subject: [PATCH 6/6] ag_literal.py: use `endswith()` rather than `in` https://github.com/nvbn/thefuck/pull/561#discussion_r81898499 --- thefuck/rules/ag_literal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thefuck/rules/ag_literal.py b/thefuck/rules/ag_literal.py index d36d698..a07ae07 100644 --- a/thefuck/rules/ag_literal.py +++ b/thefuck/rules/ag_literal.py @@ -3,7 +3,7 @@ from thefuck.utils import for_app @for_app('ag') def match(command): - return 'run ag with -Q' in command.stderr + return command.stderr.endswith('run ag with -Q\n') def get_new_command(command):