Skip to content

Commit 9e0cdc2

Browse files
committed
test: demonstrate old lockfile compat matrix
1 parent 3beeb28 commit 9e0cdc2

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/testsuite/lockfile_compat.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,3 +1155,135 @@ fn v4_and_git_url_encoded_tag() {
11551155
fn v4_and_git_url_encoded_rev() {
11561156
v4_and_git_url_encoded("rev", create_tag)
11571157
}
1158+
1159+
#[cargo_test]
1160+
fn with_msrv() {
1161+
let cksum = Package::new("bar", "0.1.0").publish();
1162+
let v2_lockfile = format!(
1163+
r#"# This file is automatically @generated by Cargo.
1164+
# It is not intended for manual editing.
1165+
[[package]]
1166+
name = "bar"
1167+
version = "0.1.0"
1168+
source = "registry+https://github.com/rust-lang/crates.io-index"
1169+
checksum = "{cksum}"
1170+
1171+
[[package]]
1172+
name = "foo"
1173+
version = "0.0.1"
1174+
dependencies = [
1175+
"bar",
1176+
]
1177+
"#
1178+
);
1179+
1180+
let v1_lockfile = format!(
1181+
r#"# This file is automatically @generated by Cargo.
1182+
# It is not intended for manual editing.
1183+
[[package]]
1184+
name = "bar"
1185+
version = "0.1.0"
1186+
source = "registry+https://github.com/rust-lang/crates.io-index"
1187+
1188+
[[package]]
1189+
name = "foo"
1190+
version = "0.0.1"
1191+
dependencies = [
1192+
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
1193+
]
1194+
1195+
[metadata]
1196+
"checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "{cksum}"
1197+
"#
1198+
);
1199+
1200+
let p = project()
1201+
.file(
1202+
"Cargo.toml",
1203+
r#"
1204+
[package]
1205+
name = "foo"
1206+
version = "0.0.1"
1207+
1208+
[dependencies]
1209+
bar = "0.1.0"
1210+
"#,
1211+
)
1212+
.file("src/lib.rs", "")
1213+
.build();
1214+
1215+
let cases = [
1216+
// v1 is the default
1217+
("1.37", None, 3),
1218+
("1.37", Some(1), 1),
1219+
("1.37", Some(2), 2),
1220+
("1.37", Some(3), 3),
1221+
// v2 introduced
1222+
("1.38", None, 3),
1223+
// last version of v1 as the default
1224+
("1.40", None, 3),
1225+
// v2 is the default
1226+
("1.41", None, 3),
1227+
("1.41", Some(1), 1),
1228+
("1.41", Some(2), 2),
1229+
("1.41", Some(3), 3),
1230+
// v3 introduced
1231+
("1.47", None, 3),
1232+
// last version of v2 as the default
1233+
("1.48", None, 3),
1234+
// v3 is the default
1235+
("1.53", None, 3),
1236+
("1.53", Some(1), 1),
1237+
("1.53", Some(2), 2),
1238+
("1.53", Some(3), 3),
1239+
];
1240+
1241+
for (msrv, existing_lockfile, expected_version) in cases {
1242+
// Clean previous lockfile.
1243+
_ = std::fs::remove_file(p.root().join("Cargo.lock"));
1244+
1245+
p.change_file(
1246+
"Cargo.toml",
1247+
&format!(
1248+
r#"
1249+
[package]
1250+
name = "foo"
1251+
version = "0.0.1"
1252+
rust-version = "{msrv}"
1253+
1254+
[dependencies]
1255+
bar = "0.1.0"
1256+
"#,
1257+
),
1258+
);
1259+
1260+
if let Some(existing_lockfile) = existing_lockfile {
1261+
let existing_lockfile = match existing_lockfile {
1262+
1 => v1_lockfile.as_str().into(),
1263+
2 => v2_lockfile.as_str().into(),
1264+
v => std::borrow::Cow::from(format!("version = {v}")),
1265+
};
1266+
p.change_file("Cargo.lock", &existing_lockfile);
1267+
}
1268+
1269+
p.cargo("fetch").run();
1270+
1271+
let lock = p.read_lockfile();
1272+
let toml = lock.parse::<toml::Table>().unwrap();
1273+
// get `version = <n>` from Cargo.lock
1274+
let version_field = toml.get("version").and_then(|v| v.as_integer());
1275+
1276+
let actual_version = if let Some(ver) = version_field {
1277+
ver
1278+
} else if lock.find("\nchecksum = ").is_some() {
1279+
2
1280+
} else {
1281+
1
1282+
};
1283+
1284+
assert_eq!(
1285+
expected_version, actual_version,
1286+
"msrv: {msrv}, existing lockfile: {existing_lockfile:?}"
1287+
);
1288+
}
1289+
}

0 commit comments

Comments
 (0)