Documentation
¶
Overview ¶
Package testutil provides test helpers for the Dockerfile linter.
Index ¶
- func AssertNoViolations(tb testing.TB, violations []rules.Violation)
- func AssertViolationCount(tb testing.TB, violations []rules.Violation, want int)
- func GetSemantic(tb testing.TB, input rules.LintInput) *semantic.Model
- func MakeLintInput(tb testing.TB, file, content string) rules.LintInput
- func MakeLintInputWithConfig(tb testing.TB, file, content string, config any) rules.LintInput
- func MakeLintInputWithSemantic(tb testing.TB, file, content string) rules.LintInput
- func MatchDockerfileSnapshot(tb testing.TB, content string)
- func ParseDockerfile(tb testing.TB, content string) *dockerfile.ParseResult
- func RunRuleTests(t *testing.T, rule rules.Rule, cases []RuleTestCase)
- type RuleTestCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertNoViolations ¶
AssertNoViolations fails the test if there are any violations.
func AssertViolationCount ¶
AssertViolationCount fails if the violation count doesn't match.
func GetSemantic ¶
GetSemantic extracts the *semantic.Model from a LintInput. Fails the test if the Semantic field is nil or not a *semantic.Model.
func MakeLintInput ¶
MakeLintInput creates a LintInput for testing a rule. Parses the Dockerfile content and constructs the input struct with full BuildKit instruction parsing including Stages and MetaArgs.
func MakeLintInputWithConfig ¶
MakeLintInputWithConfig creates a LintInput with rule configuration.
func MakeLintInputWithSemantic ¶
MakeLintInputWithSemantic creates a LintInput with the semantic model. This is needed for rules that use semantic analysis (package tracking, etc.).
func MatchDockerfileSnapshot ¶ added in v0.11.0
MatchDockerfileSnapshot compares content against a standalone snapshot file, writing raw bytes without any formatting transformation.
go-snaps' MatchStandaloneSnapshot passes content through pretty.Sprint (github.com/kr/pretty) whose tabwriter expands tab bytes (0x09) into spaces. All go-snaps helpers (snapshotPath, prettyDiff, etc.) are unexported so we cannot reuse them selectively. This thin helper preserves exact bytes so snapshot files match actual tally output.
Tracking issue: https://github.com/gkampitakis/go-snaps/issues/153
Follows go-snaps' naming convention for standalone snapshots:
__snapshots__/<TestName>_1.snap.Dockerfile
Set UPDATE_SNAPS=true to create or update snapshot files.
func ParseDockerfile ¶
func ParseDockerfile(tb testing.TB, content string) *dockerfile.ParseResult
ParseDockerfile parses a Dockerfile from a string using the full parsing pipeline. Returns the complete ParseResult including AST, Stages, MetaArgs, and Warnings.
Use this when you need the full parsed result (e.g., testing parser features). For rule testing, prefer MakeLintInput which creates a ready-to-use LintInput.
func RunRuleTests ¶
func RunRuleTests(t *testing.T, rule rules.Rule, cases []RuleTestCase)
RunRuleTests runs a table of test cases against a rule.
Types ¶
type RuleTestCase ¶
type RuleTestCase struct {
// Name is the test case name.
Name string
// Content is the Dockerfile content to lint.
Content string
// Config is the optional rule configuration.
Config any
// WantViolations is the expected number of violations.
// Use -1 to skip the count check.
WantViolations int
// WantCodes is the expected rule codes in violation order (for detailed checks).
WantCodes []string
// WantMessages are substrings expected in violation messages.
WantMessages []string
}
RuleTestCase defines a test case for table-driven rule tests.