From ceeaeab94b5df5a4fe9d94d61e4f6b0bbea96378 Mon Sep 17 00:00:00 2001 From: josepdaniel <36941460+josepdaniel@users.noreply.github.com> Date: Sun, 25 Sep 2022 21:37:47 +0200 Subject: [PATCH] Add terraform 'no command' rule (#1317) * Add terraform 'no command' rule * Feedback from PR Co-authored-by: Joseph Daniel --- README.md | 1 + tests/rules/test_terraform_no_command.py | 27 ++++++++++++++++++++++++ thefuck/rules/terraform_no_command.py | 16 ++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/rules/test_terraform_no_command.py create mode 100644 thefuck/rules/terraform_no_command.py diff --git a/README.md b/README.md index a2becd349..3ea406fcf 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,7 @@ following rules are enabled by default: * `switch_lang` – switches command from your local layout to en; * `systemctl` – correctly orders parameters of confusing `systemctl`; * `terraform_init.py` – run `terraform init` before plan or apply; +* `terraform_no_command.py` – fixes unrecognized `terraform` commands; * `test.py` – runs `py.test` instead of `test.py`; * `touch` – creates missing directories before "touching"; * `tsuru_login` – runs `tsuru login` if not authenticated or session expired; diff --git a/tests/rules/test_terraform_no_command.py b/tests/rules/test_terraform_no_command.py new file mode 100644 index 000000000..9a45f65df --- /dev/null +++ b/tests/rules/test_terraform_no_command.py @@ -0,0 +1,27 @@ +import pytest +from thefuck.rules.terraform_no_command import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('script, output', [ + ('terraform appyl', 'Terraform has no command named "appyl". Did you mean "apply"?'), + ('terraform destory', 'Terraform has no command named "destory". Did you mean "destroy"?')]) +def test_match(script, output): + assert match(Command(script, output)) + + +@pytest.mark.parametrize('script, output', [ + ('terraform --version', 'Terraform v0.12.2'), + ('terraform plan', 'No changes. Infrastructure is up-to-date.'), + ('terraform apply', 'Apply complete! Resources: 0 added, 0 changed, 0 destroyed.'), +]) +def test_not_match(script, output): + assert not match(Command(script, output)) + + +@pytest.mark.parametrize('script, output, new_command', [ + ('terraform appyl', 'Terraform has no command named "appyl". Did you mean "apply"?', 'terraform apply',), + ('terraform destory --some-other-option', 'Terraform has no command named "destory". Did you mean "destroy"?', 'terraform destroy --some-other-option',), +]) +def test_get_new_command(script, output, new_command): + assert get_new_command(Command(script, output)) == new_command diff --git a/thefuck/rules/terraform_no_command.py b/thefuck/rules/terraform_no_command.py new file mode 100644 index 000000000..6cbb9a9a2 --- /dev/null +++ b/thefuck/rules/terraform_no_command.py @@ -0,0 +1,16 @@ +import re +from thefuck.utils import for_app + +MISTAKE = r'(?<=Terraform has no command named ")([^"]+)(?="\.)' +FIX = r'(?<=Did you mean ")([^"]+)(?="\?)' + + +@for_app('terraform') +def match(command): + return re.search(MISTAKE, command.output) and re.search(FIX, command.output) + + +def get_new_command(command): + mistake = re.search(MISTAKE, command.output).group(0) + fix = re.search(FIX, command.output).group(0) + return command.script.replace(mistake, fix)