use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;
use super::follow_up;
fn tree(files: &[&str]) -> TempDir {
let root = TempDir::new().unwrap();
for rel in files {
let path = root.path().join(rel);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, "").unwrap();
}
root
}
fn repo_at(dir: &Path) {
assert!(
Command::new("git")
.current_dir(dir)
.args(["init", "-q", "-b", "main"])
.status()
.unwrap()
.success()
);
}
#[test]
fn a_cargo_lock_beside_the_manifest_asks_for_the_narrowest_refresh() {
let root = tree(&["Cargo.toml", "Cargo.lock"]);
assert_eq!(
follow_up(&root.path().join("Cargo.toml")),
Some("cargo update --workspace".into())
);
}
#[test]
fn a_workspace_member_finds_the_lock_at_the_repository_root() {
let root = tree(&["Cargo.lock", "crates/member/Cargo.toml"]);
repo_at(root.path());
assert_eq!(
follow_up(&root.path().join("crates/member/Cargo.toml")),
Some("cargo update --workspace".into())
);
}
#[test]
fn a_workspace_member_runs_its_owner_beside_the_lock() {
let root = tree(&["Cargo.lock", "crates/member/Cargo.toml"]);
repo_at(root.path());
let owner = super::owner(&root.path().join("crates/member/Cargo.toml")).unwrap();
let top = root.path().canonicalize().unwrap();
assert_eq!(owner.dir, top);
assert_eq!(owner.lockfile, Some(top.join("Cargo.lock")));
}
#[test]
fn a_cargo_lock_above_the_repository_is_not_ours() {
let root = tree(&["Cargo.lock", "checkout/Cargo.toml"]);
repo_at(&root.path().join("checkout"));
assert_eq!(follow_up(&root.path().join("checkout/Cargo.toml")), None);
}
#[test]
fn without_a_repository_the_search_stays_in_the_directory() {
let root = tree(&["Cargo.lock", "member/Cargo.toml"]);
assert_eq!(follow_up(&root.path().join("member/Cargo.toml")), None);
}
#[test]
fn a_bun_lock_at_the_repository_root_serves_a_package() {
let root = tree(&["bun.lock", "packages/pkg/package.json"]);
repo_at(root.path());
assert_eq!(
follow_up(&root.path().join("packages/pkg/package.json")),
Some("bun install".into())
);
}
#[test]
fn a_bun_lock_above_the_repository_is_not_ours() {
let root = tree(&["bun.lock", "checkout/package.json"]);
repo_at(&root.path().join("checkout"));
assert_eq!(follow_up(&root.path().join("checkout/package.json")), None);
}
#[test]
fn a_uv_lock_at_the_repository_root_serves_a_python_package() {
let root = tree(&["uv.lock", "tools/cli/pyproject.toml"]);
repo_at(root.path());
assert_eq!(
follow_up(&root.path().join("tools/cli/pyproject.toml")),
Some("uv lock".into())
);
}
#[test]
fn a_python_package_without_a_uv_lock_has_no_follow_up() {
let root = tree(&["pyproject.toml"]);
assert_eq!(follow_up(&root.path().join("pyproject.toml")), None);
}
#[test]
fn the_package_manager_field_outranks_the_lockfile() {
let root = TempDir::new().unwrap();
fs::write(root.path().join("bun.lock"), "").unwrap();
fs::write(
root.path().join("package.json"),
r#"{"packageManager":"pnpm@10.0.0"}"#,
)
.unwrap();
assert_eq!(
follow_up(&root.path().join("package.json")),
Some("pnpm install".into())
);
}