Skip to content

Commit

Permalink
test: demonstrate old lockfile compat matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Oct 20, 2023
1 parent bf0a90d commit 3790a62
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,3 +1161,137 @@ 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
("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),
("1.38", Some(1), 1),
("1.38", Some(2), 2),
("1.38", Some(3), 3),
// v2 became 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),
("1.47", Some(1), 1),
("1.47", Some(2), 2),
("1.47", Some(3), 3),
// v3 became 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:?}"
);
}
}

0 comments on commit 3790a62

Please sign in to comment.