testutils

package
v1.11.3 Latest Latest
Warning

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

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

README

internal/testutils

This package provides general-purpose testing utilities for KSail's internal packages.

Purpose

Contains generic testing helpers, utilities, and shared functionality used across multiple packages for testing. This package provides foundational testing capabilities that support testing in various parts of KSail's codebase.

Features

  • File Testing Utilities: Helpers for file operations and temporary file management
  • Error Handling: Shared test error utilities and error testing patterns
  • String Utilities: String manipulation and testing helpers
  • Marshal/Unmarshal Testing: Utilities for testing serialization and deserialization
  • Name Case Testing: Utilities for testing naming conventions and case conversions
  • Generic Helpers: Common testing patterns and setup/teardown utilities

Usage

import "github.com/devantler-tech/ksail-go/internal/testutils"

// Use file testing utilities
tempFile := testutils.CreateTempFile(t, "test-content")
defer os.Remove(tempFile)

// Use error testing utilities
testutils.AssertError(t, err, "expected error message")

// Use string testing utilities
result := testutils.NormalizeString(input)

Key Components

  • file.go: File operation testing utilities
  • errors.go: Error handling and testing patterns
  • helpers.go: General-purpose testing helper functions
  • marshal.go: Serialization/deserialization testing utilities
  • namecases.go: Name case conversion testing utilities
  • string.go: String manipulation testing helpers

This package provides the foundational testing infrastructure used throughout KSail's test suite.


⬅️ Go Back

Documentation

Overview

Package testutils provides testing utilities to aid error handling in tests.

This package contains generic file-related test utilities, testing helpers shared across packages, and various utilities for error handling and test setup/teardown.

Index

Constants

This section is empty.

Variables

View Source
var ErrTestConfigLoadError = errors.New("test config load error")

ErrTestConfigLoadError is a static test error to comply with err113 and allow reuse across packages.

Functions

func AssertErrContains

func AssertErrContains(t *testing.T, got error, contains string, ctx string)

AssertErrContains asserts that an error is non-nil and its message contains the provided substring.

func AssertErrWrappedContains

func AssertErrWrappedContains(t *testing.T, got error, want error, contains string, ctx string)

AssertErrWrappedContains verifies that an error exists, wraps a target error, and optionally contains a given substring in its message. "ctx" describes the calling context.

func AssertFileEquals

func AssertFileEquals(t *testing.T, dir, path, expected string)

AssertFileEquals compares the file content with the expected string.

func AssertStringContains

func AssertStringContains(t *testing.T, haystack string, subs ...string)

AssertStringContains asserts that the haystack contains all provided substrings.

func AssertStringContainsOneOf

func AssertStringContainsOneOf(t *testing.T, haystack string, options ...string)

AssertStringContainsOneOf asserts that the haystack contains at least one of the provided options.

func MustMarshal

func MustMarshal[T any](t *testing.T, m marshaller.Marshaller[T], v T) string

MustMarshal marshals a value or fails the test.

func MustUnmarshal

func MustUnmarshal[T any](t *testing.T, m marshaller.Marshaller[T], data []byte, out *T)

MustUnmarshal unmarshals bytes into a value or fails the test.

func MustUnmarshalString

func MustUnmarshalString[T any](t *testing.T, m marshaller.Marshaller[T], data string, out *T)

MustUnmarshalString unmarshals a string into a value or fails the test.

func RunNameCases

func RunNameCases(t *testing.T, cases []NameCase, run func(t *testing.T, c NameCase))

RunNameCases runs the provided function for each NameCase with parallel subtests.

func RunTestMainWithSnapshotCleanup

func RunTestMainWithSnapshotCleanup(m *testing.M)

RunTestMainWithSnapshotCleanup runs the standard TestMain pattern with snapshot cleanup. Shared across packages that only need snapshot cleanup (non command-specific logic).

func SetupExistingFile

func SetupExistingFile(t *testing.T, filename string) (string, string, string)

SetupExistingFile creates a temporary directory and an existing config file with default placeholder content, returning the directory, file path, and content string.

Types

type NameCase

type NameCase struct {
	Name         string
	InputName    string
	ExpectedName string
}

NameCase represents a common test case shape for commands that accept an optional name and default to a configured name when empty.

func DefaultNameCases

func DefaultNameCases(defaultCfgName string) []NameCase

DefaultNameCases returns the standard two cases used throughout the repo: 1) explicit name; 2) empty name falls back to default/cfg name.

Jump to

Keyboard shortcuts

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