From a906a751c8bf1af672d991d99b78961e3a622af1 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Sun, 15 Oct 2017 15:51:09 +0200 Subject: [PATCH] #N/A: Add `prove_recursively` rule --- README.md | 1 + tests/rules/test_prove_recursively.py | 40 +++++++++++++++++++++++++++ thefuck/rules/prove_recursively.py | 27 ++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/rules/test_prove_recursively.py create mode 100644 thefuck/rules/prove_recursively.py diff --git a/README.md b/README.md index 1ba5b9e4..ae1d58c6 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `pip_unknown_command` – fixes wrong `pip` commands, for example `pip instatl/pip install`; * `php_s` – replaces `-s` by `-S` when trying to run a local php server; * `port_already_in_use` – kills process that bound port; +* `prove_recursively` – adds `-r` when called with directory; * `python_command` – prepends `python` when you trying to run not executable/without `./` python script; * `python_execute` – appends missing `.py` when executing Python files; * `quotation_marks` – fixes uneven usage of `'` and `"` when containing args'; diff --git a/tests/rules/test_prove_recursively.py b/tests/rules/test_prove_recursively.py new file mode 100644 index 00000000..6f6f3c71 --- /dev/null +++ b/tests/rules/test_prove_recursively.py @@ -0,0 +1,40 @@ +import pytest +from thefuck.rules.prove_recursively import match, get_new_command +from thefuck.types import Command + + +output = '''Files=0, Tests=0, 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) +Result: NOTESTS''' + + +@pytest.fixture +def isdir(mocker): + return mocker.patch('thefuck.rules.prove_recursively' + '.os.path.isdir') + + +@pytest.mark.parametrize('script, output', [ + ('prove -lv t', output), + ('prove app/t', output)]) +def test_match(isdir, script, output): + isdir.return_value = True + command = Command(script, output) + assert match(command) + + +@pytest.mark.parametrize('script, output, isdir_result', [ + ('prove -lv t', output, False), + ('prove -r t', output, True), + ('prove --recurse t', output, True)]) +def test_not_match(isdir, script, output, isdir_result): + isdir.return_value = isdir_result + command = Command(script, output) + assert not match(command) + + +@pytest.mark.parametrize('before, after', [ + ('prove -lv t', 'prove -r -lv t'), + ('prove t', 'prove -r t')]) +def test_get_new_command(before, after): + command = Command(before, output) + assert get_new_command(command) == after diff --git a/thefuck/rules/prove_recursively.py b/thefuck/rules/prove_recursively.py new file mode 100644 index 00000000..fad5a63c --- /dev/null +++ b/thefuck/rules/prove_recursively.py @@ -0,0 +1,27 @@ +import os +from thefuck.utils import for_app + + +def _is_recursive(part): + if part == '--recurse': + return True + elif not part.startswith('--') and part.startswith('-') and 'r' in part: + return True + + +def _isdir(part): + return not part.startswith('-') and os.path.isdir(part) + + +@for_app('prove') +def match(command): + return ( + 'NOTESTS' in command.stdout + and not any(_is_recursive(part) for part in command.script_parts[1:]) + and any(_isdir(part) for part in command.script_parts[1:])) + + +def get_new_command(command): + parts = command.script_parts[:] + parts.insert(1, '-r') + return u' '.join(parts)