diff --git a/README.md b/README.md index 26cf0d0f..d9424dfd 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,7 @@ following rules are enabled by default: * `sudo_command_from_user_path` – runs commands from users `$PATH` with `sudo`; * `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; * `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_init.py b/tests/rules/test_terraform_init.py new file mode 100644 index 00000000..1bd744c2 --- /dev/null +++ b/tests/rules/test_terraform_init.py @@ -0,0 +1,33 @@ +import pytest +from thefuck.rules.terraform_init import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('script, output', [ + ('terraform plan', 'Error: Initialization required. ' + 'Please see the error message above.'), + ('terraform plan', 'This module is not yet installed. Run "terraform init" ' + 'to install all modules required by this configuration.'), + ('terraform apply', 'Error: Initialization required. ' + 'Please see the error message above.'), + ('terraform apply', 'This module is not yet installed. Run "terraform init" ' + 'to install all modules required by this configuration.')]) +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=output)) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('terraform plan', ''), 'terraform init && terraform plan'), + (Command('terraform apply', ''), 'terraform init && terraform apply'), +]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/terraform_init.py b/thefuck/rules/terraform_init.py new file mode 100644 index 00000000..29dd44fd --- /dev/null +++ b/thefuck/rules/terraform_init.py @@ -0,0 +1,13 @@ +from thefuck.shells import shell +from thefuck.utils import for_app + + +@for_app('terraform') +def match(command): + return ('this module is not yet installed' in command.output.lower() or + 'initialization required' in command.output.lower() + ) + + +def get_new_command(command): + return shell.and_('terraform init', command.script)