Back to Repositories

Testing Terraform Command Correction Logic in thefuck

This test suite validates Terraform command correction functionality in the thefuck utility, focusing on handling misspelled Terraform commands and providing appropriate corrections. The tests ensure accurate command matching and replacement behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of Terraform command correction scenarios.

Key areas tested include:
  • Command misspelling detection (e.g. ‘appyl’ → ‘apply’)
  • Command correction suggestions
  • Handling of valid Terraform commands
  • Preservation of command options during correction

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for data-driven testing, enabling efficient validation of multiple test cases.

Testing patterns include:
  • Separate test functions for matching and command generation
  • Explicit positive and negative test cases
  • Command object usage for input/output handling

Technical Details

Testing components:
  • pytest framework for test organization
  • Command class from thefuck.types
  • Parametrized test cases for varied scenarios
  • match() and get_new_command() function testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Comprehensive positive and negative test cases
  • Clear test function naming and organization
  • Efficient use of parametrized testing
  • Isolated function testing
  • Explicit assertion statements

nvbn/thefuck

tests/rules/test_terraform_no_command.py

            
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