scantest

package
v0.36.4 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package scantest exposes utilities for testing the codescan packages.

Choosing the loader a test run uses

The suites emit the same document however the package graph was fetched, so which loader runs is a question about what the run costs. ApplyLoader is the one place that decides, reading LoaderEnv or the build tag the binary was compiled with:

go test ./...                                       # the shipped default, source dependencies
CODESCAN_TEST_LOADER=compiled go test ./...          # dependency types from the build cache
CODESCAN_TEST_LOADER=own go test ./...               # codescan's own loader, no go command
go test -tags=testloader_compiled ./...              # the same, selected at build time

The environment variable is for a person at a terminal. The build tags exist because CI reaches `go test` through a shared workflow that forwards flags and not the environment.

It is worth having because the harness is the shape compiled dependencies were made for: several hundred scans of small trees over one dependency closure, so the closure is compiled once and every scan after the first reads it back. Over the whole repository that is a quarter of the CPU, and a runner with two cores spends most of that difference on the wall.

Two kinds of test do not come through here. Those that pin a loader themselves — the agreement A/B, the virtual-filesystem and export-data suites — call codescan.Run directly, because there is no way to tell a field left at its zero value from one set to it on purpose. And the runnable examples under docs/ show a caller how to call the API, so they use the real defaults and nothing else.

Index

Constants

View Source
const (
	LoaderSource   = testloader.LoaderSource
	LoaderCompiled = testloader.LoaderCompiled
	LoaderOwn      = testloader.LoaderOwn

	LoaderEnv = testloader.Env
)

Re-exported so a caller that already imports scantest needs nothing else.

The policy itself lives in internal/testloader, which imports nothing: the scanner's own tests are in-package and this package imports the scanner, so a policy declared here could not be read from where most of the scans are.

Variables

This section is empty.

Functions

func ApplyLoader added in v0.36.4

func ApplyLoader(opts *scanner.Options) *scanner.Options

ApplyLoader writes the selected loader onto opts and returns it, for chaining onto a call.

It is deliberately a WRITE rather than a default: a test that has an opinion about how its graph is loaded must not route through here at all, because there is no way to tell a field left at its zero value from one set to it on purpose. Those tests set their options directly, and say so.

Options.FS is the one thing it will not fight: a virtual filesystem forces the toolchain-free loader whatever anyone asks for, so asking for something else here would only produce a diagnostic about an intent that could not be met.

func AssertArrayProperty

func AssertArrayProperty(t *testing.T, schema *oaispec.Schema, typeName, jsonName, format, goName string)

func AssertArrayRef

func AssertArrayRef(t *testing.T, schema *oaispec.Schema, jsonName, goName, fragment string)

func AssertProperty

func AssertProperty(t *testing.T, schema *oaispec.Schema, typeName, jsonName, format, goName string)

func AssertRef

func AssertRef(t *testing.T, schema *oaispec.Schema, jsonName, _, fragment string)

AssertRef checks that the named property is a $ref to fragment.

Accepts both shapes: a bare $ref schema, and the P7/S7 allOf compound where the $ref rides arm[0] (with description and optional override siblings on the parent / arm[1]).

func CompareOrDumpJSON

func CompareOrDumpJSON(t *testing.T, got any, name string)

CompareOrDumpJSON marshals got to stable JSON and either writes it to <repo>/testdata/integration/golden/<name> (when UPDATE_GOLDEN=1) or asserts that it JSON-equals the stored golden.

This is the regression-testing harness used to detect any behavior change in the go-openapi/spec objects produced by the scanner, compared against a captured baseline.

Golden files are named by content (fixture bundle + object kind + entity), not by test name, so they survive test reshuffling.

func FixturesDir

func FixturesDir() string

FixturesDir returns the absolute path to the repo-level testdata/ directory, so tests can run from any package depth without fragile relative paths.

func LoadClassificationPkgsCtx

func LoadClassificationPkgsCtx(t *testing.T) *scanner.ScanCtx

func LoadGo118ClassificationPkgsCtx

func LoadGo118ClassificationPkgsCtx(t *testing.T) *scanner.ScanCtx

func LoadPetstorePkgsCtx

func LoadPetstorePkgsCtx(t testing.TB, enableDebug bool) *scanner.ScanCtx

func ResolveTestKey added in v0.35.0

func ResolveTestKey(t *testing.T, defs map[string]oaispec.Schema, short string) string

ResolveTestKey returns the fully-qualified definitions key whose leaf (the segment after the last '/') equals short, so a sub-builder unit test can keep indexing the definitions map by the short Go name:

schema := models[scantest.ResolveTestKey(t, models, "NoModel")]

and can build the expected fully-qualified $ref the same way:

scantest.AssertRef(t, &schema, "pet", "Pet",
    "#/definitions/"+scantest.ResolveTestKey(t, models, "pet"))

The schema builder keys the definitions map by the fully-qualified identity ("<pkgpath>/<name>", see scanner.EntityDecl.DefKey); the spec orchestrator's reduce stage later shortens unique leaves back to the bare name, but sub-builder unit tests run WITHOUT that stage.

When no definition carries the leaf, short is returned unchanged so absence checks (`_, ok := models[ResolveTestKey(...)]; assert.False(ok)`) still observe a miss. It fails only on a genuinely ambiguous match, which no current unit fixture produces. §12.1.

func TestLoader added in v0.36.4

func TestLoader() testloader.Loader

TestLoader reports which loader the suites run under. See testloader.Selected.

Types

This section is empty.

Directories

Path Synopsis
Package mocks contains mock types for all interfaces.
Package mocks contains mock types for all interfaces.

Jump to

Keyboard shortcuts

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