From fe1942866bb2cee93f750a97ee026508aa0b4fc6 Mon Sep 17 00:00:00 2001 From: Ryan Delaney <1139517+rpdelaney@users.noreply.github.com> Date: Wed, 7 Jul 2021 17:05:55 -0400 Subject: [PATCH] #1133: Match commands with path prefixes in @for_app decorations * Resolve paths before checking app identity Commands entered with a path do not match is_app. I encountered this when working with a test for the rm_dir rule. This rule did not use the @for_app decorator, but when I migrated it, the test for "./bin/hdfs.." failed because 'hdfs' was recognized as a command, while "./bin/hdfs" was not. This commit addresses the false negative by resolving path names in the command, via os.path.basename. * Remove paths from for_app invocations in rules I presume that the `./` in `./gradlew` was used here because thefuck would not find an app match on just `gradlew`, and thus no fucks would be given on the most common and idiomatic way of invoking gradlew. After 8faf9b1, thefuck does not distinguish between commands with paths and those without. Therefore, the tests for this rule are now broken because thefuck strips paths from the _user_'s command, but not from the for_app decoration. This commit addresses that problem by changing the for_app decoration to this rule. --- tests/test_utils.py | 4 ++++ thefuck/rules/gradle_no_task.py | 2 +- thefuck/utils.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 8a315916..eae743f5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -146,6 +146,8 @@ def test_get_all_matched_commands(stderr, result): @pytest.mark.usefixtures('no_memoize') @pytest.mark.parametrize('script, names, result', [ + ('/usr/bin/git diff', ['git', 'hub'], True), + ('/bin/hdfs dfs -rm foo', ['hdfs'], True), ('git diff', ['git', 'hub'], True), ('hub diff', ['git', 'hub'], True), ('hg diff', ['git', 'hub'], False)]) @@ -155,6 +157,8 @@ def test_is_app(script, names, result): @pytest.mark.usefixtures('no_memoize') @pytest.mark.parametrize('script, names, result', [ + ('/usr/bin/git diff', ['git', 'hub'], True), + ('/bin/hdfs dfs -rm foo', ['hdfs'], True), ('git diff', ['git', 'hub'], True), ('hub diff', ['git', 'hub'], True), ('hg diff', ['git', 'hub'], False)]) diff --git a/thefuck/rules/gradle_no_task.py b/thefuck/rules/gradle_no_task.py index 7820d1df..4a074794 100644 --- a/thefuck/rules/gradle_no_task.py +++ b/thefuck/rules/gradle_no_task.py @@ -5,7 +5,7 @@ from thefuck.utils import for_app, eager, replace_command regex = re.compile(r"Task '(.*)' (is ambiguous|not found)") -@for_app('gradle', './gradlew') +@for_app('gradle', 'gradlew') def match(command): return regex.findall(command.output) diff --git a/thefuck/utils.py b/thefuck/utils.py index 3a632a5a..42f32631 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -180,7 +180,7 @@ def is_app(command, *app_names, **kwargs): raise TypeError("got an unexpected keyword argument '{}'".format(kwargs.keys())) if len(command.script_parts) > at_least: - return command.script_parts[0] in app_names + return os.path.basename(command.script_parts[0]) in app_names return False