golden

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package golden provides snapshot testing utilities for verifying resource primitive output across versions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertYAML

func AssertYAML[T client.Object](t *testing.T, path string, p Previewer[T], opts ...Option)

AssertYAML is a test helper that calls CompareYAML and fails the test if the result does not match the golden file. See CompareYAML for details.

func CompareYAML

func CompareYAML[T client.Object](path string, p Previewer[T], opts ...Option) error

CompareYAML calls PreviewObject on p, serializes the result as YAML, and compares it against the golden file at path. Returns a *MismatchError if they differ, or nil if they match.

When Update is enabled, the golden file is written (creating intermediate directories as needed) and comparison is skipped.

Returns an error if the golden file does not exist and Update is not enabled.

Types

type MismatchError

type MismatchError struct {
	Path string
	Diff string
}

MismatchError is returned by CompareYAML when the serialized output does not match the golden file. The Diff field contains a unified diff.

func (*MismatchError) Error

func (e *MismatchError) Error() string

type Option

type Option func(*config)

Option configures behavior.

func Update

func Update(enabled bool) Option

Update returns an Option that overwrites the golden file with the actual output when enabled. Typically wired to a -update test flag:

var update = flag.Bool("update", false, "update golden files")

golden.AssertYAML(t, "testdata/foo.yaml", res, golden.Update(*update))

func WithScheme

func WithScheme(s *runtime.Scheme) Option

WithScheme returns an Option that sets apiVersion and kind on the serialized object when TypeMeta is not populated. The scheme is used to look up the object's GroupVersionKind.

Without this option, objects that omit TypeMeta cause serialization to fail. Callers must either populate TypeMeta on the object or provide a scheme via WithScheme so apiVersion and kind can be resolved.

type Previewer

type Previewer[T client.Object] interface {
	PreviewObject() (T, error)
}

Previewer is satisfied by any resource primitive that can render its post-mutation state. All built-in primitives implement this through [generic.BaseResource].

Jump to

Keyboard shortcuts

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