Skip to content

Commit

Permalink
cargo-lock: fix V3 lockfile handling and tests
Browse files Browse the repository at this point in the history
The `version` field was previously being completely ignored.

This commit first fixes the tests so they run locally again (they were
broken in #533 but due to a quirk of how CI runs, namely nuking the
toplevel workspace Cargo.toml to support a 1.40 MSRV, they continued to
pass in CI).

It removes all previous dependencies on `cargo-lock` having a Cargo.lock
file (kind of silly to use something that changes as a test vector) and
replaces it with explicit V2 and V3 lockfile test vectors, which are now
in a newly renamed `tests/examples` directory.

Finally, it fixes parsing of V3 lockfiles, actually paying attention to
the `version` field if present.
  • Loading branch information
tarcieri committed Apr 23, 2022
1 parent 7ddddc7 commit d566685
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 5,076 deletions.
2 changes: 1 addition & 1 deletion cargo-lock/src/dependency/tree.rs
Expand Up @@ -190,7 +190,7 @@ mod tests {

/// Load this crate's `Cargo.lock`
fn load_lockfile() -> Lockfile {
Lockfile::load("Cargo.lock").unwrap()
Lockfile::load("tests/examples/Cargo.lock.v3").unwrap()
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion cargo-lock/src/lib.rs
Expand Up @@ -21,7 +21,7 @@
//! ```
//! use cargo_lock::Lockfile;
//!
//! let lockfile = Lockfile::load("Cargo.lock").unwrap();
//! let lockfile = Lockfile::load("tests/examples/Cargo.lock").unwrap();
//! println!("number of dependencies: {}", lockfile.packages.len());
//! ```
//!
Expand Down
6 changes: 5 additions & 1 deletion cargo-lock/src/lockfile/encoding.rs
Expand Up @@ -74,7 +74,11 @@ impl TryFrom<EncodableLockfile> for Lockfile {
type Error = Error;

fn try_from(raw_lockfile: EncodableLockfile) -> Result<Lockfile, Error> {
let version = ResolveVersion::detect(&raw_lockfile.package, &raw_lockfile.metadata)?;
let version = match raw_lockfile.version {
Some(n) => n.try_into()?,
None => ResolveVersion::detect(&raw_lockfile.package, &raw_lockfile.metadata)?
};

let mut packages = Vec::with_capacity(raw_lockfile.package.len());

for raw_package in &raw_lockfile.package {
Expand Down
8 changes: 3 additions & 5 deletions cargo-lock/src/lockfile/version.rs
Expand Up @@ -58,16 +58,14 @@ impl ResolveVersion {

/// Should this version be explicitly encoded?
pub(super) fn is_explicit(self) -> bool {
u32::from(self) > 3
u32::from(self) >= 3
}
}

/// V2 format is now the default.
///
///See: <https://github.com/rust-lang/cargo/pull/7579>
/// V3 format is now the default.
impl Default for ResolveVersion {
fn default() -> Self {
ResolveVersion::V2
ResolveVersion::V3
}
}

Expand Down
1 change: 1 addition & 0 deletions cargo-lock/tests/examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
220 changes: 220 additions & 0 deletions cargo-lock/tests/examples/Cargo.lock.v2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d566685

Please sign in to comment.