From 21bb439d7caac462f15042395824cedac9e7d35a Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Sat, 13 Aug 2016 19:30:46 +0300 Subject: [PATCH] #N/A: Add `gradle_wrapper` rule --- README.md | 1 + tests/rules/test_gradle_wrapper.py | 38 ++++++++++++++++++++++++++++++ thefuck/rules/gradle_wrapper.py | 13 ++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/rules/test_gradle_wrapper.py create mode 100644 thefuck/rules/gradle_wrapper.py diff --git a/README.md b/README.md index 44720a59..05478be1 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,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; * `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; +* `gradle_wrapper` – replaces `gradle` with `./gradlew`; * `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; diff --git a/tests/rules/test_gradle_wrapper.py b/tests/rules/test_gradle_wrapper.py new file mode 100644 index 00000000..39d1bc43 --- /dev/null +++ b/tests/rules/test_gradle_wrapper.py @@ -0,0 +1,38 @@ +import pytest +from thefuck.rules.gradle_wrapper import match, get_new_command +from tests.utils import Command + + +@pytest.fixture(autouse=True) +def exists(mocker): + return mocker.patch('thefuck.rules.gradle_wrapper.os.path.isfile', + return_value=True) + + +@pytest.mark.parametrize('command', [ + Command('gradle tasks', stderr='gradle: not found'), + Command('gradle build', stderr='gradle: not found')]) +def test_match(mocker, command): + mocker.patch('thefuck.rules.gradle_wrapper.which', return_value=None) + + assert match(command) + + +@pytest.mark.parametrize('command, gradlew, which', [ + (Command('gradle tasks', stderr='gradle: not found'), False, None), + (Command('gradle tasks', stderr='command not found'), True, '/usr/bin/gradle'), + (Command('npm tasks', stderr='npm: not found'), True, None)]) +def test_not_match(mocker, exists, command, gradlew, which): + mocker.patch('thefuck.rules.gradle_wrapper.which', return_value=which) + exists.return_value = gradlew + + assert not match(command) + + +@pytest.mark.parametrize('script, result', [ + ('gradle assemble', './gradlew assemble'), + ('gradle --help', './gradlew --help'), + ('gradle build -c', './gradlew build -c')]) +def test_get_new_command(script, result): + command = Command(script) + assert get_new_command(command) == result diff --git a/thefuck/rules/gradle_wrapper.py b/thefuck/rules/gradle_wrapper.py new file mode 100644 index 00000000..e325d9e3 --- /dev/null +++ b/thefuck/rules/gradle_wrapper.py @@ -0,0 +1,13 @@ +import os +from thefuck.utils import for_app, which + + +@for_app('gradle') +def match(command): + return (not which(command.script_parts[0]) + and 'not found' in command.stderr + and os.path.isfile('gradlew')) + + +def get_new_command(command): + return u'./gradlew {}'.format(' '.join(command.script_parts[1:]))