itest

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package itest holds the parts of the integration test that can be unit tested: collecting specs, the sandbox module, running and building jobs, the result cache, known failures and the report.

Index

Constants

View Source
const (
	StageGenerate = "generate"
	StageBuild    = "build"
	StageTest     = "test"
)

Stages a job can fail at.

Variables

View Source
var (
	ErrSpecNotFound = errors.New("spec not found")
	ErrCommand      = errors.New("command failed")
	ErrTimeout      = errors.New("timed out")
	ErrCache        = errors.New("read cache")
)

Functions

func Exec

func Exec(ctx context.Context, dir, name string, args ...string) ([]byte, error)

Exec runs name as a process in dir.

func HashFiles

func HashFiles(paths ...string) (string, error)

HashFiles returns the hex SHA-256 of the files' contents, in order.

func Key

func Key(job Job) (string, error)

Key is the cache key of job: the hash of its spec file, its variant name, and a hash of the config it is generated with and the call it is checked with, so a changed variant runs again.

func Report

func Report(results []Result) string

Report is the text summary of a run: totals, failures by stage, generated lines, the slowest jobs, the first failures with their output, and every failed job.

func SafeNames

func SafeNames(names []string) []string

SafeNames returns a Go package name for each spec name, in the same order. Names that collide get a number, handed out in sorted order so the result does not depend on the input order.

Types

type Cache

type Cache struct {
	Tool   string          `json:"tool"`
	Passed map[string]bool `json:"passed"`
}

Cache holds the keys of the jobs that passed with one build of the tool, Tool being its hash. A tool with another hash starts with an empty cache, so there is no expiry.

func LoadCache

func LoadCache(path, tool string) (*Cache, error)

LoadCache reads the cache file at path for tool. A missing file, or one written for another tool, gives an empty cache.

func (*Cache) Save

func (c *Cache) Save(path string) error

Save writes the cache to path.

type Command

type Command func(ctx context.Context, dir, name string, args ...string) ([]byte, error)

Command runs name with args in dir and returns its combined output.

type Job

type Job struct {
	Spec    Spec
	Variant Variant
	Package string
}

Job is one spec generated with one variant into Package, a folder of the sandbox with forward slashes whose last element is the Go package name.

func Jobs

func Jobs(specs []Spec, variants []Variant) []Job

Jobs pairs every spec with every variant, spec by spec.

type Result

type Result struct {
	Job      Job
	Stage    string
	Output   string
	Lines    int
	Elapsed  time.Duration
	IsCached bool
}

Result is how a job went: an empty Stage means it passed. Output is what the failing step printed. Lines counts the generated lines.

type Runner

type Runner struct {
	Exec        Command
	Sandbox     Sandbox
	Tool        string
	Timeout     time.Duration
	Concurrency int
	BatchSize   int
	// contains filtered or unexported fields
}

Runner generates jobs with the CLI at Tool and builds what they generate in the sandbox.

func (*Runner) Progress

func (r *Runner) Progress() string

Progress is one line on how far Run is; safe to call while it runs.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, jobs []Job) []Result

Run generates up to Concurrency jobs at a time and builds the generated packages in batches of BatchSize while generation goes on. Results are in job order.

type Sandbox

type Sandbox struct {
	Dir  string
	Repo string
}

Sandbox is the Go module in Dir that generated packages are built in. It replaces the mockzilla-codegen module with Repo, so generated code imports the runtime of the tree under test.

func (Sandbox) BuildTool

func (s Sandbox) BuildTool(ctx context.Context, run Command) (string, error)

BuildTool builds the mockzilla-codegen CLI from Repo into the sandbox and returns its path. VCS stamping is off so the binary, and the cache keyed by its hash, change only with the code.

func (Sandbox) Setup

func (s Sandbox) Setup(ctx context.Context, run Command, deps []string) error

Setup removes the generated packages of an earlier run, writes go.mod and a file importing deps, the packages generated code may import, and tidies the module.

type Spec

type Spec struct {
	Name string
	Path string
	Size int64
}

Spec is one spec file a run generates from. Name is the path relative to the specs folder, or to the repo when the file is outside it, with forward slashes.

func Collect

func Collect(repo, dir string, named []string) ([]Spec, error)

Collect returns the named specs, or every spec under dir when none is named, largest first so the slow ones start early. A name is a path relative to repo or to dir. The walk leaves out stash folders and names starting with "-". A missing dir gives no specs.

type Variant

type Variant struct {
	Name    string
	Config  string
	Files   map[string][]string
	Init    string
	Imports []string
}

Variant is one config every spec is generated with. Config is YAML added to the package and output keys the runner writes. Files are the entries of output.files, the selectors of the parts each file gets. Init, when set, is a call the runner makes on every package once it builds, with %s standing for the package, such as %s.NewRouter(nil); a panic fails the job. Imports are the other packages Init names.

Jump to

Keyboard shortcuts

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