1 //! Lockfile integration test
2
3 // TODO(tarcieri): add more example `Cargo.lock` files which cover more scenarios
4
5 use cargo_lock::{metadata, Lockfile, ResolveVersion, Version};
6
7 /// Load our own `Cargo.lock` file for use in tests
load_our_lockfile() -> Lockfile8 fn load_our_lockfile() -> Lockfile {
9 Lockfile::load("Cargo.lock").unwrap()
10 }
11
12 /// Load this crate's own V2 `Cargo.lock` file
13 #[test]
load_our_own_v2_lockfile()14 fn load_our_own_v2_lockfile() {
15 let lockfile = load_our_lockfile();
16 assert_eq!(lockfile.version, ResolveVersion::V2);
17 assert_ne!(lockfile.packages.len(), 0);
18 }
19
20 /// Load example V1 `Cargo.lock` file (from the Cargo project itself)
21 #[test]
load_example_v1_lockfile()22 fn load_example_v1_lockfile() {
23 let lockfile = Lockfile::load("tests/support/Cargo.lock.v1-example").unwrap();
24
25 assert_eq!(lockfile.version, ResolveVersion::V1);
26 assert_eq!(lockfile.packages.len(), 141);
27 assert_eq!(lockfile.metadata.len(), 136);
28
29 let package = &lockfile.packages[0];
30 assert_eq!(package.name.as_ref(), "adler32");
31 assert_eq!(package.version, Version::parse("1.0.4").unwrap());
32
33 let metadata_key: metadata::Key =
34 "checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
35 .parse()
36 .unwrap();
37
38 let metadata_value = &lockfile.metadata[&metadata_key];
39 assert_eq!(
40 metadata_value.as_ref(),
41 "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
42 );
43 }
44
45 /// Load example V2 `Cargo.lock` file (from rustc)
46 #[test]
load_example_v2_lockfile()47 fn load_example_v2_lockfile() {
48 let lockfile = Lockfile::load("tests/support/Cargo.lock.v2-example").unwrap();
49 assert_eq!(lockfile.version, ResolveVersion::V2);
50 assert_eq!(lockfile.packages.len(), 472);
51 assert_eq!(lockfile.metadata.len(), 0);
52 }
53
54 /// Ensure we can reserialize this crate's own `Cargo.lock` file
55 #[test]
serialize_our_own_lockfile()56 fn serialize_our_own_lockfile() {
57 let lockfile = load_our_lockfile();
58 let reserialized = lockfile.to_string();
59 let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
60 assert_eq!(lockfile, lockfile2);
61 }
62
63 /// Ensure we can serialize a V2 lockfile (our own) as a V1 lockfile
64 #[test]
serialize_v2_to_v1()65 fn serialize_v2_to_v1() {
66 let mut lockfile = load_our_lockfile();
67 lockfile.version = ResolveVersion::V1;
68
69 let reserialized = lockfile.to_string();
70 let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
71 assert_eq!(lockfile.packages, lockfile2.packages);
72 }
73
74 /// Ensure we can serialize a V1 lockfile as a V2 lockfile
75 #[test]
serialize_v1_to_v2()76 fn serialize_v1_to_v2() {
77 let mut lockfile = Lockfile::load("tests/support/Cargo.lock.v1-example").unwrap();
78 lockfile.version = ResolveVersion::V2;
79
80 let reserialized = lockfile.to_string();
81 let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
82 assert_eq!(lockfile.packages, lockfile2.packages);
83 }
84
85 /// Dependency tree tests
86 #[cfg(feature = "dependency-tree")]
87 mod tree {
88 use super::Lockfile;
89
90 /// Compute a dependency graph from this crate's own `Cargo.lock`
91 #[test]
compute_from_our_own_lockfile()92 fn compute_from_our_own_lockfile() {
93 let tree = Lockfile::load("Cargo.lock")
94 .unwrap()
95 .dependency_tree()
96 .unwrap();
97
98 assert_ne!(tree.nodes().len(), 0);
99 }
100
101 /// Compute a dependency graph from a non-trivial example V1 `Cargo.lock`
102 /// (i.e. from the Cargo project itself)
103 #[test]
compute_from_v1_example_lockfile()104 fn compute_from_v1_example_lockfile() {
105 let tree = Lockfile::load("tests/support/Cargo.lock.v1-example")
106 .unwrap()
107 .dependency_tree()
108 .unwrap();
109
110 assert_eq!(tree.nodes().len(), 141);
111 }
112
113 /// Compute a dependency graph from a non-trivial example V2 `Cargo.lock`
114 /// (i.e. from rustc)
115 #[test]
compute_from_v2_example_lockfile()116 fn compute_from_v2_example_lockfile() {
117 let tree = Lockfile::load("tests/support/Cargo.lock.v2-example")
118 .unwrap()
119 .dependency_tree()
120 .unwrap();
121
122 assert_eq!(tree.nodes().len(), 472);
123 }
124 }
125