supercov-engine 0.0.55

Rust instrumentation, evidence, attribution, and query engine for Supercov
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! The whole Go lifecycle, on a module with more than one package.
//!
//! The frontend test proves instrumented Go compiles and reports the truth for
//! one file. This proves the parts around it: that the project is copied and
//! rewritten without touching the author's tree, that each package's test
//! binary writes evidence of its own and the run merges them, that decisions
//! numbered in different files do not collide, and that a run is published.
//!
//! Needs `go` on PATH and skips without it.

use std::path::{Path, PathBuf};
use std::process::Command;

use supercov_engine::go_run::{DirectGoRunRequest, run_direct_go};

mod common;

fn go_binary() -> Option<PathBuf> {
    // Homebrew's Go is not on a non-login shell's PATH on macOS.
    for candidate in ["go", "/opt/homebrew/bin/go", "/usr/local/go/bin/go"] {
        let path = PathBuf::from(candidate);
        if Command::new(&path)
            .arg("version")
            .output()
            .is_ok_and(|out| out.status.success())
        {
            return Some(path);
        }
    }
    None
}

fn temporary(label: &str) -> PathBuf {
    let root = std::env::temp_dir().join(format!(
        "supercov-go-run-{label}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&root).unwrap();
    root
}

fn write(root: &Path, relative: &str, contents: &str) {
    let path = root.join(relative);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, contents).unwrap();
}

/// Two packages, each with a decision of its own. If decisions were numbered
/// per file they would both be decision 0 and would share condition state.
fn fixture(root: &Path) {
    write(root, "go.mod", "module example.com/app\n\ngo 1.22\n");
    write(
        root,
        "auth/auth.go",
        "package auth\n\nfunc Allow(admin bool, active bool) bool {\n\tif admin && active {\n\t\treturn true\n\t}\n\treturn false\n}\n",
    );
    write(
        root,
        "auth/auth_test.go",
        "package auth\n\nimport \"testing\"\n\nfunc TestAllows(t *testing.T) {\n\tif !Allow(true, true) {\n\t\tt.Fatal(\"admin and active should be allowed\")\n\t}\n}\n\nfunc TestDenies(t *testing.T) {\n\tif Allow(false, true) {\n\t\tt.Fatal(\"a non-admin should be denied\")\n\t}\n}\n",
    );
    write(
        root,
        "billing/billing.go",
        "package billing\n\nfunc Charge(amount int, trial bool) int {\n\tif amount > 0 || trial {\n\t\treturn amount\n\t}\n\treturn 0\n}\n",
    );
    write(
        root,
        "billing/billing_test.go",
        "package billing\n\nimport \"testing\"\n\nfunc TestCharges(t *testing.T) {\n\tif Charge(5, false) != 5 {\n\t\tt.Fatal(\"a positive amount is charged\")\n\t}\n}\n\nfunc TestSkipsWhenAsked(t *testing.T) {\n\tt.Skip(\"not today\")\n}\n",
    );
}

#[test]
fn a_multi_package_module_runs_and_publishes_what_each_test_reached() {
    let Some(go) = go_binary() else {
        common::skip("go", "no Go toolchain found");
        return;
    };
    // The lifecycle shells out to the command as given, so `go` has to be
    // reachable by the name the command uses.
    let root = temporary("multi-package");
    fixture(&root);

    let before = std::fs::read_to_string(root.join("auth/auth.go")).unwrap();
    let request = DirectGoRunRequest {
        root: root.clone(),
        command: vec![go.display().to_string(), "test".into(), "./...".into()],
        run_id: "run-go-multi".into(),
        started_at: "2026-01-01T00:00:00.000Z".into(),
    };
    let mut diagnostics = Vec::new();
    let result = match run_direct_go(&request, &mut diagnostics) {
        Ok(result) => result,
        Err(error) => panic!(
            "run failed: {error}\n--- diagnostics ---\n{}",
            String::from_utf8_lossy(&diagnostics)
        ),
    };

    // The author's tree is untouched: instrumentation happens on a copy.
    assert_eq!(
        std::fs::read_to_string(root.join("auth/auth.go")).unwrap(),
        before,
        "the project's own sources must come back exactly as they went in"
    );

    assert_eq!(result.exit_code, 0);
    assert_eq!(result.packages, 2, "one test binary per package");
    assert_eq!(result.source_files, 2);
    assert_eq!(result.tests, 4);

    let archive = result.run_directory.join("evidence.raw.gz");
    let named = supercov_engine::evidence_archive::read_archive(&archive)
        .expect("published archive")
        .into_iter()
        .map(|entry| entry.path)
        .collect::<Vec<_>>();
    // A published run carries what the numbers mean, who produced them, the
    // obligations they are measured against, and one record per test.
    for required in ["coverage-model.json", "frontend.json", "manifest.json"] {
        assert!(named.iter().any(|path| path == required), "{named:?}");
    }
    assert_eq!(
        named
            .iter()
            .filter(|path| path.ends_with("mcdc.json"))
            .count(),
        4,
        "one record per test: {named:?}"
    );
}

#[test]
fn decisions_in_different_packages_keep_their_own_condition_state() {
    let Some(go) = go_binary() else {
        common::skip("go", "no Go toolchain found");
        return;
    };
    // `auth` evaluates `admin && active` and `billing` evaluates
    // `amount > 0 || trial`. Numbered per file they would both be decision 0,
    // sharing one slot in the runtime's state array: the vectors recorded
    // would describe neither expression, and MC/DC would be judged from an
    // evaluation that never happened.
    let root = temporary("decision-state");
    fixture(&root);

    let request = DirectGoRunRequest {
        root: root.clone(),
        command: vec![go.display().to_string(), "test".into(), "./...".into()],
        run_id: "run-go-decisions".into(),
        started_at: "2026-01-01T00:00:00.000Z".into(),
    };
    let mut diagnostics = Vec::new();
    let result = match run_direct_go(&request, &mut diagnostics) {
        Ok(result) => result,
        Err(error) => panic!(
            "run failed: {error}\n--- diagnostics ---\n{}",
            String::from_utf8_lossy(&diagnostics)
        ),
    };

    let archive = result.run_directory.join("evidence.raw.gz");
    assert!(archive.exists(), "{}", archive.display());
    let entries =
        supercov_engine::evidence_archive::read_archive(&archive).expect("published archive");
    let manifest = String::from_utf8(
        entries
            .iter()
            .find(|entry| entry.path == "manifest.json")
            .expect("a published run carries its manifest")
            .contents
            .clone(),
    )
    .expect("utf-8");
    // Two decisions, one per package, each with its own two conditions.
    let decisions = manifest.matches("\"conditions\"").count();
    assert_eq!(
        decisions, 2,
        "each package's decision is its own obligation:\n{manifest}"
    );
    assert!(manifest.contains("auth/auth.go"), "{manifest}");
    assert!(manifest.contains("billing/billing.go"), "{manifest}");
}

/// A go.work repository has several modules and no module at its root. Each
/// needs a runtime of its own, because an import resolves against the module
/// that names it and one module cannot import a package inside another.
#[test]
fn every_module_of_a_workspace_is_measured_and_merged() {
    let Some(go) = go_binary() else {
        common::skip("go", "no Go toolchain found");
        return;
    };
    let root = temporary("workspace");
    write(
        root.as_path(),
        "go.work",
        "go 1.22\n\nuse (\n\t./core\n\t./app\n)\n",
    );
    write(
        root.as_path(),
        "core/go.mod",
        "module example.com/core\n\ngo 1.22\n",
    );
    write(
        root.as_path(),
        "core/calc.go",
        "package core\n\nfunc Allow(admin, active bool) bool {\n\tif admin && active {\n\t\treturn true\n\t}\n\treturn false\n}\n",
    );
    write(
        root.as_path(),
        "core/calc_test.go",
        "package core\n\nimport \"testing\"\n\nfunc TestAllow(t *testing.T) {\n\tif !Allow(true, true) {\n\t\tt.Fatal(\"expected allow\")\n\t}\n}\n",
    );
    write(
        root.as_path(),
        "app/go.mod",
        "module example.com/app\n\ngo 1.22\n",
    );
    write(
        root.as_path(),
        "app/greet.go",
        "package app\n\nfunc Hi(loud bool) string {\n\tif loud {\n\t\treturn \"HI\"\n\t}\n\treturn \"hi\"\n}\n",
    );
    // One line, brace to brace: an announcement inserted after the brace with
    // nothing following it would run into this statement and stop being Go.
    write(
        root.as_path(),
        "app/greet_test.go",
        "package app\n\nimport \"testing\"\n\nfunc TestHi(t *testing.T) { if Hi(true) != \"HI\" { t.Fatal(\"expected HI\") } }\n",
    );

    let request = DirectGoRunRequest {
        root: root.clone(),
        command: vec![
            go.display().to_string(),
            "test".into(),
            "./core/...".into(),
            "./app/...".into(),
        ],
        run_id: "run-go-workspace".into(),
        started_at: "2026-01-01T00:00:00.000Z".into(),
    };
    let mut diagnostics = Vec::new();
    let result = match run_direct_go(&request, &mut diagnostics) {
        Ok(result) => result,
        Err(error) => panic!(
            "run failed: {error}\n--- diagnostics ---\n{}",
            String::from_utf8_lossy(&diagnostics)
        ),
    };
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.packages, 2);
    assert_eq!(result.source_files, 2);
    assert_eq!(result.tests, 2);

    let entries = supercov_engine::evidence_archive::read_archive(
        &result.run_directory.join("evidence.raw.gz"),
    )
    .expect("published archive");
    let manifest = String::from_utf8(
        entries
            .into_iter()
            .find(|entry| entry.path == "manifest.json")
            .expect("manifest")
            .contents,
    )
    .expect("utf-8");
    assert!(manifest.contains("core/calc.go"), "{manifest}");
    assert!(manifest.contains("app/greet.go"), "{manifest}");
    std::fs::remove_dir_all(root).ok();
}

/// A directory with a go.mod that the workspace does not name is a different
/// module. `go test ./...` walks straight past it, so instrumenting it puts
/// obligations in the denominator no test can reach and writes a probe file
/// importing a runtime that module cannot resolve -- which stops a build that
/// worked before Supercov was asked to measure it.
#[test]
fn a_nested_module_does_not_break_the_build_around_it() {
    let Some(go) = go_binary() else {
        common::skip("go", "no Go toolchain found");
        return;
    };
    let root = temporary("nested");
    write(
        root.as_path(),
        "go.mod",
        "module example.com/root\n\ngo 1.22\n",
    );
    write(
        root.as_path(),
        "top.go",
        "package root\n\nfunc Top(x bool) int {\n\tif x {\n\t\treturn 1\n\t}\n\treturn 0\n}\n",
    );
    write(
        root.as_path(),
        "top_test.go",
        "package root\n\nimport \"testing\"\n\nfunc TestTop(t *testing.T) { if Top(true) != 1 { t.Fatal(\"no\") } }\n",
    );
    write(
        root.as_path(),
        "sub/go.mod",
        "module example.com/sub\n\ngo 1.22\n",
    );
    write(
        root.as_path(),
        "sub/sub.go",
        "package sub\n\nfunc Inner(x bool) int {\n\tif x {\n\t\treturn 2\n\t}\n\treturn 0\n}\n",
    );

    let request = DirectGoRunRequest {
        root: root.clone(),
        command: vec![go.display().to_string(), "test".into(), "./...".into()],
        run_id: "run-go-nested".into(),
        started_at: "2026-01-01T00:00:00.000Z".into(),
    };
    let mut diagnostics = Vec::new();
    let result = match run_direct_go(&request, &mut diagnostics) {
        Ok(result) => result,
        Err(error) => panic!(
            "run failed: {error}\n--- diagnostics ---\n{}",
            String::from_utf8_lossy(&diagnostics)
        ),
    };
    assert_eq!(result.exit_code, 0, "the build must still work");
    // Only the root module's source: the nested one is not part of this run.
    assert_eq!(result.source_files, 1);
    assert_eq!(result.tests, 1);
    std::fs::remove_dir_all(root).ok();
}

/// A Go test that calls `t.Parallel()` is deliberately left unattributed:
/// probes are a store into one shared array, so what it reaches while others
/// run beside it cannot be credited to it. The declaration says its coverage
/// still counts run-wide — and until there was a record to carry it, nothing
/// did. A suite written the way Go suites are written measured almost nothing
/// and was told nothing about why: samber/lo calls `t.Parallel()` 1606 times
/// across 548 tests, and reported 0.26% of its statements.
#[test]
fn a_parallel_tests_coverage_counts_even_though_no_test_can_claim_it() {
    let Some(go) = go_binary() else {
        common::skip("go", "no Go toolchain found");
        return;
    };
    let root = temporary("parallel");
    write(
        root.as_path(),
        "go.mod",
        "module example.com/par\n\ngo 1.22\n",
    );
    write(
        root.as_path(),
        "lib.go",
        "package par\n\nfunc Serial(x bool) int {\n\tif x {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc Parallel(x bool) int {\n\tif x {\n\t\treturn 2\n\t}\n\treturn 0\n}\n\nfunc Never(x bool) int {\n\tif x {\n\t\treturn 3\n\t}\n\treturn 0\n}\n",
    );
    write(
        root.as_path(),
        "lib_test.go",
        "package par\n\nimport \"testing\"\n\nfunc TestSerial(t *testing.T) {\n\tif Serial(true) != 1 {\n\t\tt.Fatal(\"serial\")\n\t}\n}\n\nfunc TestParallel(t *testing.T) {\n\tt.Parallel()\n\tif Parallel(true) != 2 {\n\t\tt.Fatal(\"parallel\")\n\t}\n}\n",
    );

    let request = DirectGoRunRequest {
        root: root.clone(),
        command: vec![go.display().to_string(), "test".into(), "./...".into()],
        run_id: "run-go-parallel".into(),
        started_at: "2026-01-01T00:00:00.000Z".into(),
    };
    let mut diagnostics = Vec::new();
    let result = match run_direct_go(&request, &mut diagnostics) {
        Ok(result) => result,
        Err(error) => panic!(
            "run failed: {error}\n--- diagnostics ---\n{}",
            String::from_utf8_lossy(&diagnostics)
        ),
    };
    assert_eq!(result.exit_code, 0);
    // Only the serial one can be named.
    assert_eq!(result.tests, 1);

    let records = supercov_engine::evidence_archive::read_archive(
        &result.run_directory.join("evidence.raw.gz"),
    )
    .expect("published archive")
    .into_iter()
    .filter(|entry| entry.path.ends_with("mcdc.json"))
    .map(|entry| String::from_utf8(entry.contents).expect("utf-8"))
    .collect::<Vec<_>>();

    let background = records
        .iter()
        .find(|record| record.contains("\"role\":\"background\""))
        .expect("the run carries what no test could claim");
    // What the parallel test reached is in it, and what nothing reached is not.
    assert!(background.contains("go:statement:"), "{background}");
    // It counts as coverage only because the run passed. A failing run cannot
    // say which of this came from the test that failed, so it would not.
    assert!(background.contains("\"status\":\"passed\""), "{background}");
    // And the serial test still claims its own, rather than everything being
    // swept into the background record.
    assert!(
        records
            .iter()
            .any(|record| record.contains("TestSerial") && record.contains("go:statement:")),
        "{records:?}"
    );
    std::fs::remove_dir_all(root).ok();
}