Skip to content

Commit 2aa4b18

Browse files
committed
test: demonstrate old lockfile compat matrix
1 parent 4d80f10 commit 2aa4b18

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

0 commit comments

Comments
 (0)