Skip to content

Commit

Permalink
#1133: Match commands with path prefixes in @for_app decorations
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
rpdelaney committed Jul 7, 2021
1 parent 13fda64 commit fe19428
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/test_utils.py
Expand Up @@ -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)])
Expand All @@ -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)])
Expand Down
2 changes: 1 addition & 1 deletion thefuck/rules/gradle_no_task.py
Expand Up @@ -5,7 +5,7 @@
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)

Expand Down
2 changes: 1 addition & 1 deletion thefuck/utils.py
Expand Up @@ -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

Expand Down

0 comments on commit fe19428

Please sign in to comment.