From 55dcf0656932faf68ecb8e88e9dcb5ee54aab587 Mon Sep 17 00:00:00 2001 From: dhilipsiva Date: Sun, 5 Feb 2017 14:50:36 +0530 Subject: [PATCH 1/5] Hostscli --- tests/rules/test_hostscli.py | 65 ++++++++++++++++++++++++++++++++++++ thefuck/rules/hostscli.py | 37 ++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/rules/test_hostscli.py create mode 100644 thefuck/rules/hostscli.py diff --git a/tests/rules/test_hostscli.py b/tests/rules/test_hostscli.py new file mode 100644 index 00000000..1736d43e --- /dev/null +++ b/tests/rules/test_hostscli.py @@ -0,0 +1,65 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# vim: fenc=utf-8 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# +# + +""" +File name: test_hostscli.py +Author: dhilipsiva +Date created: 2017-02-05 +""" + +import pytest +from thefuck.rules.hostscli import no_command, need_sudo, no_website, \ + match, get_new_command +from tests.utils import Command + + +no_command = ''' +Usage: hostscli [OPTIONS] COMMAND [ARGS]... + +%s "invalid". +''' % no_command + +need_sudo = ''' +%s: + +"sudo" permissions are required to run this command. + +Please run the last command again with sudo +''' % need_sudo + + +no_website = ''' +%s: + +No Domain list found for website: a_website_that_does_not_exist + +Please raise a Issue here: https://github.com/dhilipsiva/hostscli/issues/new +if you think we should add domains for this website. + +type `hostscli websites` to see a list of websites that you can block/unblock +''' % no_website + + +@pytest.mark.parametrize('command', [ + Command('hostscli invalid', stderr=no_command)]) +def test_match(command): + assert match(command) + + +@pytest.mark.parametrize('command, result', [ + (Command( + 'hostscli invalid', stderr=no_command), ['hostscli --help']), + (Command( + 'hostscli block test', stderr=need_sudo), + ['sudo hostscli block test']), + (Command( + 'sudo hostscli block a_website_that_does_not_exist', + stderr=no_website), + ['hostscli websites'])]) +def test_get_new_command(command, result): + assert get_new_command(command) == result diff --git a/thefuck/rules/hostscli.py b/thefuck/rules/hostscli.py new file mode 100644 index 00000000..7f3bd6fb --- /dev/null +++ b/thefuck/rules/hostscli.py @@ -0,0 +1,37 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# vim: fenc=utf-8 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# +# + +""" +File name: hostscli.py +Author: dhilipsiva +Date created: 2017-02-05 +""" + +from thefuck.utils import for_app + +no_command = "Error: No such command" +need_sudo = "hostscli.errors.SudoRequiredError" +no_website = "hostscli.errors.WebsiteImportError" + + +@for_app("hostscli") +def match(command): + errors = [no_command, need_sudo, no_website] + for error in errors: + if error in command.stderr: + return True + return False + + +def get_new_command(command): + if no_website in command.stderr: + return ['hostscli websites'] + if need_sudo in command.stderr: + return ['sudo {}'.format(command.script)] + if no_command in command.stderr: + return ['hostscli --help'] From fb07cdfb4a2eb22746c9aa53edf3849e9622562c Mon Sep 17 00:00:00 2001 From: dhilipsiva Date: Sun, 5 Feb 2017 14:58:36 +0530 Subject: [PATCH 2/5] Add hostscli to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0aeae4f1..3fc614c6 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `has_exists_script` – prepends `./` when script/binary exists; * `heroku_not_command` – fixes wrong `heroku` commands like `heroku log`; * `history` – tries to replace command with most similar command from history; +* `hostscli` – tries to fix `hostscli` usage; * `ifconfig_device_not_found` – fixes wrong device names like `wlan0` to `wlp2s0`; * `java` – removes `.java` extension when running Java programs; * `javac` – appends missing `.java` when compiling Java files; From 7f9025c7ad220c155afbefa83c3cd4df666d28e7 Mon Sep 17 00:00:00 2001 From: dhilipsiva Date: Thu, 9 Feb 2017 08:46:54 +0530 Subject: [PATCH 3/5] Make Changes suggested by @nvbn * remove comments/doctrings at the top of files; * move sudo-related stuff to sudo rule; * for no_command case try to find most similar command, like, for example, in react_native_command_unrecognized rule. --- tests/rules/test_hostscli.py | 29 ++--------------------------- thefuck/rules/hostscli.py | 27 +++++++-------------------- thefuck/rules/sudo.py | 3 ++- 3 files changed, 11 insertions(+), 48 deletions(-) diff --git a/tests/rules/test_hostscli.py b/tests/rules/test_hostscli.py index 1736d43e..1c6cc5cf 100644 --- a/tests/rules/test_hostscli.py +++ b/tests/rules/test_hostscli.py @@ -1,20 +1,6 @@ -#! /usr/bin/env python -# -*- coding: utf-8 -*- -# -# vim: fenc=utf-8 -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -# -# - -""" -File name: test_hostscli.py -Author: dhilipsiva -Date created: 2017-02-05 -""" - import pytest -from thefuck.rules.hostscli import no_command, need_sudo, no_website, \ - match, get_new_command +from thefuck.rules.hostscli import no_command, no_website, get_new_command, \ + match from tests.utils import Command @@ -24,14 +10,6 @@ Usage: hostscli [OPTIONS] COMMAND [ARGS]... %s "invalid". ''' % no_command -need_sudo = ''' -%s: - -"sudo" permissions are required to run this command. - -Please run the last command again with sudo -''' % need_sudo - no_website = ''' %s: @@ -54,9 +32,6 @@ def test_match(command): @pytest.mark.parametrize('command, result', [ (Command( 'hostscli invalid', stderr=no_command), ['hostscli --help']), - (Command( - 'hostscli block test', stderr=need_sudo), - ['sudo hostscli block test']), (Command( 'sudo hostscli block a_website_that_does_not_exist', stderr=no_website), diff --git a/thefuck/rules/hostscli.py b/thefuck/rules/hostscli.py index 7f3bd6fb..d62b6437 100644 --- a/thefuck/rules/hostscli.py +++ b/thefuck/rules/hostscli.py @@ -1,27 +1,14 @@ -#! /usr/bin/env python -# -*- coding: utf-8 -*- -# -# vim: fenc=utf-8 -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -# -# +import re -""" -File name: hostscli.py -Author: dhilipsiva -Date created: 2017-02-05 -""" - -from thefuck.utils import for_app +from thefuck.utils import for_app, replace_command no_command = "Error: No such command" -need_sudo = "hostscli.errors.SudoRequiredError" no_website = "hostscli.errors.WebsiteImportError" @for_app("hostscli") def match(command): - errors = [no_command, need_sudo, no_website] + errors = [no_command, no_website] for error in errors: if error in command.stderr: return True @@ -31,7 +18,7 @@ def match(command): def get_new_command(command): if no_website in command.stderr: return ['hostscli websites'] - if need_sudo in command.stderr: - return ['sudo {}'.format(command.script)] - if no_command in command.stderr: - return ['hostscli --help'] + misspelled_command = re.findall( + r'Error: No such command "(.*)"', command.stderr)[0] + commands = ['block', 'unblock', 'websites', 'block_all', 'unblock_all'] + return replace_command(command, misspelled_command, commands) diff --git a/thefuck/rules/sudo.py b/thefuck/rules/sudo.py index 91e5f4bb..0f85168a 100644 --- a/thefuck/rules/sudo.py +++ b/thefuck/rules/sudo.py @@ -20,7 +20,8 @@ patterns = ['permission denied', 'authentication is required', 'edspermissionerror', 'you don\'t have write permissions', - 'use `sudo`'] + 'use `sudo`', + 'SudoRequiredError'] def match(command): From 8a8ade1e6b327a09301e9801e630ddedfcf5c376 Mon Sep 17 00:00:00 2001 From: dhilipsiva Date: Thu, 9 Feb 2017 08:52:20 +0530 Subject: [PATCH 4/5] Fix regex --- thefuck/rules/hostscli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thefuck/rules/hostscli.py b/thefuck/rules/hostscli.py index d62b6437..9c82d7ae 100644 --- a/thefuck/rules/hostscli.py +++ b/thefuck/rules/hostscli.py @@ -19,6 +19,6 @@ def get_new_command(command): if no_website in command.stderr: return ['hostscli websites'] misspelled_command = re.findall( - r'Error: No such command "(.*)"', command.stderr)[0] + r'Error: No such command ".*"', command.stderr)[0] commands = ['block', 'unblock', 'websites', 'block_all', 'unblock_all'] return replace_command(command, misspelled_command, commands) From 309fe8f6ee5ccc014886ee0725506ea97d945a9e Mon Sep 17 00:00:00 2001 From: dhilipsiva Date: Thu, 9 Feb 2017 09:16:20 +0530 Subject: [PATCH 5/5] Fix test cases --- tests/rules/test_hostscli.py | 25 ++++++++----------------- thefuck/rules/hostscli.py | 3 +-- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/tests/rules/test_hostscli.py b/tests/rules/test_hostscli.py index 1c6cc5cf..740af43b 100644 --- a/tests/rules/test_hostscli.py +++ b/tests/rules/test_hostscli.py @@ -1,17 +1,9 @@ import pytest -from thefuck.rules.hostscli import no_command, no_website, get_new_command, \ - match +from thefuck.rules.hostscli import no_website, get_new_command, match from tests.utils import Command -no_command = ''' -Usage: hostscli [OPTIONS] COMMAND [ARGS]... - -%s "invalid". -''' % no_command - - -no_website = ''' +no_website_long = ''' %s: No Domain list found for website: a_website_that_does_not_exist @@ -24,17 +16,16 @@ type `hostscli websites` to see a list of websites that you can block/unblock @pytest.mark.parametrize('command', [ - Command('hostscli invalid', stderr=no_command)]) + Command( + 'sudo hostscli block a_website_that_does_not_exist', + stderr=no_website_long)]) def test_match(command): assert match(command) -@pytest.mark.parametrize('command, result', [ - (Command( - 'hostscli invalid', stderr=no_command), ['hostscli --help']), - (Command( +@pytest.mark.parametrize('command, result', [( + Command( 'sudo hostscli block a_website_that_does_not_exist', - stderr=no_website), - ['hostscli websites'])]) + stderr=no_website_long), ['hostscli websites'])]) def test_get_new_command(command, result): assert get_new_command(command) == result diff --git a/thefuck/rules/hostscli.py b/thefuck/rules/hostscli.py index 9c82d7ae..eea6a9ef 100644 --- a/thefuck/rules/hostscli.py +++ b/thefuck/rules/hostscli.py @@ -1,12 +1,11 @@ import re -from thefuck.utils import for_app, replace_command +from thefuck.utils import replace_command no_command = "Error: No such command" no_website = "hostscli.errors.WebsiteImportError" -@for_app("hostscli") def match(command): errors = [no_command, no_website] for error in errors: