Skip to content

Commit

Permalink
Add terraform 'no command' rule (#1317)
Browse files Browse the repository at this point in the history
* Add terraform 'no command' rule

* Feedback from PR

Co-authored-by: Joseph Daniel <jdn@logpoint.com>
  • Loading branch information
josepdaniel and Joseph Daniel committed Sep 25, 2022
1 parent 77627a3 commit ceeaeab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -332,6 +332,7 @@ following rules are enabled by default:
* `switch_lang` &ndash; switches command from your local layout to en;
* `systemctl` &ndash; correctly orders parameters of confusing `systemctl`;
* `terraform_init.py` &ndash; run `terraform init` before plan or apply;
* `terraform_no_command.py` &ndash; fixes unrecognized `terraform` commands;
* `test.py` &ndash; runs `py.test` instead of `test.py`;
* `touch` &ndash; creates missing directories before "touching";
* `tsuru_login` &ndash; runs `tsuru login` if not authenticated or session expired;
Expand Down
27 changes: 27 additions & 0 deletions 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
16 changes: 16 additions & 0 deletions 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)

0 comments on commit ceeaeab

Please sign in to comment.