test: demonstrate old lockfile compat matrix

This commit is contained in:
Weihang Lo 2023-10-19 21:16:46 -04:00
parent 7b0919399d
commit 92b82d6134
No known key found for this signature in database
GPG key ID: D7DBF189825E82E7

View file

@ -1157,3 +1157,135 @@ fn v4_and_git_url_encoded_tag() {
fn v4_and_git_url_encoded_rev() {
v4_and_git_url_encoded("rev", create_tag)
}
#[cargo_test]
fn with_msrv() {
let cksum = Package::new("bar", "0.1.0").publish();
let v2_lockfile = format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bar"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{cksum}"
[[package]]
name = "foo"
version = "0.0.1"
dependencies = [
"bar",
]
"#
);
let v1_lockfile = format!(
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bar"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "foo"
version = "0.0.1"
dependencies = [
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "{cksum}"
"#
);
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.1.0"
"#,
)
.file("src/lib.rs", "")
.build();
let cases = [
// v1 is the default
("1.37", None, 3),
("1.37", Some(1), 1),
("1.37", Some(2), 2),
("1.37", Some(3), 3),
// v2 introduced
("1.38", None, 3),
// last version of v1 as the default
("1.40", None, 3),
// v2 is the default
("1.41", None, 3),
("1.41", Some(1), 1),
("1.41", Some(2), 2),
("1.41", Some(3), 3),
// v3 introduced
("1.47", None, 3),
// last version of v2 as the default
("1.48", None, 3),
// v3 is the default
("1.53", None, 3),
("1.53", Some(1), 1),
("1.53", Some(2), 2),
("1.53", Some(3), 3),
];
for (msrv, existing_lockfile, expected_version) in cases {
// Clean previous lockfile.
_ = std::fs::remove_file(p.root().join("Cargo.lock"));
p.change_file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
rust-version = "{msrv}"
[dependencies]
bar = "0.1.0"
"#,
),
);
if let Some(existing_lockfile) = existing_lockfile {
let existing_lockfile = match existing_lockfile {
1 => v1_lockfile.as_str().into(),
2 => v2_lockfile.as_str().into(),
v => std::borrow::Cow::from(format!("version = {v}")),
};
p.change_file("Cargo.lock", &existing_lockfile);
}
p.cargo("fetch").run();
let lock = p.read_lockfile();
let toml = lock.parse::<toml::Table>().unwrap();
// get `version = <n>` from Cargo.lock
let version_field = toml.get("version").and_then(|v| v.as_integer());
let actual_version = if let Some(ver) = version_field {
ver
} else if lock.find("\nchecksum = ").is_some() {
2
} else {
1
};
assert_eq!(
expected_version, actual_version,
"msrv: {msrv}, existing lockfile: {existing_lockfile:?}"
);
}
}