This commit is contained in:
Nikos Kakonas 2023-10-14 21:26:29 -03:00 committed by GitHub
commit b38ae2f78c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 0 deletions

View File

@ -331,6 +331,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;
* `terraform_no_command.py` – fixes unrecognized `terraform` commands;
* `test.py` – runs `pytest` instead of `test.py`;

View File

@ -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

View File

@ -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