helper

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package helper provides testing utilities for tfbreak plugins. Use TestRunner to test rules without running tfbreak-core.

Example:

func TestMyRule(t *testing.T) {
    runner := helper.TestRunner(t,
        map[string]string{"main.tf": `resource "azurerm_rg" "test" { location = "westus" }`},
        map[string]string{"main.tf": `resource "azurerm_rg" "test" { location = "eastus" }`},
    )

    rule := &MyRule{}
    if err := rule.Check(runner); err != nil {
        t.Fatal(err)
    }

    helper.AssertIssues(t, helper.Issues{
        {Rule: rule, Message: "location changed"},
    }, runner.Issues)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertIssues

func AssertIssues(t *testing.T, want, got Issues)

AssertIssues compares expected and actual issues. It ignores issue order and byte positions in ranges.

Example:

helper.AssertIssues(t, helper.Issues{
    {Rule: rule, Message: "location changed"},
}, runner.Issues)

func AssertIssuesWithoutRange

func AssertIssuesWithoutRange(t *testing.T, want, got Issues)

AssertIssuesWithoutRange compares issues ignoring the Range field entirely. Use this when exact source locations are not important for the test.

Example:

helper.AssertIssuesWithoutRange(t, helper.Issues{
    {Rule: rule, Message: "location changed"},
}, runner.Issues)

func AssertNoIssues

func AssertNoIssues(t *testing.T, got Issues)

AssertNoIssues verifies that no issues were emitted.

Types

type Issue

type Issue struct {
	// Rule is the rule that emitted the issue.
	Rule tflint.Rule
	// Message is the issue message.
	Message string
	// Range is the source location of the issue.
	Range hcl.Range
}

Issue represents a finding from a rule for test assertions.

type Issues

type Issues []Issue

Issues is a slice of Issue for convenience.

type Runner

type Runner struct {

	// Issues contains all issues emitted during rule execution.
	Issues Issues
	// contains filtered or unexported fields
}

Runner is a mock tflint.Runner for testing. Use TestRunner to create an instance.

func TestRunner

func TestRunner(t *testing.T, oldFiles, newFiles map[string]string) *Runner

TestRunner creates a new Runner for testing.

DEVIATION FROM TFLINT (see ADR-0001): Unlike tflint's helper.TestRunner which takes a single file map, tfbreak's TestRunner takes two maps: old (baseline) and new (changed).

Example:

runner := helper.TestRunner(t,
    map[string]string{
        "main.tf": `resource "azurerm_resource_group" "rg" { location = "westus" }`,
    },
    map[string]string{
        "main.tf": `resource "azurerm_resource_group" "rg" { location = "eastus" }`,
    },
)

rule := &MyRule{}
rule.Check(runner)
helper.AssertIssues(t, expected, runner.Issues)

func (*Runner) DecodeRuleConfig

func (r *Runner) DecodeRuleConfig(_ string, _ any) error

DecodeRuleConfig decodes rule configuration. This is a stub implementation that always returns nil (no config).

func (*Runner) EmitIssue

func (r *Runner) EmitIssue(rule tflint.Rule, message string, issueRange hcl.Range) error

EmitIssue records an issue.

func (*Runner) GetNewModuleContent

func (r *Runner) GetNewModuleContent(schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)

GetNewModuleContent retrieves content from new files.

func (*Runner) GetNewResourceContent

func (r *Runner) GetNewResourceContent(resourceType string, schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)

GetNewResourceContent retrieves resources of a specific type from new files.

func (*Runner) GetOldModuleContent

func (r *Runner) GetOldModuleContent(schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)

GetOldModuleContent retrieves content from old files.

func (*Runner) GetOldResourceContent

func (r *Runner) GetOldResourceContent(resourceType string, schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)

GetOldResourceContent retrieves resources of a specific type from old files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL