From b15bc8c4237f39a6b093cd30a48cc0ae79867457 Mon Sep 17 00:00:00 2001 From: nvbn Date: Fri, 24 Jul 2015 00:47:57 +0300 Subject: [PATCH] #N/A Add `gulp_not_task` rule --- README.md | 1 + tests/rules/test_gulp_not_task.py | 28 ++++++++++++++++++++++++++++ thefuck/rules/gulp_not_task.py | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tests/rules/test_gulp_not_task.py create mode 100644 thefuck/rules/gulp_not_task.py diff --git a/README.md b/README.md index aa65af00..f44c816a 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_stash` – stashes you local modifications before rebasing or switching branch; * `go_run` – appends `.go` extension when compiling/running Go programs * `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; * `heroku_no_command` – fixes wrong `heroku` commands like `heroku log`; * `history` – tries to replace command with most similar command from history; diff --git a/tests/rules/test_gulp_not_task.py b/tests/rules/test_gulp_not_task.py new file mode 100644 index 00000000..f5fa09b0 --- /dev/null +++ b/tests/rules/test_gulp_not_task.py @@ -0,0 +1,28 @@ +import pytest +from tests.utils import Command +from thefuck.rules.gulp_not_task import match, get_new_command + + +def stdout(task): + return '''[00:41:11] Using gulpfile gulpfile.js +[00:41:11] Task '{}' is not in your gulpfile +[00:41:11] Please check the documentation for proper gulpfile formatting +'''.format(task) + + +def test_match(): + assert match(Command('gulp srve', stdout('srve')), None) + + +@pytest.mark.parametrize('script, stdout', [ + ('gulp serve', ''), + ('cat srve', stdout('srve'))]) +def test_not_march(script, stdout): + assert not match(Command(script, stdout), None) + + +def test_get_new_command(mocker): + mocker.patch('thefuck.rules.gulp_not_task.get_gulp_tasks', return_value=[ + 'serve', 'build', 'default']) + command = Command('gulp srve', stdout('srve')) + assert get_new_command(command, None) == 'gulp serve' diff --git a/thefuck/rules/gulp_not_task.py b/thefuck/rules/gulp_not_task.py new file mode 100644 index 00000000..c7ecc99b --- /dev/null +++ b/thefuck/rules/gulp_not_task.py @@ -0,0 +1,22 @@ +import re +import subprocess +from thefuck.utils import get_closest, replace_argument + + +def match(command, script): + return command.script.startswith('gulp')\ + and 'is not in your gulpfile' in command.stdout + + +def get_gulp_tasks(): + proc = subprocess.Popen(['gulp', '--tasks-simple'], + stdout=subprocess.PIPE) + return [line.decode('utf-8')[:-1] + for line in proc.stdout.readlines()] + + +def get_new_command(command, script): + wrong_task = re.findall(r"Task '(\w+)' is not in your gulpfile", + command.stdout)[0] + fixed_task = get_closest(wrong_task, get_gulp_tasks()) + return replace_argument(command.script, wrong_task, fixed_task)