use super::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TestBinary {
pub path: PathBuf,
pub target: String,
pub src_path: PathBuf,
pub integration: bool,
}
pub fn test_binary_candidates(output: &str) -> Vec<TestBinary> {
let mut ranked: Vec<(u8, TestBinary)> = Vec::new();
for line in output.lines() {
let Ok(v) = serde_json::from_str::<serde_json::Value>(line) else {
continue;
};
if v.get("reason").and_then(|r| r.as_str()) != Some("compiler-artifact")
|| v.get("profile")
.and_then(|p| p.get("test"))
.and_then(|t| t.as_bool())
!= Some(true)
{
continue;
}
let Some(exe) = v.get("executable").and_then(|e| e.as_str()) else {
continue;
};
let target = v.get("target");
let has_kind = |want: &str| {
target
.and_then(|t| t.get("kind"))
.and_then(|k| k.as_array())
.is_some_and(|kinds| kinds.iter().filter_map(|k| k.as_str()).any(|k| k == want))
};
let kind_rank = if has_kind("lib") {
0
} else if has_kind("bin") {
1
} else {
2
};
ranked.push((
kind_rank,
TestBinary {
path: PathBuf::from(exe),
target: target
.and_then(|t| t.get("name"))
.and_then(|n| n.as_str())
.unwrap_or("")
.to_string(),
src_path: PathBuf::from(
target
.and_then(|t| t.get("src_path"))
.and_then(|s| s.as_str())
.unwrap_or(""),
),
integration: has_kind("test"),
},
));
}
ranked.sort_by_key(|(r, _)| *r); ranked.into_iter().map(|(_, c)| c).collect()
}
pub fn binary_containing_test(candidates: &[PathBuf], name: &str) -> Option<PathBuf> {
let qualified = format!("::{name}");
for exe in candidates {
let Ok(out) = Command::new(exe)
.args([name, "--list"])
.stdin(Stdio::null())
.output()
else {
continue;
};
let stdout = String::from_utf8_lossy(&out.stdout);
let listed = stdout.lines().any(|l| {
l.trim_end()
.strip_suffix(": test")
.is_some_and(|t| t == name || t.ends_with(&qualified))
});
if listed {
return Some(exe.clone());
}
}
None
}
pub fn build_test_binary(
root: &Path,
name: &str,
source: Option<&Path>,
) -> std::io::Result<PathBuf> {
let out = cargo_cmd(root, &["test", "--no-run", "--message-format=json"]).output()?;
let stderr = String::from_utf8_lossy(&out.stderr);
if !out.status.success() {
return Err(std::io::Error::other(format!(
"cargo test --no-run failed: {}",
stderr
.lines()
.rev()
.find(|l| l.starts_with("error"))
.or_else(|| stderr.lines().last())
.unwrap_or("")
.trim()
)));
}
let stdout = String::from_utf8_lossy(&out.stdout);
let all = test_binary_candidates(&stdout);
if all.is_empty() {
return Err(std::io::Error::other(format!(
"no test binary in cargo's build output: {}",
stderr.lines().last().unwrap_or("")
)));
}
let pool: Vec<&TestBinary> = match source {
Some(src) => {
let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
let src = canon(src);
let exact: Vec<&TestBinary> =
all.iter().filter(|c| canon(&c.src_path) == src).collect();
if exact.is_empty() {
let is_integration_src = src.strip_prefix(canon(root)).is_ok_and(|rel| {
rel.components()
.next()
.is_some_and(|c| c.as_os_str() == "tests")
});
let side: Vec<&TestBinary> = all
.iter()
.filter(|c| c.integration == is_integration_src)
.collect();
if side.is_empty() {
return Err(std::io::Error::other(format!(
"no {} harness in the build owns {}",
if is_integration_src {
"integration-test"
} else {
"unit-test"
},
src.display()
)));
}
side
} else {
exact
}
}
None => all.iter().collect(),
};
match pool.as_slice() {
[only] => Ok(only.path.clone()),
_ => {
let paths: Vec<PathBuf> = pool.iter().map(|c| c.path.clone()).collect();
binary_containing_test(&paths, name).ok_or_else(|| {
std::io::Error::other(format!(
"none of the {} test harnesses lists a test named {name}",
paths.len()
))
})
}
}
}