diff --git a/README.md b/README.md index e67bff9d..3b602da7 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `tsuru_login` – runs `tsuru login` if not authenticated or session expired; * `tsuru_not_command` – fixes wrong `tsuru` commands like `tsuru shell`; * `tmux` – fixes `tmux` commands; +* `unknown_command` – fixes hadoop hdfs-style "unknown command" for example adds missing '-' to the command on `hdfs dfs ls`; * `whois` – fixes `whois` command. Enabled by default only on specific platforms: diff --git a/tests/rules/test_unknown_command.py b/tests/rules/test_unknown_command.py new file mode 100644 index 00000000..a31c9d9c --- /dev/null +++ b/tests/rules/test_unknown_command.py @@ -0,0 +1,35 @@ +import pytest +from thefuck.rules.unknown_command import match, get_new_command +from tests.utils import Command + + +@pytest.mark.parametrize('command', [ + Command(script='./bin/hdfs dfs ls', stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.'), + Command(script='hdfs dfs ls', + stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.'), + Command(script='hdfs dfs ls /foo/bar', stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command(script='./bin/hdfs dfs -ls', stderr=''), + Command(script='./bin/hdfs dfs -ls /foo/bar', stderr=''), + Command(script='hdfs dfs -ls -R /foo/bar', stderr=''), + Command()]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('hdfs dfs ls', + stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.'), ['hdfs dfs -ls']), + (Command('hdfs dfs rm /foo/bar', + stderr='rm: Unknown command\nDid you mean -rm? This command begins with a dash.'), ['hdfs dfs -rm /foo/bar']), + (Command('./bin/hdfs dfs ls -R /foo/bar', + stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.'), ['./bin/hdfs dfs -ls -R /foo/bar']), + (Command('./bin/hdfs dfs -Dtest=fred ls -R /foo/bar', + stderr='ls: Unknown command\nDid you mean -ls? This command begins with a dash.'), ['./bin/hdfs dfs -Dtest=fred -ls -R /foo/bar'])]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command + diff --git a/thefuck/rules/unknown_command.py b/thefuck/rules/unknown_command.py new file mode 100644 index 00000000..d8632251 --- /dev/null +++ b/thefuck/rules/unknown_command.py @@ -0,0 +1,13 @@ +import re +from thefuck.utils import (replace_command, get_all_matched_commands) + +def match(command, settings): + return (re.search(r"([^:]*): Unknown command.*", command.stderr) != None + and re.search(r"Did you mean ([^?]*)?", command.stderr) != None) + + +def get_new_command(command, settings): + broken_cmd = re.findall(r"([^:]*): Unknown command.*", command.stderr)[0] + matched = re.findall(r"Did you mean ([^?]*)?", command.stderr) + return replace_command(command, broken_cmd, matched) +