Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mdt #1378

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ following rules are enabled by default:
* `ls_lah` – adds `-lah` to `ls`;
* `man` – changes manual section;
* `man_no_space` – fixes man commands without spaces, for example `mandiff`;
* `mdt` – fixes misspellings in mdt commands like `mdt shll`;
* `mercurial` – fixes wrong `hg` commands;
* `missing_space_before_subcommand` – fixes command with missing space like `npminstall`;
* `mkdir_p` – adds `-p` when you try to create a directory without a parent;
Expand Down
37 changes: 37 additions & 0 deletions tests/rules/test_mdt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from thefuck.rules.mdt import match, get_new_command
from thefuck.types import Command

output_unknown_shell = """Unknown command 'shll': try 'mdt help'"""

output_unknown_devices = """Unknown command 'dvices': try 'mdt help'"""

output_unknown_reboot = """Unknown command 'rboot': try 'mdt help'"""

output_unknown_version = """Unknown command 'verson': try 'mdt help'"""

output_unknown_wait = """Unknown command 'wai-for-dvice': try 'mdt help'"""


@pytest.mark.parametrize('command', [
Command('mdt shll', output_unknown_shell),
Command('mdt dvices', output_unknown_devices),
Command('mdt rboot', output_unknown_reboot),
Command('mdt verson', output_unknown_version),
Command('mdt wai-for-dvice', output_unknown_wait)
])
def test_match(command):
# check mdt detection
assert match(command)


@pytest.mark.parametrize('command, new_command', [
(Command('mdt shll', output_unknown_shell), 'mdt shell'),
(Command('mdt dvices', output_unknown_devices), 'mdt devices'),
(Command('mdt rboot', output_unknown_reboot), 'mdt reboot'),
(Command('mdt verson', output_unknown_version), 'mdt version'),
(Command('mdt wai-for-dvice', output_unknown_wait), 'mdt wait-for-device')
])
def test_get_new_command(command, new_command):
# check first correction
assert get_new_command(command)[0] == new_command
30 changes: 30 additions & 0 deletions thefuck/rules/mdt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re
from thefuck.utils import for_app


@for_app('mdt')
def match(command):
return "Unknown command" in command.output and "try 'mdt help'" in command.output


def get_new_command(command):
corrections = ["help"]

""" Extract what the user typed in"""
command = str(command)
extracted_command = re.findall(r"'(.*?)'", command)[0]

""" Find possible matches in the case of typos"""
if re.match('[shell]{2,}', extracted_command):
corrections.insert(0, "shell")
elif re.match('[devices]{3,}', extracted_command):
corrections.insert(0, "devices")
elif re.match('[reboot]{2,}', extracted_command):
corrections.insert(0, "reboot")
corrections.insert(1, "reboot-bootloader")
elif re.match('[version]{3,}', extracted_command):
corrections.insert(0, "version")
elif re.match('[wait\-for\-device]{3,}', extracted_command):
corrections.insert(0, "wait-for-device")

return ["mdt " + correction for correction in corrections]