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 ¶
- func AssertIssues(t *testing.T, want, got Issues)
- func AssertIssuesWithoutRange(t *testing.T, want, got Issues)
- func AssertNoIssues(t *testing.T, got Issues)
- type Issue
- type Issues
- type Runner
- func (r *Runner) DecodeRuleConfig(_ string, _ any) error
- func (r *Runner) EmitIssue(rule tflint.Rule, message string, issueRange hcl.Range) error
- func (r *Runner) GetNewModuleContent(schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)
- func (r *Runner) GetNewResourceContent(resourceType string, schema *hclext.BodySchema, ...) (*hclext.BodyContent, error)
- func (r *Runner) GetOldModuleContent(schema *hclext.BodySchema, _ *tflint.GetModuleContentOption) (*hclext.BodyContent, error)
- func (r *Runner) GetOldResourceContent(resourceType string, schema *hclext.BodySchema, ...) (*hclext.BodyContent, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertIssues ¶
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 ¶
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 ¶
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 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 ¶
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 ¶
DecodeRuleConfig decodes rule configuration. This is a stub implementation that always returns nil (no config).
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.