Skip to content

Commit 92b82d6

Browse files
committed
test: demonstrate old lockfile compat matrix
1 parent 7b09193 commit 92b82d6

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

0 commit comments

Comments
 (0)