testutils

package
v1.12.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

pkg/io/generator/testutils

This package provides testing utilities specifically for generator package testing.

Purpose

Contains testing utilities and helper functions designed for testing generator packages. This package provides marshal failure testing, generic test utilities, and common testing functionality for file generation and I/O operations.

Features

  • Marshal Failure Testing: Utilities for testing marshal/unmarshal error scenarios
  • Generator Testing: Common patterns for testing file generation functionality
  • Common Test Utilities: Shared testing functionality across generator packages
  • Error Scenario Testing: Utilities for testing various error conditions
  • File Generation Testing: Patterns for testing file creation and content validation

Usage

import "github.com/devantler-tech/ksail-go/pkg/io/generator/testutils"

func TestGenerator(t *testing.T) {
    // Use marshal failure testing utilities
    failer := testutils.NewMarshalFailer()

    // Test error scenarios in generators
    err := generator.Generate(failer)
    assert.Error(t, err, "expected marshal failure")
}

Key Components

  • generator.go: Core generator testing utilities
  • marshal_failer.go: Marshal failure testing utilities and error simulation
  • common_tests.go: Common test patterns shared across generator packages
  • doc.go: Package documentation and overview

Integration

This package is used by generator packages for testing:

  • pkg/io/generator/kind: Kind configuration generation testing
  • pkg/io/generator/k3d: K3d configuration generation testing
  • pkg/io/generator/eks: EKS configuration generation testing
  • pkg/io/generator/yaml: YAML generation testing
  • pkg/io/generator/kustomization: Kustomization file generation testing

This ensures consistent testing patterns across all generator functionality.


⬅️ Go Back

Documentation

Overview

Package testutils provides generator-specific test utilities.

This package contains generic test utilities, marshal failure test utilities, and other common testing functionality for generator packages.

Index

Constants

This section is empty.

Variables

View Source
var ErrMarshalFailed = errors.New("marshal failed")

ErrMarshalFailed is a common error for testing marshal failures.

Functions

func RunStandardGeneratorTests

func RunStandardGeneratorTests[T any](
	t *testing.T,
	gen generator.Generator[T, yamlgenerator.Options],
	createCluster func(name string) T,
	expectedFileName string,
	assertContent func(*testing.T, string, string),
)

RunStandardGeneratorTests runs the standard generator test suite.

func TestExistingFile

func TestExistingFile[T any](
	t *testing.T,
	gen generator.Generator[T, yamlgenerator.Options],
	cluster T,
	filename string,
	assertContent func(*testing.T, string, string),
	clusterName string,
	force bool,
)

TestExistingFile runs a common test pattern for generators with existing files.

func TestGenerateCommon

func TestGenerateCommon[T any](
	t *testing.T,
	tests []GenerateTestCase,
	createCluster func(name string) T,
	gen generator.Generator[T, yamlgenerator.Options],
	assertContent func(*testing.T, string, string),
	expectedFileName string,
)

TestGenerateCommon runs the common TestGenerate pattern with shared setup and assertions.

func TestGeneratorMarshalError

func TestGeneratorMarshalError[T, M any](
	t *testing.T,
	gen generator.Generator[T, yamlgenerator.Options],
	cluster T,
	expectedErrorContains string,
)

TestGeneratorMarshalError runs a generic test pattern for generator marshal errors.

Types

type GenerateTestCase

type GenerateTestCase struct {
	Name        string
	ClusterName string
	SetupOutput func(t *testing.T) (output string, verifyFile bool, tempDir string)
	ExpectError bool
	Force       bool
}

GenerateTestCase represents a test case for the basic TestGenerate pattern.

func GetStandardGenerateTestCases

func GetStandardGenerateTestCases(expectedFileName string) []GenerateTestCase

GetStandardGenerateTestCases returns standard test cases for generator testing.

func GetStandardGenerateTestCasesWithForce

func GetStandardGenerateTestCasesWithForce(expectedFileName string) []GenerateTestCase

GetStandardGenerateTestCasesWithForce returns standard test cases including force overwrite.

type MarshalFailer

type MarshalFailer[T any] struct {
	marshaller.Marshaller[T]
}

MarshalFailer is a generic marshal failer that can be used with any config type. It embeds the marshaller interface and overrides only the Marshal method to fail.

func (MarshalFailer[T]) Marshal

func (m MarshalFailer[T]) Marshal(_ T) (string, error)

Marshal always returns an error for testing purposes.

Jump to

Keyboard shortcuts

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