use super::{BloatDir, EnforcePolicy, PackageManager, dir_size, enforce_two_tier};
use anyhow::Result;
use std::path::{Path, PathBuf};
pub struct Cargo;
fn workspace_lockfile(path: &Path) -> Option<PathBuf> {
let mut dir = path;
loop {
let candidate = dir.join("Cargo.lock");
if candidate.exists() {
return Some(candidate);
}
if dir.join(".git").exists() {
return None;
}
dir = dir.parent()?;
}
}
impl PackageManager for Cargo {
fn name(&self) -> &'static str {
"cargo"
}
fn detect(&self, path: &Path) -> bool {
path.join("Cargo.toml").exists()
}
fn bloat_dirs(&self, path: &Path) -> Vec<BloatDir> {
let mut dirs = Vec::new();
let target_path = path.join("target");
if target_path.exists() {
dirs.push(BloatDir {
name: "target".to_string(),
path: target_path.clone(),
size_bytes: dir_size(&target_path),
shared_bytes: 0,
});
}
dirs
}
fn enforce_lockfile(&self, path: &Path, policy: EnforcePolicy) -> Result<()> {
if path.join("target").join("criterion").is_dir() {
crate::output::print_warning(&format!(
"{}: `target/criterion` holds benchmark history that a rebuild does not \
bring back — copy it first if the baselines matter.",
crate::output::clean_path(path)
));
}
let lockfile = workspace_lockfile(path).unwrap_or_else(|| path.join("Cargo.lock"));
enforce_two_tier(
&lockfile,
"cargo",
&["metadata", "--locked", "--format-version", "1"],
&["generate-lockfile"],
path,
policy,
)
}
fn restore(&self, _path: &Path, _timeout: std::time::Duration) -> Result<()> {
println!("Rust target/ will regenerate on next cargo build");
Ok(())
}
fn lockfiles(&self) -> &'static [&'static str] {
&["Cargo.lock"]
}
fn opt_in(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::fs::File;
use tempfile::tempdir;
#[test]
fn cargo_is_opt_in() {
assert!(Cargo.opt_in());
}
#[test]
fn test_name() {
let adapter = Cargo;
assert_eq!(adapter.name(), "cargo");
}
#[test]
fn a_default_pass_never_rewrites_a_stale_lockfile() {
if !super::super::binary_available("cargo") {
return;
}
let dir = tempdir().unwrap();
fs::write(
dir.path().join("Cargo.toml"),
"[package]\nname = \"stale\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n\
[dependencies]\nserde = \"1\"\n",
)
.unwrap();
fs::create_dir(dir.path().join("src")).unwrap();
fs::write(dir.path().join("src").join("lib.rs"), "").unwrap();
let stale = "version = 3\n\n[[package]]\nname = \"stale\"\nversion = \"0.1.0\"\n";
fs::write(dir.path().join("Cargo.lock"), stale).unwrap();
let result = Cargo.enforce_lockfile(dir.path(), EnforcePolicy::default());
assert!(
result.is_err(),
"a lockfile that cannot resolve the manifest must not pass verification"
);
assert_eq!(
fs::read_to_string(dir.path().join("Cargo.lock")).unwrap(),
stale,
"the read-only verification rewrote Cargo.lock"
);
}
#[test]
fn test_detect_positive() {
let dir = tempdir().unwrap();
File::create(dir.path().join("Cargo.toml")).unwrap();
let adapter = Cargo;
assert!(adapter.detect(dir.path()));
}
#[test]
fn test_detect_negative() {
let dir = tempdir().unwrap();
let adapter = Cargo;
assert!(!adapter.detect(dir.path()));
}
#[test]
fn test_bloat_dirs_present() {
let dir = tempdir().unwrap();
fs::create_dir(dir.path().join("target")).unwrap();
let adapter = Cargo;
let dirs = adapter.bloat_dirs(dir.path());
assert_eq!(dirs.len(), 1);
assert_eq!(dirs[0].name, "target");
}
#[test]
fn test_bloat_dirs_absent() {
let dir = tempdir().unwrap();
let adapter = Cargo;
let dirs = adapter.bloat_dirs(dir.path());
assert!(dirs.is_empty());
}
}