diff --git a/README.md b/README.md index 48b4b0f..02bae98 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/tests/rules/test_mdt.py b/tests/rules/test_mdt.py new file mode 100644 index 0000000..4249818 --- /dev/null +++ b/tests/rules/test_mdt.py @@ -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 diff --git a/thefuck/rules/mdt.py b/thefuck/rules/mdt.py new file mode 100644 index 0000000..435369f --- /dev/null +++ b/thefuck/rules/mdt.py @@ -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]