How it works

On this page

One package, step by step #

sequenceDiagram
    autonumber
    participant G as gomagi
    participant C as go test -c
    participant B as test binary
    G->>C: rewrite every operator into a switch, compile once (overlay)
    C-->>G: shared binary
    G->>C: compile the original package with coverage
    C-->>G: coverage binary
    G->>B: run the whole suite without a mutant
    B-->>G: pass, and its duration
    loop each top-level test, in parallel
        G->>B: run the test alone with coverage
        B-->>G: lines it reaches, how often, and its duration
    end
    loop each mutant, in parallel
        G->>B: GOMAGI_ACTIVE=id, the covering tests, fastest first
        B-->>G: first failure: killed
        opt every covering test passed
            G->>B: once per package, the whole suite with a recorder
            B-->>G: the mutants that would change a value
        end
        opt every covering test passed and the mutant would change a value
            G->>B: the whole suite
            B-->>G: killed, or lived
        end
    end
    G->>G: undo what the mutants wrote into the package directory
  1. gomagi replaces each operator it can mutate with a call to a small helper, keeping every line number and evaluating every operand once and in order. The rewritten files reach the compiler through -overlay, so nothing in your module changes. With no mutant selected the helpers do what the operator did, and the compiler inlines them all. Each function keeps its body as written beside the switched copy, which runs only when one of that function’s mutants is active, so the rest of the package runs at its usual speed; a //line directive keeps the copy’s positions those of the original. A test that builds the package with go build gets the same overlay through GOFLAGS.
  2. Each top-level test runs alone with coverage, child processes of its own binary included. The lines it reaches decide which mutants it can kill, and its duration orders the tests.
  3. Each mutant runs only the tests that reach its line, fastest first, and stops at the first failure.
  4. A mutant those tests let live runs the whole suite in its usual order before it counts as lived, since a later test can read what a covering test left behind. --covering-only skips this step.
  5. When the first survivor of a package reaches step 4, the switched sources compile once more with a recorder in place of the helpers, and the whole suite runs once with it and no mutant; a package whose mutants all die never pays for it. Each helper of the recorder computes the original operator and also its mutants, and notes a mutant whose operator would have given another value, or would have changed what runs, as && turned into || does. Until that first different value a mutant runs exactly as the original does, so a survivor the recorder never noted passes the whole suite as it passed without a mutant, and step 4 is left out for it. A mutant compiled on its own, or one whose suite run leaves out colliding tests, still runs step 4.

Every run has exactly one mutant active: two active mutants can hide each other.

Packages side by side #

Packages overlap: one compiles while another’s mutants run, and all their test processes share --workers. On Linux a process whose tests mostly wait, such as tests that sleep, takes only part of a worker. A run that must be alone, such as one with a colliding test, waits for every other test process.

Time and memory #

  • Inside the test binary, a mutant that runs 100 times as often as its line runs without it, and at least a million times, stops its process as an endless loop. The run goes once more with ten times the count before it counts as timed out, since some mutants run far more often and still end.
  • A run gets ten times as long as its tests took without the mutant, and once more four times as long before it counts as timed out.
  • A process over --memory-limit counts as killed. On Linux gomagi watches its resident memory; elsewhere the test binary checks its heap after each garbage collection.

Flaky tests #

  • The suite runs without a mutant first, up to three times. A test that fails and then passes is flaky.
  • A kill counts only when the failure repeats. The repeat runs only the failing tests, and the whole selection when they pass alone.
  • A kill by a flaky test, a colliding test or a test that never reached the mutant’s line must repeat twice more.
  • Such a kill then runs the failing tests once more without the mutant. When they fail without it too, the machine failed them, as a test does that cannot open a local port while other suites hold them all: the mutant runs again after 5 seconds, then 20, then a minute, and gomagi stops the package with an error when they still fail.

Mutants compiled one at a time #

An operator that cannot be switched at run time, such as one in a constant declaration or an array length, gets a binary of its own per mutant, as gremlins does for every mutant. --verify N builds N switched mutants per package that way and compares the outcomes.

Running in your package directory #

Test binaries run in the package directory, as under go test.

  • Each test process gets its own TMPDIR, and TMP and TEMP on Windows.
  • A test that fails beside other processes but passes alone collides, for example by taking a fixed port. Mutants run without such tests, and a survivor then runs the whole suite alone with them. A collision gomagi cannot see still needs --workers 1.
  • pgregory.net/rapid properties run with RAPID_SHRINKTIME=0s, RAPID_SEED=1 unless you set one, and RAPID_NOFAILFILE=true, so they stay short and draw the same inputs in every run.

Before a package’s mutants run, gomagi records the files of its directory and of its non-package subdirectories such as testdata. After the last mutant it removes what appeared, restores what changed and names both. A new Go file, which may be yours, is moved instead to gomagi/moved in your cache directory, and one in the package directory itself is moved before the next build, since the package would not compile with it. A changed Go source file is only reported. Leave those directories alone until the package finishes.