From c816c8e45b24ba9acbf1c6b35602155f31fb2969 Mon Sep 17 00:00:00 2001 From: Saiwing Yeung Date: Fri, 5 Nov 2021 18:24:07 -0700 Subject: [PATCH] Add rule for restic * Detect typos recognized by restic and add fixed command for it * Add test cases for the restic rule * Update README.md --- README.md | 1 + tests/rules/test_restic_typo.py | 22 ++++++++++++++++++++++ thefuck/rules/restic_typo.py | 14 ++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/rules/test_restic_typo.py create mode 100644 thefuck/rules/restic_typo.py diff --git a/README.md b/README.md index 182acce..bf3223b 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ following rules are enabled by default: * `react_native_command_unrecognized` – fixes unrecognized `react-native` commands; * `remove_shell_prompt_literal` – remove leading shell prompt symbol `$`, common when copying commands from documentations; * `remove_trailing_cedilla` – remove trailing cedillas `รง`, a common typo for European keyboard layouts; +* `restic` – fixes restic commands; * `rm_dir` – adds `-rf` when you try to remove a directory; * `scm_correction` – corrects wrong scm like `hg log` to `git log`; * `sed_unterminated_s` – adds missing '/' to `sed`'s `s` commands; diff --git a/tests/rules/test_restic_typo.py b/tests/rules/test_restic_typo.py new file mode 100644 index 0000000..20aeea5 --- /dev/null +++ b/tests/rules/test_restic_typo.py @@ -0,0 +1,22 @@ +import pytest + +from thefuck.rules.restic_typo import match, get_new_command +from thefuck.types import Command + + +@pytest.fixture +def mistype_suggestion(): + return """ +unknown command "snapshot" for "restic" + +Did you mean this? + snapshots + """ + + +def test_match(mistype_suggestion): + assert match(Command('restic snapshot', mistype_suggestion)) + + +def test_get_new_command(mistype_suggestion): + assert (get_new_command(Command('restic snapshot', mistype_suggestion)) == ['restic snapshots']) diff --git a/thefuck/rules/restic_typo.py b/thefuck/rules/restic_typo.py new file mode 100644 index 0000000..48f12de --- /dev/null +++ b/thefuck/rules/restic_typo.py @@ -0,0 +1,14 @@ +import re +from thefuck.utils import replace_command, for_app + + +@for_app("restic") +def match(command): + return (re.search('unknown command ".*" for "restic"', command.output) + and "Did you mean this" in command.output) + + +def get_new_command(command): + broken_cmd = re.findall(r"(?<=unknown command \")([a-z\-]+)", command.output)[0] + correct_cmd = re.findall(r"([a-z\-]+)", command.output)[-1] + return replace_command(command, broken_cmd, [correct_cmd])