File: lockfile.rs

package info (click to toggle)
rust-cargo-lock 11.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: makefile: 10
file content (316 lines) | stat: -rw-r--r-- 9,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Lockfile integration test

use std::fs;
use std::str::FromStr;

// TODO(tarcieri): add more example `Cargo.lock` files which cover more scenarios

use cargo_lock::{
    Lockfile, MetadataKey, ResolveVersion, Version,
    package::{GitReference, SourceKind},
};

/// Path to a V1 `Cargo.lock` file.
const V1_LOCKFILE_PATH: &str = "tests/examples/Cargo.lock.v1";

/// Path to a V2 `Cargo.lock` file.
const V2_LOCKFILE_PATH: &str = "tests/examples/Cargo.lock.v2";

/// Path to a V3 `Cargo.lock` file.
const V3_LOCKFILE_PATH: &str = "tests/examples/Cargo.lock.v3";

/// Path to a V4 `Cargo.lock` file.
const V4_LOCKFILE_PATH: &str = "tests/examples/Cargo.lock.v4";

/// Load example V1 `Cargo.lock` file (from the Cargo project itself)
#[test]
fn load_example_v1_lockfile() {
    let lockfile = Lockfile::load(V1_LOCKFILE_PATH).unwrap();

    assert_eq!(lockfile.version, ResolveVersion::V1);
    assert_eq!(lockfile.packages.len(), 141);
    assert_eq!(lockfile.metadata.len(), 136);

    let package = &lockfile.packages[0];
    assert_eq!(package.name.as_ref(), "adler32");
    assert_eq!(package.version, Version::parse("1.0.4").unwrap());

    let metadata_key: MetadataKey =
        "checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
            .parse()
            .unwrap();

    let metadata_value = &lockfile.metadata[&metadata_key];
    assert_eq!(
        metadata_value.as_ref(),
        "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
    );
}

/// Load example V2 `Cargo.lock` file
#[test]
fn load_example_v2_lockfile() {
    let lockfile = Lockfile::load(V2_LOCKFILE_PATH).unwrap();
    assert_eq!(lockfile.version, ResolveVersion::V2);
    assert_eq!(lockfile.packages.len(), 25);
    assert_eq!(lockfile.metadata.len(), 0);
}

/// Load example V3 `Cargo.lock` file
#[test]
fn load_example_v3_lockfile() {
    let lockfile = Lockfile::load(V3_LOCKFILE_PATH).unwrap();
    assert_eq!(lockfile.version, ResolveVersion::V3);
    assert_eq!(lockfile.packages.len(), 25);
    assert_eq!(lockfile.metadata.len(), 0);
}

/// Ensure V3 lockfiles encode their version correctly.
#[test]
fn serialize_v3() {
    let lockfile = Lockfile::load(V3_LOCKFILE_PATH).unwrap();
    let reserialized = lockfile.to_string();
    let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
    assert_eq!(lockfile2.version, ResolveVersion::V3);
    assert_eq!(lockfile2.packages, lockfile.packages);
}

/// Load example V4 `Cargo.lock` file
#[test]
fn load_example_v4_lockfile() {
    let lockfile = Lockfile::load(V4_LOCKFILE_PATH).unwrap();
    assert_eq!(lockfile.version, ResolveVersion::V4);
    assert_eq!(lockfile.packages.len(), 25);
    assert_eq!(lockfile.metadata.len(), 0);

    let source_kind = lockfile
        .packages
        .iter()
        .find(|pkg| pkg.name.as_str() == "url")
        .and_then(|pkg| pkg.source.as_ref())
        .map(|id| id.kind())
        .unwrap();
    assert_eq!(
        source_kind,
        &SourceKind::Git(GitReference::Tag("a-_+#$)z".into()))
    );

    let source_kind = lockfile
        .packages
        .iter()
        .find(|pkg| pkg.name.as_str() == "toml")
        .and_then(|pkg| pkg.source.as_ref())
        .map(|id| id.kind())
        .unwrap();
    assert_eq!(
        source_kind,
        &SourceKind::Git(GitReference::Branch("a-_+#$)z".into()))
    );
}

/// Ensure V4 lockfiles encode their version correctly.
#[test]
fn serialize_v4() {
    let lockfile = Lockfile::load(V4_LOCKFILE_PATH).unwrap();
    let reserialized = lockfile.to_string();
    let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
    assert_eq!(lockfile2.version, ResolveVersion::V4);
    assert_eq!(lockfile2.packages, lockfile.packages);
}

/// Ensure we can serialize a V2 lockfile as a V1 lockfile
#[test]
fn serialize_v2_to_v1() {
    let mut lockfile = Lockfile::load(V2_LOCKFILE_PATH).unwrap();
    lockfile.version = ResolveVersion::V1;

    let reserialized = lockfile.to_string();
    let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
    assert_eq!(lockfile2.version, ResolveVersion::V1);
    assert_eq!(lockfile2.packages, lockfile.packages);
}

/// Ensure we can serialize a V1 lockfile as a V2 lockfile
#[test]
fn serialize_v1_to_v2() {
    let mut lockfile = Lockfile::load(V1_LOCKFILE_PATH).unwrap();
    lockfile.version = ResolveVersion::V2;

    let reserialized = lockfile.to_string();
    let lockfile2 = reserialized.parse::<Lockfile>().unwrap();
    assert_eq!(lockfile.packages, lockfile2.packages);
}

/// Test that encoded V1 lockfiles match what Cargo would normally write.
#[test]
fn serde_matches_v1() {
    let lockfile = Lockfile::load(V1_LOCKFILE_PATH).unwrap();
    let reserialized = lockfile.to_string();
    let file_content = fs::read_to_string(V1_LOCKFILE_PATH).unwrap();

    assert_eq!(reserialized, file_content);
}

/// Test that encoded V2 lockfiles match what Cargo would normally write.
#[test]
fn serde_matches_v2() {
    let lockfile = Lockfile::load(V2_LOCKFILE_PATH).unwrap();
    let reserialized = lockfile.to_string();
    let file_content = fs::read_to_string(V2_LOCKFILE_PATH).unwrap();

    assert_eq!(reserialized, file_content);
}

/// Test that encoded V3 lockfiles match what Cargo would normally write.
#[test]
fn serde_matches_v3() {
    let lockfile = Lockfile::load(V3_LOCKFILE_PATH).unwrap();
    let reserialized = lockfile.to_string();
    let file_content = fs::read_to_string(V3_LOCKFILE_PATH).unwrap();

    assert_eq!(reserialized, file_content);
}

/// Dependency tree tests
#[cfg(feature = "dependency-tree")]
mod tree {
    use super::{Lockfile, V1_LOCKFILE_PATH, V2_LOCKFILE_PATH};

    /// Compute a dependency graph from a non-trivial example V1 `Cargo.lock`
    #[test]
    fn compute_from_v1_example_lockfile() {
        let tree = Lockfile::load(V1_LOCKFILE_PATH)
            .unwrap()
            .dependency_tree()
            .unwrap();

        assert_eq!(tree.nodes().len(), 141);
    }

    /// Compute a dependency graph from a non-trivial example V2 `Cargo.lock`
    #[test]
    fn compute_from_v2_example_lockfile() {
        let tree = Lockfile::load(V2_LOCKFILE_PATH)
            .unwrap()
            .dependency_tree()
            .unwrap();

        assert_eq!(tree.nodes().len(), 25);
    }
}

#[test]
fn source_disambiguation_single_version_different_registries() {
    source_disambiguation("single_version_different_registries");
}

#[test]
fn source_disambiguation_single_version_different_source_types() {
    source_disambiguation("single_version_different_source_types");
}

#[test]
fn source_disambiguation_two_versions_different_registries() {
    source_disambiguation("two_versions_different_registries");
}

#[test]
fn source_disambiguation_two_versions_different_source_types() {
    source_disambiguation("two_versions_different_source_types");
}

#[test]
fn source_disambiguation_two_versions_same_registry() {
    source_disambiguation("two_versions_same_registry");
}

/// Test logic related to the disambiguation of dependency specifications when
/// the lock file contains multiple versions of the same crate and/or multiple
/// sources for the same crate.
fn source_disambiguation(test_file_suffix: &str) {
    let original = fs::read_to_string(&format!(
        "tests/examples/source_disambiguation/Cargo.lock.{}",
        test_file_suffix
    ))
    .unwrap();
    let re_encoded = Lockfile::from_str(&original).unwrap().to_string();

    assert_eq!(original, re_encoded);
}

/// Test that a lockfile with git sources are correctly encoded
#[test]
fn encoding_registry_and_git() {
    let lockfile = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "tower-buffer"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4887dc2a65d464c8b9b66e0e4d51c2fd6cf5b3373afc72805b0a60bce00446a"
dependencies = [
 "tracing 0.1.35",
]

[[package]]
name = "tracing"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160"

[[package]]
name = "tracing"
version = "0.2.0"
source = "git+https://github.com/tokio-rs/tracing.git?rev=1e09e50e8d15580b5929adbade9c782a6833e4a0#1e09e50e8d15580b5929adbade9c782a6833e4a0"

[[package]]
name = "example"
version = "0.1.0"
dependencies = [
 "tower-buffer",
 "tracing 0.2.0",
]
"#;

    assert_eq!(
        lockfile,
        cargo_lock::Lockfile::from_str(lockfile)
            .unwrap()
            .to_string(),
    );
}

#[cfg(feature = "dependency-tree")]
#[test]
fn hash_fragment_dep() {
    let lockfile_str = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "parent"
version = "0.1.0"
source = "git+https://github.com/nope/parent?rev=xxxx#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
dependencies = [
 "child 0.1.0 (git+https://github.com/nope/child?rev=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)",
 "child 0.1.0 (git+https://github.com/nope/child?rev=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)",
]

[[package]]
name = "child"
version = "0.1.0"
source = "git+https://github.com/nope/child?rev=aaaa#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

[[package]]
name = "child"
version = "0.1.0"
source = "git+https://github.com/nope/child?rev=bbbb#bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"#;
    let lockfile = cargo_lock::Lockfile::from_str(lockfile_str).unwrap();
    assert_eq!(lockfile_str, lockfile.to_string(),);
    // This will fail to resolve if child source URLs aren't normalized when deriving
    // dependencies from packges.
    let _tree = cargo_lock::dependency::tree::Tree::new(&lockfile).unwrap();
}