Back to Repositories

Testing Terraform Init Rule Implementation in thefuck

This test suite validates the Terraform initialization rule functionality in the ‘thefuck’ command-line tool, ensuring proper handling of terraform init requirements before plan and apply operations.

Test Coverage Overview

The test suite comprehensively covers Terraform command initialization scenarios, focusing on detecting when ‘terraform init’ is required.

  • Tests both terraform plan and apply commands
  • Validates initialization requirement detection
  • Covers multiple error message patterns
  • Includes negative test cases for non-matching scenarios

Implementation Analysis

The implementation uses pytest’s parametrize feature for efficient test case management, enabling thorough validation of different command combinations and outputs.

  • Parametrized test cases for matching and non-matching scenarios
  • Command object usage for input/output simulation
  • Boolean assertion patterns for match verification

Technical Details

  • Framework: pytest
  • Test Components: match() and get_new_command() functions
  • Command Class: Custom Command type for input/output handling
  • Pattern Matching: String-based error detection

Best Practices Demonstrated

The test suite exemplifies clean testing practices through organized test case separation and comprehensive scenario coverage.

  • Clear test function naming
  • Parametrized test cases for maintainability
  • Separate positive and negative test cases
  • Consistent assertion patterns

nvbn/thefuck

tests/rules/test_terraform_init.py

            
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