From b494c4e273943f4b05925da141cccd8a8a63de26 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 13 Aug 2015 13:09:19 +0100 Subject: [PATCH 1/4] basic support for the hdfs dfs when the command misses the dash --- README.md | 1 + tests/rules/test_hadoop_dfs_missing_dash.py | 30 +++++++++++++++++++++ thefuck/rules/hadoop_dfs_missing_dash.py | 10 +++++++ 3 files changed, 41 insertions(+) create mode 100644 tests/rules/test_hadoop_dfs_missing_dash.py create mode 100644 thefuck/rules/hadoop_dfs_missing_dash.py diff --git a/README.md b/README.md index 4f974177..0197c1f4 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `tsuru_not_command` – fixes wrong tsuru commands like `tsuru shell`; * `tmux` – fixes `tmux` commands; * `whois` – fixes `whois` command. +* `hdfs dfs` – Add the missing dash to the command. Enabled by default only on specific platforms: diff --git a/tests/rules/test_hadoop_dfs_missing_dash.py b/tests/rules/test_hadoop_dfs_missing_dash.py new file mode 100644 index 00000000..8bb659de --- /dev/null +++ b/tests/rules/test_hadoop_dfs_missing_dash.py @@ -0,0 +1,30 @@ +import pytest +from thefuck.rules.hadoop_dfs_missing_dash 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'), 'hdfs dfs -ls'), + (Command('hdfs dfs ls /foo/bar'), 'hdfs dfs -ls /foo/bar'), + (Command('./bin/hdfs dfs ls -R /foo/bar'), './bin/hdfs dfs -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/hadoop_dfs_missing_dash.py b/thefuck/rules/hadoop_dfs_missing_dash.py new file mode 100644 index 00000000..e525c548 --- /dev/null +++ b/thefuck/rules/hadoop_dfs_missing_dash.py @@ -0,0 +1,10 @@ +def match(command, settings): + return ('hdfs dfs' in command.script + and "this command begins with a dash." in command.stderr.lower()) + + +def get_new_command(command, settings): + data = command.script.split() + data[2] = '-' + data[2] + return ' '.join(data) + From 2cdfe105fb5af5f6cf7c61f70c025fb27ca27e71 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 13 Aug 2015 16:23:11 +0100 Subject: [PATCH 2/4] Reorderd be in alphabetical order --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0197c1f4..d83fbd2b 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `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; +* `hadoop_dfs_missing_dash` – Add the missing dash to the command. * `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; @@ -192,7 +193,6 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `tsuru_not_command` – fixes wrong tsuru commands like `tsuru shell`; * `tmux` – fixes `tmux` commands; * `whois` – fixes `whois` command. -* `hdfs dfs` – Add the missing dash to the command. Enabled by default only on specific platforms: From c08a8bddc9ff448d31da6b3a4c7dc9c9259df9e3 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 13 Aug 2015 17:19:16 +0100 Subject: [PATCH 3/4] More generic a solution, now works with any command that follows the same pattern of error message --- tests/rules/test_hadoop_dfs_missing_dash.py | 11 ++++++++--- thefuck/rules/hadoop_dfs_missing_dash.py | 13 ++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/rules/test_hadoop_dfs_missing_dash.py b/tests/rules/test_hadoop_dfs_missing_dash.py index 8bb659de..d43216f9 100644 --- a/tests/rules/test_hadoop_dfs_missing_dash.py +++ b/tests/rules/test_hadoop_dfs_missing_dash.py @@ -22,9 +22,14 @@ def test_not_match(command): @pytest.mark.parametrize('command, new_command', [ - (Command('hdfs dfs ls'), 'hdfs dfs -ls'), - (Command('hdfs dfs ls /foo/bar'), 'hdfs dfs -ls /foo/bar'), - (Command('./bin/hdfs dfs ls -R /foo/bar'), './bin/hdfs dfs -ls -R /foo/bar')]) + (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/hadoop_dfs_missing_dash.py b/thefuck/rules/hadoop_dfs_missing_dash.py index e525c548..d8632251 100644 --- a/thefuck/rules/hadoop_dfs_missing_dash.py +++ b/thefuck/rules/hadoop_dfs_missing_dash.py @@ -1,10 +1,13 @@ +import re +from thefuck.utils import (replace_command, get_all_matched_commands) + def match(command, settings): - return ('hdfs dfs' in command.script - and "this command begins with a dash." in command.stderr.lower()) + 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): - data = command.script.split() - data[2] = '-' + data[2] - return ' '.join(data) + 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) From 8c9416e57f5ac58d146754bdfb0eca156517817e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 14 Aug 2015 09:38:42 +0100 Subject: [PATCH 4/4] Renamed to unknown command to better match current functionality --- README.md | 2 +- ...{test_hadoop_dfs_missing_dash.py => test_unknown_command.py} | 2 +- .../rules/{hadoop_dfs_missing_dash.py => unknown_command.py} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename tests/rules/{test_hadoop_dfs_missing_dash.py => test_unknown_command.py} (95%) rename thefuck/rules/{hadoop_dfs_missing_dash.py => unknown_command.py} (100%) diff --git a/README.md b/README.md index d83fbd2b..079b54b4 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,6 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `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; -* `hadoop_dfs_missing_dash` – Add the missing dash to the command. * `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; @@ -192,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_hadoop_dfs_missing_dash.py b/tests/rules/test_unknown_command.py similarity index 95% rename from tests/rules/test_hadoop_dfs_missing_dash.py rename to tests/rules/test_unknown_command.py index d43216f9..a31c9d9c 100644 --- a/tests/rules/test_hadoop_dfs_missing_dash.py +++ b/tests/rules/test_unknown_command.py @@ -1,5 +1,5 @@ import pytest -from thefuck.rules.hadoop_dfs_missing_dash import match, get_new_command +from thefuck.rules.unknown_command import match, get_new_command from tests.utils import Command diff --git a/thefuck/rules/hadoop_dfs_missing_dash.py b/thefuck/rules/unknown_command.py similarity index 100% rename from thefuck/rules/hadoop_dfs_missing_dash.py rename to thefuck/rules/unknown_command.py