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> {
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();
}
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;
};
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)
),
};
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<_>>();
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;
};
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");
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}");
}
#[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",
);
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();
}
#[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");
assert_eq!(result.source_files, 1);
assert_eq!(result.tests, 1);
std::fs::remove_dir_all(root).ok();
}
#[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);
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");
assert!(background.contains("go:statement:"), "{background}");
assert!(background.contains("\"status\":\"passed\""), "{background}");
assert!(
records
.iter()
.any(|record| record.contains("TestSerial") && record.contains("go:statement:")),
"{records:?}"
);
std::fs::remove_dir_all(root).ok();
}