testhelper

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package testhelper provides deterministic in-memory implementations of the interfaces package for consumer tests.

Example

Example shows the five deterministic in-memory seams and problem-typed normalization of an injected error.

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/AtomiCloud/diene.go-errors-problems/lib/problem"
	"github.com/AtomiCloud/diene.go-interfaces/lib/interfaces"
	"github.com/AtomiCloud/diene.go-interfaces/testhelper"
)

func main() {
	ctx := context.Background()
	system := testhelper.NewInMemorySystem(testhelper.InMemorySystemOptions{})
	vfs := testhelper.NewInMemoryVfs(testhelper.InMemoryVfsOptions{})
	terminal := testhelper.NewInMemoryTerminal()
	logger := testhelper.NewInMemoryLoggerSink()
	metrics := testhelper.NewInMemoryMetricsCollector()

	now, _ := system.NowUTC()
	exists, _ := vfs.Exists(ctx, "/")
	terminal.EnqueueResult(interfaces.TerminalOutput{Stdout: "ok"}, nil)
	output, _ := terminal.Run(ctx, interfaces.NewTerminalCommand("tool", nil, nil, nil, false, false))
	_ = logger.Emit(interfaces.NewLogRecord(now, interfaces.LogLevelInfo, "ready", nil, nil, nil))
	_ = metrics.Emit(interfaces.NewMetricRecord(now, "ready", interfaces.MetricKindGauge, 1, nil, nil))

	injected := errors.New("launch failed")
	terminal.EnqueueResult(interfaces.TerminalOutput{}, injected)
	_, err := terminal.Run(ctx, interfaces.NewTerminalCommand("tool", nil, nil, nil, false, false))
	var problemErr *problem.Error

	fmt.Println(now.Equal(time.Unix(0, 0).UTC()), exists, output.Stdout, len(logger.Records()), len(metrics.Records()), errors.As(err, &problemErr))
}
Output:
true true ok 1 1 true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InMemoryLoggerSink

type InMemoryLoggerSink struct {
	// contains filtered or unexported fields
}

InMemoryLoggerSink is a FIFO-scriptable LoggerSink that records successful emissions.

func NewInMemoryLoggerSink

func NewInMemoryLoggerSink() *InMemoryLoggerSink

NewInMemoryLoggerSink creates an empty logger mock.

func (*InMemoryLoggerSink) Emit

func (s *InMemoryLoggerSink) Emit(record interfaces.LogRecord) error

Emit implements interfaces.LoggerSink.

func (*InMemoryLoggerSink) EnqueueResult

func (s *InMemoryLoggerSink) EnqueueResult(err error)

EnqueueResult adds a FIFO logger result.

func (*InMemoryLoggerSink) Records

func (s *InMemoryLoggerSink) Records() []interfaces.LogRecord

Records returns an independently owned snapshot of emitted records.

type InMemoryMetricsCollector

type InMemoryMetricsCollector struct {
	// contains filtered or unexported fields
}

InMemoryMetricsCollector is a FIFO-scriptable MetricsCollector that records successful emissions.

func NewInMemoryMetricsCollector

func NewInMemoryMetricsCollector() *InMemoryMetricsCollector

NewInMemoryMetricsCollector creates an empty metrics mock.

func (*InMemoryMetricsCollector) Emit

Emit implements interfaces.MetricsCollector.

func (*InMemoryMetricsCollector) EnqueueResult

func (s *InMemoryMetricsCollector) EnqueueResult(err error)

EnqueueResult adds a FIFO metrics result.

func (*InMemoryMetricsCollector) Records

Records returns an independently owned snapshot of emitted records.

type InMemorySystem

type InMemorySystem struct {
	// contains filtered or unexported fields
}

InMemorySystem is a deterministic System with mutable in-memory process state and FIFO-scriptable results.

func NewInMemorySystem

func NewInMemorySystem(options InMemorySystemOptions) *InMemorySystem

NewInMemorySystem creates a deterministic system from options.

func (*InMemorySystem) CurrentDirectory

func (s *InMemorySystem) CurrentDirectory() (string, error)

CurrentDirectory implements interfaces.System.

func (*InMemorySystem) Delay

func (s *InMemorySystem) Delay(_ context.Context, duration time.Duration) error

Delay implements interfaces.System without sleeping.

func (*InMemorySystem) EnqueueClockResult

func (s *InMemorySystem) EnqueueClockResult(value time.Time, err error)

EnqueueClockResult adds a FIFO NowUTC result.

func (*InMemorySystem) EnqueueDelayResult

func (s *InMemorySystem) EnqueueDelayResult(err error)

EnqueueDelayResult adds a FIFO Delay result.

func (*InMemorySystem) EnqueueDirectoryResult

func (s *InMemorySystem) EnqueueDirectoryResult(value string, err error)

EnqueueDirectoryResult adds a FIFO CurrentDirectory result.

func (*InMemorySystem) EnqueueEnvironmentResult

func (s *InMemorySystem) EnqueueEnvironmentResult(value *string, err error)

EnqueueEnvironmentResult adds a FIFO Environment result.

func (*InMemorySystem) Environment

func (s *InMemorySystem) Environment(name string) (*string, error)

Environment implements interfaces.System.

func (*InMemorySystem) NowUTC

func (s *InMemorySystem) NowUTC() (time.Time, error)

NowUTC implements interfaces.System.

func (*InMemorySystem) RequestedDelays

func (s *InMemorySystem) RequestedDelays() []time.Duration

RequestedDelays returns an independently owned snapshot of requested delays.

func (*InMemorySystem) SetDirectory

func (s *InMemorySystem) SetDirectory(directory string)

SetDirectory replaces the in-memory working directory.

func (*InMemorySystem) SetEnvironment

func (s *InMemorySystem) SetEnvironment(name, value string)

SetEnvironment sets or replaces one in-memory environment variable.

func (*InMemorySystem) SetNow

func (s *InMemorySystem) SetNow(now time.Time)

SetNow replaces the in-memory UTC clock.

type InMemorySystemOptions

type InMemorySystemOptions struct {
	// Environment is the initial process environment.
	Environment map[string]string
	// Directory is the current working directory. The empty value is "/".
	Directory string
	// Now is the initial clock value. The zero value is the Unix epoch in UTC.
	Now time.Time
}

InMemorySystemOptions configures an InMemorySystem.

type InMemoryTerminal

type InMemoryTerminal struct {
	// contains filtered or unexported fields
}

InMemoryTerminal is a FIFO-scripted Terminal that records every command.

func NewInMemoryTerminal

func NewInMemoryTerminal() *InMemoryTerminal

NewInMemoryTerminal creates an empty terminal mock.

func (*InMemoryTerminal) Commands

Commands returns an independently owned snapshot of requested commands.

func (*InMemoryTerminal) EnqueueResult

func (t *InMemoryTerminal) EnqueueResult(value interfaces.TerminalOutput, err error)

EnqueueResult adds a FIFO terminal result.

func (*InMemoryTerminal) Run

Run implements interfaces.Terminal.

type InMemoryVfs

type InMemoryVfs struct {
	// contains filtered or unexported fields
}

InMemoryVfs is a stateful byte-backed Vfs with FIFO-scriptable results. It is the synchronized, scriptable adapter around a vfsstore.Store that owns the storage algorithms.

func NewInMemoryVfs

func NewInMemoryVfs(options InMemoryVfsOptions) *InMemoryVfs

NewInMemoryVfs creates a byte-backed filesystem from options.

func (*InMemoryVfs) CreateDirectory

func (v *InMemoryVfs) CreateDirectory(_ context.Context, path string, options interfaces.DirectoryOptions) error

CreateDirectory implements interfaces.Vfs.

func (*InMemoryVfs) Delete

func (v *InMemoryVfs) Delete(_ context.Context, path string, options interfaces.DirectoryOptions) error

Delete implements interfaces.Vfs.

func (*InMemoryVfs) Directories

func (v *InMemoryVfs) Directories() []string

Directories returns a sorted, independently owned snapshot of directory paths.

func (*InMemoryVfs) EnqueueCreateDirectoryResult

func (v *InMemoryVfs) EnqueueCreateDirectoryResult(err error)

EnqueueCreateDirectoryResult adds a FIFO CreateDirectory result.

func (*InMemoryVfs) EnqueueDeleteResult

func (v *InMemoryVfs) EnqueueDeleteResult(err error)

EnqueueDeleteResult adds a FIFO Delete result.

func (*InMemoryVfs) EnqueueExistsResult

func (v *InMemoryVfs) EnqueueExistsResult(value bool, err error)

EnqueueExistsResult adds a FIFO Exists result.

func (*InMemoryVfs) EnqueueListResult

func (v *InMemoryVfs) EnqueueListResult(value []interfaces.VfsEntry, err error)

EnqueueListResult adds a FIFO List result.

func (*InMemoryVfs) EnqueueReadBytesResult

func (v *InMemoryVfs) EnqueueReadBytesResult(value []byte, err error)

EnqueueReadBytesResult adds a FIFO ReadBytes result.

func (*InMemoryVfs) EnqueueReadTextResult

func (v *InMemoryVfs) EnqueueReadTextResult(value string, err error)

EnqueueReadTextResult adds a FIFO ReadText result.

func (*InMemoryVfs) EnqueueWriteBytesResult

func (v *InMemoryVfs) EnqueueWriteBytesResult(err error)

EnqueueWriteBytesResult adds a FIFO WriteBytes result.

func (*InMemoryVfs) EnqueueWriteTextResult

func (v *InMemoryVfs) EnqueueWriteTextResult(err error)

EnqueueWriteTextResult adds a FIFO WriteText result.

func (*InMemoryVfs) Exists

func (v *InMemoryVfs) Exists(_ context.Context, path string) (bool, error)

Exists implements interfaces.Vfs.

func (*InMemoryVfs) Files

func (v *InMemoryVfs) Files() map[string][]byte

Files returns an independently owned snapshot of file contents.

func (*InMemoryVfs) List

List implements interfaces.Vfs.

func (*InMemoryVfs) ReadBytes

func (v *InMemoryVfs) ReadBytes(_ context.Context, path string) ([]byte, error)

ReadBytes implements interfaces.Vfs.

func (*InMemoryVfs) ReadText

func (v *InMemoryVfs) ReadText(_ context.Context, path string) (string, error)

ReadText implements interfaces.Vfs.

func (*InMemoryVfs) WriteBytes

func (v *InMemoryVfs) WriteBytes(_ context.Context, path string, bytes []byte, options interfaces.WriteOptions) error

WriteBytes implements interfaces.Vfs.

func (*InMemoryVfs) WriteText

func (v *InMemoryVfs) WriteText(_ context.Context, path, content string, options interfaces.WriteOptions) error

WriteText implements interfaces.Vfs.

type InMemoryVfsOptions

type InMemoryVfsOptions struct {
	// Files maps initial paths to their byte contents.
	Files map[string][]byte
	// Directories lists initial directories. Root is always present.
	Directories []string
}

InMemoryVfsOptions configures an InMemoryVfs.

Directories

Path Synopsis
internal
faults
Package faults centralizes the canonical problem minting and error normalization shared by the testhelper mocks.
Package faults centralizes the canonical problem minting and error normalization shared by the testhelper mocks.
vfsstore
Package vfsstore holds the stateful, byte-backed virtual filesystem algorithms behind the testhelper InMemoryVfs.
Package vfsstore holds the stateful, byte-backed virtual filesystem algorithms behind the testhelper InMemoryVfs.

Jump to

Keyboard shortcuts

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