From 3790a625e0d58a06cff396c5e3e30644c9388e7b Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 19 Oct 2023 21:16:46 -0400 Subject: [PATCH] test: demonstrate old lockfile compat matrix --- tests/testsuite/lockfile_compat.rs | 134 +++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/tests/testsuite/lockfile_compat.rs b/tests/testsuite/lockfile_compat.rs index 97dcff1231a7..512bfce96f58 100644 --- a/tests/testsuite/lockfile_compat.rs +++ b/tests/testsuite/lockfile_compat.rs @@ -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::().unwrap(); + // get `version = ` 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:?}" + ); + } +}