1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-07 13:41:21 +00:00

fix the rule

This commit is contained in:
Nikos Kakonas 2022-05-19 20:53:43 +00:00
parent 5d03f9f789
commit d898a4df69
2 changed files with 16 additions and 58 deletions

View File

@ -10,13 +10,6 @@ from thefuck.types import Command
[ [
("LS", "command not found"), ("LS", "command not found"),
("LS -A", "command not found"), ("LS -A", "command not found"),
("CD TESTS", "command not found"),
("CD tests", "command not found"),
("CAT README.MD", "command not found"),
("CAT README.md", "command not found"),
("MV TESTS TESTING", "command not found"),
("GIT ADD .", "command not found"),
("GIT add .", "command not found"),
], ],
) )
def test_match(script, output): def test_match(script, output):
@ -39,10 +32,12 @@ def test_not_match(script, output):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"script, output, new_command", "script, output, new_command",
[ [
("LS", "command not found", ["ls"]), ("LS", "command not found", "ls"),
("CD TESTS", "command not found", ["cd tests", "cd TESTS"]), ("CD TESTS", "command not found", "cd tests"),
("CAT README.MD", "command not found", ["cat readme.md", "cat README.MD"]), ("CAT README.MD", "command not found", "cat readme.md"),
("GIT ADD .", "command not found", ["git add ."]), ("GIT ADD .", "command not found", "git add ."),
("GIT ADD .", "command not found", "git add ."),
("MV TESTS TESTING", "command not found", "mv tests testing"),
], ],
) )
def test_get_new_command(script, output, new_command): def test_get_new_command(script, output, new_command):

View File

@ -1,54 +1,17 @@
import subprocess from thefuck.utils import get_all_executables, which
import itertools
def find_all_possible_commands(command):
lower_command = command.script.lower().split()
upper_command = command.script.upper().split()
all_commands = []
for i in lower_command:
all_commands.append(i)
for i in upper_command:
all_commands.append(i)
permutations = itertools.permutations(all_commands, len(lower_command))
new_commands = []
for per in permutations:
new_command = ""
for word in per:
new_command += word + " "
new_command = new_command.strip()
if new_command not in new_commands:
new_commands.append(new_command)
return new_commands
def find_all_correct_commands(new_commands : list):
all_correct_commands = []
for com in new_commands:
result = subprocess.getstatusoutput(com)
if result[0] == 0:
all_correct_commands.append(com)
return all_correct_commands
def match(command): def match(command):
flag = False return (command.script.isupper()
for i in command.script: and not which(command.script_parts[0])
if i.isupper(): and ('not found' in command.output
flag = True or 'is not recognized as' in command.output)
# checks if the command that typed is valid, if it is not it retunrs false and (command.script_parts[0].lower() in get_all_executables()))
# So if we type: SL -A (instead of LS -A) the rule will not be activated
new_commands = find_all_possible_commands(command)
all_correct_commands = find_all_correct_commands(new_commands)
if len(all_correct_commands) == 0:
flag = False
return flag
def get_new_command(command): def get_new_command(cmd):
new_commands = find_all_possible_commands(command) return cmd.script.lower()
all_correct_commands = find_all_correct_commands(new_commands)
return all_correct_commands
priority = 900 requires_output = False
priority = 100