use super::*;
use crate::core::backend::GeneratedFile;
use std::path::PathBuf;
const OLD_VERSION: &str = "0.1.0";
const NEW_VERSION: &str = "0.2.0";
fn manifest_file(path: &str, version: &str) -> GeneratedFile {
GeneratedFile {
path: PathBuf::from(path),
content: format!(
"[workspace]\nmembers = []\n\n[package]\nname = \"sample-native\"\nversion = \"{version}\"\nedition = \
\"2024\"\n"
),
generated_header: true,
}
}
fn lib_rs_file(path: &str) -> GeneratedFile {
GeneratedFile {
path: PathBuf::from(path),
content: String::new(),
generated_header: false,
}
}
#[test]
fn write_scaffold_files_report_relocks_a_changed_nested_manifests_lockfile() {
let tmp = tempfile::tempdir().expect("tempdir");
let base = tmp.path();
let manifest_path = "packages/example/native/Cargo.toml";
let lib_path = "packages/example/native/src/lib.rs";
write_scaffold_files_report(
&[manifest_file(manifest_path, OLD_VERSION), lib_rs_file(lib_path)],
base,
false,
)
.expect("initial scaffold write ok");
let lock_path = base.join("packages/example/native/Cargo.lock");
std::fs::write(
&lock_path,
format!("version = 4\n\n[[package]]\nname = \"sample-native\"\nversion = \"{OLD_VERSION}\"\n"),
)
.expect("seed stale Cargo.lock");
let new_files = [manifest_file(manifest_path, NEW_VERSION), lib_rs_file(lib_path)];
let report = write_scaffold_files_report(&new_files, base, false).expect("regenerated scaffold write ok");
assert!(
report.changed_paths.contains(&base.join(manifest_path)),
"the manifest write must be reported as changed: {:?}",
report.changed_paths
);
let manifest = std::fs::read_to_string(base.join(manifest_path)).expect("read regenerated Cargo.toml");
assert!(
manifest.contains(&format!("version = \"{NEW_VERSION}\"")),
"the regenerated manifest must carry the new version, got:\n{manifest}"
);
let lock = std::fs::read_to_string(&lock_path).expect("read relocked Cargo.lock");
assert!(
lock.contains(&format!("version = \"{NEW_VERSION}\"")),
"write_scaffold_files_report must relock the sibling Cargo.lock to {NEW_VERSION}, not just \
rewrite the manifest next to a stale lock, got:\n{lock}"
);
assert!(
!lock.contains(&format!("version = \"{OLD_VERSION}\"")),
"the stale {OLD_VERSION} pin must be gone from the relocked Cargo.lock, got:\n{lock}"
);
}
#[test]
fn write_scaffold_files_report_does_not_touch_a_lockfile_whose_manifest_did_not_change() {
let tmp = tempfile::tempdir().expect("tempdir");
let base = tmp.path();
let manifest_path = "packages/example/native/Cargo.toml";
let lib_path = "packages/example/native/src/lib.rs";
let untouched_lock_path = base.join("packages/other/native/Cargo.lock");
write_scaffold_files_report(
&[manifest_file(manifest_path, OLD_VERSION), lib_rs_file(lib_path)],
base,
false,
)
.expect("initial scaffold write ok");
std::fs::create_dir_all(untouched_lock_path.parent().expect("has parent")).expect("mkdir");
const SENTINEL: &str = "version = 4\n\n[[package]]\nname = \"unrelated\"\nversion = \"9.9.9\"\n";
std::fs::write(&untouched_lock_path, SENTINEL).expect("seed unrelated Cargo.lock");
write_scaffold_files_report(
&[manifest_file(manifest_path, NEW_VERSION), lib_rs_file(lib_path)],
base,
false,
)
.expect("regenerated scaffold write ok");
let untouched = std::fs::read_to_string(&untouched_lock_path).expect("read unrelated Cargo.lock");
assert_eq!(
untouched, SENTINEL,
"a Cargo.lock beside a manifest this run never wrote must be left byte-for-byte untouched"
);
}