testfixture

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 16 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 testfixture injects a known origin catalogue and cast into a scratch data directory.

It exists because the tests used to name the characters the repository shipped, which coupled them to content the author is free to change: editing the real cast broke thirty-two tests that had nothing to do with it. A test that needs "a character" now gets this one, and a test that needs "whatever is shipped" asks the book instead of naming a row.

It is a normal package rather than a _test.go file because three packages need the same fixture, and a second copy of it is a second thing to keep in step. Nothing outside a test imports it.

Index

Constants

View Source
const Archetypes = `` /* 3426-byte string literal not displayed */

Archetypes is the five role presets the tests reason about, moved here for the same reason and in the same breath: their kits name the skills above.

View Source
const Art = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect width="16" height="16" fill="#4a5"/></svg>`

Art is a placeholder drawing, small enough to be legible in a diff. The authoring tool only asks whether the file exists.

View Source
const ArtDir = "assets"

ArtDir is the folder inside a data directory that art lives under.

internal/forge declares the same folder for the game itself, and this package cannot read that declaration: internal/forge's own tests import this package, so importing it back would be an import cycle in that test binary. A second spelling of one folder name is otherwise exactly the mistake internal/forge names in its own constant's comment, which is why the two are held together by a test over there rather than by hope.

View Source
const Characters = `` /* 3029-byte string literal not displayed */

Characters is the cast the fixture adds.

View Source
const Origins = `` /* 446-byte string literal not displayed */

Origins is the catalogue the fixture adds.

View Source
const Roster = `` /* 4767-byte string literal not displayed */

Roster is the ten-unit bench the engine tests fight on: two full teams whose affinities and kits between them reach every element, every shape and every status the books declare.

It was the shipped roster until the cast became real. An engine test wants a battle that exercises everything, and a shipped roster wants to be the game -- those are different jobs, and one file could not do both once the game had one character in it.

Two units carry a trait, and they are here rather than only in the shipped roster for the reason the rest of this bench exists: an engine test needs every kind of event a battle can produce, and which trait a borrowed character holds is a design decision rather than an engine one. Both traits are shipped ones — the bench uses the real status and passive books, so a fixture inventing its own would be testing a book nobody ships.

`endurance` on ally.bulwark is the plain case: on from the opening board and never off. `blaze` on foe.bulwark is the gated one, and the unit was chosen for the skill it already had rather than for its element: `siphon` drains, so that unit is the one on the bench that reliably heals itself back up — which is the only way a gate is crossed in the *other* direction, and therefore the only way passive_released is ever emitted.

View Source
const Skills = `` /* 9083-byte string literal not displayed */

Skills is the bench the authoring tests exercise: twenty-nine skills covering every element, every shape, every kind of condition, a gradient off the caster's own health, multi-strike, area splash, cleansing and a shield.

`rekindle` is the caster side, and it is here because the bench used to have no `self_requires` at all: a test that needed one had to build it, `desperate` covered only the gradient, and a mechanic the bench cannot see is a mechanic nothing downstream really covers. It spends in the health currency rather than the power one because that is the arm with the least standing behind it — the shipped book has spenders of both, and the bench has to hold the shape a TestTheBenchCoversTheMechanics row can ask about.

`vent` is the same argument for the shape beside it: a spend that buys the CAST rather than a bonus on top of one. Its condition carries no payment at all — the flat power on its face is the whole figure — so it is the one condition here that decides whether the skill is offered, and a bench without it leaves every reader of skill.Condition.GatesCast covered by nothing but the tests that build their own.

They used to be the shipped skill book, and the tests named them directly. That coupled a hundred assertions to content the author is free to delete -- which is exactly what happened. They live here now, so what the game ships and what the tests need are two different lists.

Variables

View Source
var FixtureArt = []string{"fixture/adept.svg", "fixture/sprout.svg", "fixture/bloom.svg"}

FixtureArt is the pictures the fixture characters name, under ArtDir.

bloom.svg is the grown form's own picture. The bench carries one so that per-stage art is exercised by every test that walks this cast, rather than only by the one test written for it.

View Source
var InjectRoot = ""

InjectRoot is where injectOnce does its work, empty meaning os.TempDir().

It is a seam for one assertion that cannot be made any other way: the directory an injection happens in is deleted before the injection returns, and "nothing was left behind" is only checkable against a directory the test owns. Globbing the real temporary directory would be a flake — `make check` runs five of these suites at once, each staging its own — so a test that wants to look points this somewhere private first.

Functions

func CopiedArt added in v0.2.0

func CopiedArt(shipped, scratch string) (copied []string, walked int, err error)

CopiedArt walks a shipped data directory's art and reports which of it a scratch copy has its own bytes for, and how many pictures it looked at.

It is here rather than in each suite because five packages make scratch data directories and all five need the same guard: the fallbacks in CopyData are correct and slow, so a machine or a refactor that put every picture on the slow path would leave every test passing and the arithmetic back where it started. The count comes back so that a suite whose data directory has no art in it cannot pass this by walking nothing.

func Inject

func Inject(dir string, reload func() (Saver, error)) error

Inject adds the fixture BOOKS to a data directory that already holds the shipped ones. WriteArt is the pictures, and Data is both.

reload hands back a library reading that directory, and is called again before every write: each save rewrites a whole book, so a library opened once would hold a stale copy of the file it is about to replace. That reload is the expensive part of this — it re-parses every book each time — which is why Data runs this once a process rather than once a scratch directory.

It writes through the library rather than editing the JSON for two reasons. The files stay in exactly the form Marshal produces, which a test asserts about the shipped cast, and the fixture is validated the way real data is -- a fixture the tool would refuse is not one worth testing with.

⚠️ **This is DETERMINISTIC, and staging it rests on that.** It reads no clock, draws no randomness and generates no identifier: what it writes is the fixed package constants below put through the library's own savers. So its output is a pure function of the books it is given and those constants — which is the precondition for building it once and handing out copies, and the reason stageKey hashes exactly those two things.

func PrivateArt added in v0.2.0

func PrivateArt(dir, image string) error

PrivateArt takes one picture out of the share, so a test may write to it.

A shared file is the same file: writing through it reaches whatever it was shared from, which for the shipped art is a committed picture. Exactly one test in this repository rewrites art it was handed rather than art it made, and it calls this first. The name is what makes that visible — a test that mutates art and does not say so is the one way this arrangement could damage the repository, and a silent one.

The replacement is a plain copy of the bytes, made by writing a new file over the link rather than through it. The modification time is deliberately NOT preserved: a caller taking a private copy is about to change the file, so a stamp from before the change would be a lie the preview's cache would believe.

func RequireSharedArt added in v0.2.0

func RequireSharedArt(t Reporter, shipped, scratch string)

RequireSharedArt is the guard: a scratch data directory must not hold its own copy of the shipped pictures, on a machine that could have shared them.

It asserts the mechanism and not a stopwatch. A timing assertion would be a flake, and would pass anyway on a machine fast enough to make the copy look cheap. What it is watching for is a change that put every picture on the copy path — the fallback quietly becoming the normal path leaves every test in every one of those packages green and every one of them back at its old cost, which is the only failure a green suite cannot show.

Where neither kind of link can be made this skips instead, naming the two refusals it was given. That is not the assertion being softened: the probe below uses the same two calls the sharing does, so a machine that CAN link and did not still fails here, which is the regression the guard is for.

func SkipWithoutArtSharing added in v0.2.0

func SkipWithoutArtSharing(t Reporter, shipped, scratch string)

SkipWithoutArtSharing skips t where this machine cannot give a scratch directory a second name for a shipped picture.

It is RequireSharedArt's first half on its own, for the one caller that makes the same claim by a different reading: internal/forge counts the BYTES a scratch directory wrote, which catches what comparing inodes cannot. That assertion fails on a cross-volume Windows box for exactly the same reason the inode one does — a copied picture is seventeen megabytes however the reading is taken — so the two share this decision rather than each making up their own.

func Stagings added in v0.2.0

func Stagings() int

Stagings reports how many times the fixture has been injected in this process.

It is here so a test can assert the arrangement rather than a stopwatch: a staging that quietly stopped being shared would leave every test passing and the suite back at its old cost, which is the same failure CopiedArt names one layer up. A clock cannot say it, because a fast machine makes an unshared injection look cheap.

func WriteArt added in v0.2.0

func WriteArt(dir string) error

WriteArt writes the fixture's own pictures into a data directory.

⚠️ **It is separate from Inject because the books are staged once per process and these pictures are not.** Data below writes them into every scratch directory, so a test that rewrites one reaches nothing but its own copy — which is what they were before the injection was staged, and what TestThePreviewRasterisesOncePerFileAndSize was written over. Sharing them instead would be cheap and would quietly turn the fixture's art into a file two tests could fight over.

Each is removed before it is written, for the reason PrivateArt does the same: writing over a link writes through it. Nothing shares these today, and "today" is not a property.

Types

type DataCopy added in v0.2.0

type DataCopy struct {
	Copied      int   // files written out byte for byte
	Linked      int   // art given a second name for the same bytes
	Symlinked   int   // art pointed at, where a second name was refused
	BytesCopied int64 // bytes actually written
}

DataCopy is what CopyData did, so a caller may make an assertion about the mechanism instead of about a stopwatch.

The counts are here because the fallbacks below must never become the normal path without somebody noticing. A copy that quietly stopped sharing is a change that does nothing while every test still passes — which is the shape this whole file was written to remove, so it would be a poor thing to reintroduce one layer down.

func CopyData added in v0.2.0

func CopyData(from, to string) (DataCopy, error)

CopyData puts one data directory's contents into another: the books are copied, and the art is SHARED.

The split is the whole point. A test that drives the authoring tools needs a data directory it may write to — the save key, `skills add` and `skills edit` all write — so a single shared copy is not available. What those tests do not need is their own pictures: the shipped assets folder is 16 MB of SVG against 350 KB of JSON, and no assertion in this repository reads an asset's bytes except the art preview, which only ever reads them. Copying the books and sharing the art therefore costs a few hundred kilobytes per scratch directory instead of seventeen megabytes, and — this is the direction that matters — it stops the per-test cost growing every time a picture is added.

Sharing is a hard link first, a symbolic link second and a byte copy last. A hard link is preferred because it is the one that works where this hurts most: on Windows a hard link needs no privilege on a volume, while a symbolic link needs developer mode, and Windows is where a scratch copy was measured at four to ten seconds. A hard link can be refused across filesystems, which is what the symbolic link answers, and a symbolic link can be refused by privilege, which is what the copy answers. All three preserve the file's size and modification time, deliberately: forge's art preview keys its cache on exactly those two, so a mechanism that changed either would be a mechanism that changed what is under test.

func Data added in v0.2.0

func Data(target, shipped string, load func(dir string) (Saver, error)) (DataCopy, error)

Data fills target with a data directory a test may write to: the shipped books with the fixture injected, the shipped art shared, and the fixture's own art its own.

Why this exists rather than a CopyData followed by an Inject

That is what five packages did, and Inject is the expensive half. It reloads the whole library before every write — once for the skill dependencies and once per skill, origin and character, which is fifty-odd full parses of every book — because each save rewrites a whole file and a library opened once would hold a stale copy of the one it is about to replace. Measured on this repository, that is about 180ms of a 200ms scratch directory, and the copying the previous half of this work removed was 22ms of it.

So the injection is done **once per process** and its result — the book files, as bytes — is held in memory. A scratch directory is then those bytes written out, the shipped pictures linked, and the fixture's three pictures written. The books are still private per directory, which is the property the authoring suites rest on: they write, and two of them over one file would be a suite whose answers depended on the order it ran in.

Why it cannot go stale

⚠️ **The failure worth designing against is a staged directory built from old books while every test still passes**, so two things are true of this and both are deliberate. Nothing is written to disk between runs, so a later process can never pick up an earlier one's injection — the injector compiled into the test binary is always the one that ran. And within a run the stage is keyed on a digest of the books it was built from *and* of the fixture constants it wrote, so books edited while the tests are running produce a different stage rather than a stale one. See stageKey.

load is given the directory to read, because the injection happens somewhere the caller has never heard of. It is a function rather than the library itself so that internal/forge's own tests can use this without an import cycle.

func (DataCopy) Shared added in v0.2.0

func (d DataCopy) Shared() int

Shared is the art this copy did not have to write.

type Reporter added in v0.2.0

type Reporter interface {
	Helper()
	Skipf(format string, args ...any)
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
}

Reporter is the part of a test these guards report through.

It is an interface of this package's own rather than testing.TB because testing.TB cannot be implemented outside the standard library — it holds an unexported method — and **both arms of the decision below have to be demonstrated by a test**: one machine shape that skips, one that fails. A suite that can only observe whichever arm it happens to be on is exactly the blindness this file exists to remove, so the seam that lets a test watch a skip happen is part of the design rather than a convenience.

*testing.T and *testing.B satisfy it as they are.

type Saver

type Saver interface {
	SaveOrigin(cast.Origin) error
	SaveCharacter(cast.Character) error
	SaveSkill(skill.Skill) error
	SkillDeps() skill.Deps
}

Saver is the part of the authoring library this package needs.

It is an interface rather than the library itself because internal/forge's own tests use this fixture, and importing forge here would be a cycle. Go's interfaces are structural, so *forge.Library satisfies this without either package knowing about the other.

Jump to

Keyboard shortcuts

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