mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-07 13:41:21 +00:00
fix it to work for more cases
This commit is contained in:
parent
506884ef2d
commit
5d03f9f789
@ -333,7 +333,7 @@ following rules are enabled by default:
|
||||
* `tmux` – fixes `tmux` commands;
|
||||
* `unknown_command` – fixes hadoop hdfs-style "unknown command", for example adds missing '-' to the command on `hdfs dfs ls`;
|
||||
* `unsudo` – removes `sudo` from previous command if a process refuses to run on superuser privilege.
|
||||
* `upper_to_lowercase` – replaces the uppercase letters with lowercase, if all the srcipt is typed in uppercase letters;
|
||||
* `upper_to_lower_case` – replaces the uppercase letters with lowercase, so the command is correct;
|
||||
* `vagrant_up` – starts up the vagrant instance;
|
||||
* `whois` – fixes `whois` command;
|
||||
* `workon_doesnt_exists` – fixes `virtualenvwrapper` env name os suggests to create new.
|
||||
|
49
tests/rules/test_upper_to_lower_case.py
Normal file
49
tests/rules/test_upper_to_lower_case.py
Normal file
@ -0,0 +1,49 @@
|
||||
import pytest
|
||||
|
||||
from thefuck.rules.upper_to_lower_case import match, get_new_command
|
||||
from thefuck.types import Command
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output",
|
||||
[
|
||||
("LS", "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):
|
||||
assert match(Command(script, output))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output",
|
||||
[
|
||||
("ls", ""),
|
||||
("cd thefuck", ""),
|
||||
("mv tests testing", ""),
|
||||
("git add .", ""),
|
||||
],
|
||||
)
|
||||
def test_not_match(script, output):
|
||||
assert not match(Command(script, output))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output, new_command",
|
||||
[
|
||||
("LS", "command not found", ["ls"]),
|
||||
("CD TESTS", "command not found", ["cd tests", "cd TESTS"]),
|
||||
("CAT README.MD", "command not found", ["cat readme.md", "cat README.MD"]),
|
||||
("GIT ADD .", "command not found", ["git add ."]),
|
||||
],
|
||||
)
|
||||
def test_get_new_command(script, output, new_command):
|
||||
assert get_new_command(Command(script, output)) == new_command
|
@ -1,47 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from thefuck.rules.upper_to_lowercase import match, get_new_command
|
||||
from thefuck.types import Command
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output",
|
||||
[
|
||||
("LS", ""),
|
||||
("CD THEFUCK", ""),
|
||||
("CAT README.MD", ""),
|
||||
("MV TESTS TESTING", ""),
|
||||
("GIT ADD .", ""),
|
||||
],
|
||||
)
|
||||
def test_match(script, output):
|
||||
assert match(Command(script, output))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output",
|
||||
[
|
||||
("ls", ""),
|
||||
("cd thefuck", ""),
|
||||
("cat README.md", ""),
|
||||
("mv tests testing", ""),
|
||||
("git add .", ""),
|
||||
],
|
||||
)
|
||||
def test_not_match(script, output):
|
||||
assert not match(Command(script, output))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script, output, new_command",
|
||||
[
|
||||
("LS", "", "ls"),
|
||||
("CD THEFUCK", "", "cd thefuck"),
|
||||
("CAT README.MD", "", "cat readme.md"),
|
||||
("MV TESTS TESTING", "", "mv tests testing"),
|
||||
("GIT ADD .", "", "git add ."),
|
||||
],
|
||||
)
|
||||
def test_get_new_command(script, output, new_command):
|
||||
assert get_new_command(Command(script, output)) == new_command
|
54
thefuck/rules/upper_to_lower_case.py
Normal file
54
thefuck/rules/upper_to_lower_case.py
Normal file
@ -0,0 +1,54 @@
|
||||
import subprocess
|
||||
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):
|
||||
flag = False
|
||||
for i in command.script:
|
||||
if i.isupper():
|
||||
flag = True
|
||||
# checks if the command that typed is valid, if it is not it retunrs false
|
||||
# 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):
|
||||
new_commands = find_all_possible_commands(command)
|
||||
all_correct_commands = find_all_correct_commands(new_commands)
|
||||
return all_correct_commands
|
||||
|
||||
|
||||
priority = 900
|
@ -1,8 +0,0 @@
|
||||
def match(command):
|
||||
return command.script.isupper()
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
return command.script.lower()
|
||||
|
||||
priority = 0
|
Loading…
x
Reference in New Issue
Block a user