archtest

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package archtest holds the architecture checks of the repository: rules about the shape of the code that the compiler and the linters do not enforce, run as Go tests so that the existing CI job finds them.

CheckAssertions is the first one: optional behaviour of a port (a store, a retriever, an exporter, a notifier, a segment provider) is part of the port, not discovered by a type assertion at the place of use. See docs/dev/architecture.md for the rule and for how to add a capability to a port.

CheckLayers is the second: the packages are in layers, and an import goes inward only. The layers are data (a LayerSpec), and an import that breaks the rule is listed in an allow-list with its reason, which only shrinks.

Index

Constants

View Source
const GroupKept = "kept"

GroupKept is the group of an assertion that a standard-library or third-party contract requires.

Variables

View Source
var ErrLoad = errors.New("archtest: cannot load the packages")

ErrLoad is the failure to list or type-check the packages.

Functions

This section is empty.

Types

type Allowed

type Allowed struct {
	// File is the path of the file, relative to the root of the module, with forward slashes.
	File string
	// Func is the enclosing function: "Name", "Type.Method", or "<package>" for a package-level declaration.
	Func string
	// Type is the asserted interface as [Finding.Type] prints it, for example "io.Closer".
	Type string
	// Group tells why the entry exists: [GroupKept].
	Group string
	// Reason is the one-line reason of the entry.
	Reason string
	// Count is the number of assertions of Type in Func that the entry allows; 0 means 1.
	Count int
}

Allowed is one place that non-test code may assert to an interface. An entry matches the assertions of Type in the function Func of the file File, as many as Count says. The check fails when there are more (a new assertion slipped in: the extra sites are reported) and when there are fewer (lower the count, or remove the entry when it is 0).

type AllowedImport

type AllowedImport struct {
	// From is the importing package, relative to the root of the module.
	From string
	// To is the imported package, relative to the root of the module.
	To string
	// Reason is the one-line reason of the entry, and what removes it.
	Reason string
	// Count is the number of files of From that import To; 0 means 1.
	Count int
}

AllowedImport is one import that breaks the rule and is let through until it is fixed. An entry matches the files of the package From that import the package To, as many as Count says. The check fails when there are more (a new import slipped in) and when there are fewer (lower the count, or remove the entry when it is 0): the list only shrinks.

type Build

type Build struct {
	// GOOS is the target operating system; empty is the one of the machine.
	GOOS string
	// GOARCH is the target architecture; empty is the one of the machine.
	GOARCH string
	// Tags are the build tags.
	Tags []string
}

Build is one way of building the module: the operating system and the build tags of a scan.

type Finding

type Finding struct {
	// File is where the assertion is, relative to the module root; empty for a stale entry.
	File string
	// Func is the enclosing function.
	Func string
	// Type is the asserted interface, with the package names of its parts: "io.Closer", "interface{Close() error}".
	Type string
	// Message is the text for the developer: what is wrong and what to do.
	Message string
	// Line is the line of the assertion; 0 for a stale entry.
	Line int
}

Finding is one problem of CheckAssertions.

func CheckAssertions

func CheckAssertions(ctx context.Context, dir string, builds []Build, allow []Allowed) ([]Finding, error)

CheckAssertions type-checks the non-test, non-generated Go files of the module in dir and reports every type assertion and type switch case to an interface that is either inline (`x.(interface{ Close() error })`) or applied to a value whose static type is an interface declared in the module (a port; also through a conversion to `any`, or through a type parameter that a port constrains), unless an entry of allow lists it.

The first build is scanned in full, with type information. Each later build only parses the files that no earlier build saw (a file behind a build tag or an operating system) and flags the assertions whose asserted type is an inline interface or a name that resolves to an interface declared in the module. Nothing is compiled for those builds: they cost one `go list` each. A site that several builds see is reported once.

func CheckLayers

func CheckLayers(
	ctx context.Context,
	dir string,
	builds []Build,
	spec *LayerSpec,
	allow []AllowedImport,
) ([]Finding, error)

CheckLayers reads the imports of the non-test files of the module in dir (and the test files of the layers of the spec's TestLayers), over the builds, and reports every import that the spec does not allow and the allow-list does not list: a package that no rule puts in a layer, an import that goes against the direction, and an entry of the allow-list that matches nothing (or fewer imports than it says). Only the import declarations are read, so the check costs one `go list` for each build.

func (Finding) String

func (f Finding) String() string

String prints the finding as "file:line: message".

type Layer

type Layer string

Layer is the name of a layer of the architecture: a group of packages with one place in the import direction.

type LayerRule

type LayerRule struct {
	// Path is the import path relative to the root of the module, with forward slashes. "a/b" matches that package
	// only, "a/b/..." matches it and every package below it, and "." is the package at the root.
	Path string
	// Layer is the layer of the packages that match.
	Layer Layer
	// Unit names the unit of every package that matches, for a family of packages that are one adapter together (the
	// HTTP side of the relay). Without it the unit is Path, or the child of Path with PerChild.
	Unit string
	// PerChild makes each child of Path its own unit (for "retriever/..." the units are "retriever/s3retrieverv2",
	// "retriever/httpretriever" and so on). Without it the packages that match are one unit.
	PerChild bool
}

LayerRule puts the packages under a path into a layer. The rules of a LayerSpec are tried in order and the first that matches a package decides, so a narrow rule goes before the wide one that holds it.

type LayerSpec

type LayerSpec struct {
	// Allowed lists, for each layer, the layers that its packages may import. A layer may always import itself,
	// except the layer in SameUnit.
	Allowed map[Layer][]Layer
	// SameUnit is the layer whose packages may import a package of the same layer only inside the same unit: the
	// adapters never import each other.
	SameUnit Layer
	// TestLayers are the layers whose test files are read too: a test of such a layer may import only what the layer
	// may import, so a test of the innermost layers does not pull an outer one in. The test files of the other layers
	// are not read (they use the doubles and the tools of the testsupport layer).
	TestLayers []Layer
	// TestSupport is the layer of the doubles and the fixtures: a test file of a TestLayers layer may import it, and
	// the layers that its own layer may import, nothing else.
	TestSupport Layer
	// Rules put the packages into layers.
	Rules []LayerRule
}

LayerSpec is the architecture as data: which package is in which layer, and which layer may import which.

Jump to

Keyboard shortcuts

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