From e53cd85558129338574c6564484ff8e07bee0def Mon Sep 17 00:00:00 2001 From: Nikos Kakonas Date: Thu, 5 May 2022 05:26:40 +0300 Subject: [PATCH] add rule tar_missed_argument --- README.md | 1 + tests/rules/test_tar_missed_argument.py | 86 +++++++++++++++++++++++++ thefuck/rules/tar_missed_argument.py | 37 +++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/rules/test_tar_missed_argument.py create mode 100644 thefuck/rules/tar_missed_argument.py diff --git a/README.md b/README.md index 0a10605..e22203c 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,7 @@ following rules are enabled by default: * `sudo_command_from_user_path` – runs commands from users `$PATH` with `sudo`; * `switch_lang` – switches command from your local layout to en; * `systemctl` – correctly orders parameters of confusing `systemctl`; +* `tar_missed_arguments` – add arguments to the command tar if they do not exist (e.g -cvf or -xvf); * `terraform_init.py` – run `terraform init` before plan or apply; * `test.py` – runs `py.test` instead of `test.py`; * `touch` – creates missing directories before "touching"; diff --git a/tests/rules/test_tar_missed_argument.py b/tests/rules/test_tar_missed_argument.py new file mode 100644 index 0000000..bb25ad0 --- /dev/null +++ b/tests/rules/test_tar_missed_argument.py @@ -0,0 +1,86 @@ +import pytest + +from thefuck.rules.tar_missed_argument import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize( + "script, output", + [ + ("tar tarname.tar /workspaces/", "tar: invalid option -- '.'"), + + ("tar tar_name.tar", + "Try 'tar --help' or 'tar --usage' for more information."), + + ("tar name_tar.tar /workspaces/thefuck/rules", + "tar: invalid option -- '.'\nTry 'tar --help' or 'tar --usage' for more information."), + ], +) +def test_match(script, output): + assert match(Command(script, output)) + + +@pytest.mark.parametrize( + "script, output", + [ + ("python release.py", ""), + + ("tar tarname.tar /workspaces/", ""), + + ("tar -cvf tar_name.tar", "tar: Cowardly refusing to create an empty archive"), + + ("tar -cvfj /workspaces/thefuck", + "Try 'tar --help' or 'tar --usage' for more information."), + + ("tar -cvzf /workspaces/thefuck/rules", + "tar: invalid option -- '.'\nTry 'tar --help' or 'tar --usage' for more information."), + + ("tar -jxvf tar_name.tar", "tar: Cowardly refusing to create an empty archive"), + + ("tar -xvf /workspaces/thefuck", + "Try 'tar --help' or 'tar --usage' for more information."), + + ("tar -zxvf /workspaces/thefuck/rules", + "tar: invalid option -- '.'\nTry 'tar --help' or 'tar --usage' for more information."), + ], +) +def test_not_match(script, output): + assert not match(Command(script, output)) + + +@pytest.mark.parametrize( + "script, output, new_command", + [ + ("tar tar_name.tar /workspaces", + "tar: You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option", + [ + "tar -cvf tar_name.tar /workspaces", "tar -cvfj tar_name.tar /workspaces", + "tar -cvzf tar_name.tar /workspaces", "tar -jxvf tar_name.tar /workspaces", + "tar -xvf tar_name.tar /workspaces", "tar -zxvf tar_name.tar /workspaces" + ]), + + ("tar tar_name.tar /workspaces/thefuck", + "Try 'tar --help' or 'tar --usage' for more information.", + [ + "tar -cvf tar_name.tar /workspaces/thefuck", + "tar -cvfj tar_name.tar /workspaces/thefuck", + "tar -cvzf tar_name.tar /workspaces/thefuck", + "tar -jxvf tar_name.tar /workspaces/thefuck", + "tar -xvf tar_name.tar /workspaces/thefuck", + "tar -zxvf tar_name.tar /workspaces/thefuck" + ]), + + ("tar tar_name.tar /workspaces/thefuck/rules", + "tar: invalid option -- '.'\nTry 'tar --help' or 'tar --usage' for more information.", + [ + "tar -cvf tar_name.tar /workspaces/thefuck/rules", + "tar -cvfj tar_name.tar /workspaces/thefuck/rules", + "tar -cvzf tar_name.tar /workspaces/thefuck/rules", + "tar -jxvf tar_name.tar /workspaces/thefuck/rules", + "tar -xvf tar_name.tar /workspaces/thefuck/rules", + "tar -zxvf tar_name.tar /workspaces/thefuck/rules" + ]), + ], +) +def test_get_new_command(script, output, new_command): + assert get_new_command(Command(script, output)) == new_command diff --git a/thefuck/rules/tar_missed_argument.py b/thefuck/rules/tar_missed_argument.py new file mode 100644 index 0000000..215d628 --- /dev/null +++ b/thefuck/rules/tar_missed_argument.py @@ -0,0 +1,37 @@ +# The purpose of this rule is to add arguments to the command tar if the user +# forgets to type them or he / she does not know that they exist. + +# This is a list with some possible arguments, which come after the command tar. +arguments = [ + "-cvf", + "-cvfj", + "-cvzf", + "-jxvf", + "-xvf", + "-zxvf" +] + + +# This fucntion: + +# checks if '-' exist in the command.script. If there is the rule will not work. +# But if it does not exist, this means that the user has forgot to type an argupent +# after the command tar. + +# checks if the word 'tar' exists in the command.script, because the rule is working +# only for the command tar. + +# checks if the typed command is invalid, in order to correct it. +def match(command): + return ('-' not in command.script and 'tar' in command.script and + ('invalid option' in command.output or 'try' in command.output or + 'Try' in command.output or 'tar' in command.output)) + + +# This function returns some possible arguments that can be typed after the command tar. +def get_new_command(command): + new_commands = [] + for arg in arguments: + new_commands.append( + command.script[:4] + arg + " " + command.script[4:]) + return new_commands