use std::collections::BTreeSet;
use std::path::Path;
use super::worker::Runner;
use crate::lsp::manifest::{self, RunnerKindDecl, TestRunnerDecl};
fn kind_of(decl: RunnerKindDecl) -> Runner {
match decl {
RunnerKindDecl::Cargo => Runner::Cargo,
RunnerKindDecl::Pytest => Runner::Pytest,
RunnerKindDecl::Vitest => Runner::Vitest,
RunnerKindDecl::Jest => Runner::Jest,
}
}
fn package_declares(root: &Path, deps: &[String]) -> bool {
if deps.is_empty() {
return false;
}
let Ok(text) = std::fs::read_to_string(root.join("package.json")) else {
return false;
};
let Ok(pkg) = serde_json::from_str::<serde_json::Value>(&text) else {
return false;
};
["devDependencies", "dependencies"].iter().any(|section| {
pkg.get(section)
.and_then(|d| d.as_object())
.is_some_and(|d| deps.iter().any(|dep| d.contains_key(dep)))
})
}
fn decl_matches(decl: &TestRunnerDecl, root: &Path) -> bool {
decl.markers.iter().any(|m| root.join(m).is_file())
|| package_declares(root, &decl.package_deps)
}
fn resolve(sources: &[String], disabled: &BTreeSet<String>, root: &Path) -> Option<Runner> {
sources
.iter()
.filter_map(|s| manifest::parse(s).ok())
.flat_map(|m| {
let id = m.id;
m.test_runners.into_iter().map(move |d| (id.clone(), d))
})
.filter(|(id, _)| !disabled.contains(id))
.find(|(_, d)| decl_matches(d, root))
.map(|(_, d)| kind_of(d.kind))
}
fn all_sources() -> Vec<String> {
let mut sources: Vec<String> = manifest::BUNDLED_MANIFESTS
.iter()
.map(|s| s.to_string())
.collect();
sources.extend(manifest::read_extension_sources(
&manifest::user_extensions_dir(),
));
sources
}
pub fn runner_for(root: &Path) -> Option<Runner> {
let disabled = crate::prefs::Prefs::load_or_default().disabled_extensions;
resolve(&all_sources(), &disabled, root)
}
#[cfg(test)]
mod tests {
use super::*;
fn bundled() -> Vec<String> {
manifest::BUNDLED_MANIFESTS
.iter()
.map(|s| s.to_string())
.collect()
}
fn none() -> BTreeSet<String> {
BTreeSet::new()
}
#[test]
fn bundled_manifests_prefer_cargo_then_python_markers() {
let s = bundled();
let tmp = tempfile::tempdir().unwrap();
assert_eq!(
resolve(&s, &none(), tmp.path()),
None,
"no manifest, no runner"
);
std::fs::write(tmp.path().join("pyproject.toml"), "[project]\n").unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Pytest));
std::fs::write(tmp.path().join("Cargo.toml"), "[package]\n").unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Cargo));
let py = tempfile::tempdir().unwrap();
std::fs::write(py.path().join("pytest.ini"), "[pytest]\n").unwrap();
assert_eq!(resolve(&s, &none(), py.path()), Some(Runner::Pytest));
}
#[test]
fn bundled_manifests_identify_js_runners_from_package_json() {
let s = bundled();
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("package.json"),
r#"{"devDependencies":{"vitest":"^3.0.0"}}"#,
)
.unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Vitest));
std::fs::write(
tmp.path().join("package.json"),
r#"{"devDependencies":{"jest":"^30.0.0"}}"#,
)
.unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Jest));
std::fs::write(
tmp.path().join("package.json"),
r#"{"dependencies":{"react":"^19.0.0"}}"#,
)
.unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), None);
std::fs::write(tmp.path().join("vitest.config.ts"), "").unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Vitest));
std::fs::write(tmp.path().join("Cargo.toml"), "[package]\n").unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Cargo));
let jest_cfg = tempfile::tempdir().unwrap();
std::fs::write(jest_cfg.path().join("package.json"), "{}").unwrap();
std::fs::write(jest_cfg.path().join("jest.config.js"), "").unwrap();
assert_eq!(resolve(&s, &none(), jest_cfg.path()), Some(Runner::Jest));
}
#[test]
fn a_disabled_runner_extension_stops_claiming_its_projects() {
let s = bundled();
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("Cargo.toml"), "[package]\n").unwrap();
std::fs::write(tmp.path().join("pyproject.toml"), "[project]\n").unwrap();
let mut disabled = BTreeSet::new();
disabled.insert("test-cargo".to_string());
assert_eq!(resolve(&s, &disabled, tmp.path()), Some(Runner::Pytest));
disabled.insert("test-pytest".to_string());
assert_eq!(resolve(&s, &disabled, tmp.path()), None);
}
#[test]
fn a_user_manifest_can_retarget_a_built_in_mechanism_to_new_markers() {
let mut s = bundled();
s.push(
r#"
id = "test-nox"
name = "Nox Test Runner"
api_version = 1
[[test_runners]]
id = "test-nox"
label = "pytest (nox)"
kind = "pytest"
markers = ["noxfile.py"]
"#
.to_string(),
);
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("noxfile.py"), "").unwrap();
assert_eq!(resolve(&s, &none(), tmp.path()), Some(Runner::Pytest));
}
}