-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdetails.rs
631 lines (562 loc) · 20 KB
/
details.rs
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::{
dep_helpers::{
assert_all_links, assert_deps_internal, assert_topo_ids, assert_topo_metadatas,
assert_transitive_deps_internal,
},
package_id,
};
use ahash::AHashMap;
use camino::Utf8PathBuf;
use guppy::{
DependencyKind, PackageId, Version,
errors::FeatureGraphWarning,
graph::{
BuildTargetId, BuildTargetKind, DependencyDirection, EnabledStatus, PackageGraph,
PackageLink, PackageMetadata, PackageSource, Workspace,
},
platform::{EnabledTernary, Platform, PlatformSpec},
};
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;
/// This captures metadata fields that are relevant for tests. They are meant to be written out
/// lazily as tests are filled out -- feel free to add more details as necessary!
pub struct FixtureDetails {
workspace_members: Option<BTreeMap<Utf8PathBuf, PackageId>>,
package_details: AHashMap<PackageId, PackageDetails>,
link_details: AHashMap<(PackageId, PackageId), LinkDetails>,
feature_graph_warnings: Vec<FeatureGraphWarning>,
cycles: Vec<Vec<PackageId>>,
}
impl FixtureDetails {
pub fn new(package_details: AHashMap<PackageId, PackageDetails>) -> Self {
Self {
workspace_members: None,
package_details,
link_details: AHashMap::new(),
feature_graph_warnings: vec![],
cycles: vec![],
}
}
pub fn with_workspace_members<'a>(
mut self,
workspace_members: impl IntoIterator<Item = (impl Into<Utf8PathBuf>, &'a str)>,
) -> Self {
self.workspace_members = Some(
workspace_members
.into_iter()
.map(|(path, id)| (path.into(), package_id(id)))
.collect(),
);
self
}
pub fn with_link_details(
mut self,
link_details: AHashMap<(PackageId, PackageId), LinkDetails>,
) -> Self {
self.link_details = link_details;
self
}
pub fn with_feature_graph_warnings(mut self, mut warnings: Vec<FeatureGraphWarning>) -> Self {
warnings.sort();
self.feature_graph_warnings = warnings;
self
}
pub fn with_cycles(mut self, cycles: Vec<Vec<&'static str>>) -> Self {
let cycles: Vec<_> = cycles
.into_iter()
.map(|cycle| cycle.into_iter().map(package_id).collect())
.collect();
// Don't sort because the order returned by all_cycles (both the outer and inner vecs) is
// significant.
self.cycles = cycles;
self
}
pub fn known_ids(&self) -> impl Iterator<Item = &PackageId> {
self.package_details.keys()
}
pub fn assert_workspace(&self, workspace: Workspace) {
if let Some(expected_members) = &self.workspace_members {
let members: Vec<_> = workspace
.iter_by_path()
.map(|(path, metadata)| (path, metadata.id()))
.collect();
assert_eq!(
expected_members
.iter()
.map(|(path, id)| (path.as_path(), id))
.collect::<Vec<_>>(),
members,
"workspace members should be correct"
);
assert_eq!(
workspace.iter_by_path().len(),
workspace.iter_by_name().len(),
"workspace.members() and members_by_name() return the same number of items"
);
for (name, metadata) in workspace.iter_by_name() {
assert_eq!(
name,
metadata.name(),
"members_by_name returns consistent results"
);
}
}
}
pub fn assert_topo(&self, graph: &PackageGraph) {
assert_topo_ids(graph, DependencyDirection::Forward, "topo sort");
assert_topo_ids(graph, DependencyDirection::Reverse, "reverse topo sort");
assert_topo_metadatas(graph, DependencyDirection::Forward, "topo sort (metadatas)");
assert_topo_metadatas(
graph,
DependencyDirection::Reverse,
"reverse topo sort (metadatas)",
);
assert_all_links(graph, DependencyDirection::Forward, "all links");
assert_all_links(graph, DependencyDirection::Reverse, "all links reversed");
}
pub fn assert_metadata(&self, id: &PackageId, metadata: PackageMetadata<'_>, msg: &str) {
let details = &self.package_details[id];
details.assert_metadata(metadata, msg);
}
// ---
// Build targets
// ---
pub fn has_build_targets(&self, id: &PackageId) -> bool {
let details = &self.package_details[id];
details.build_targets.is_some()
}
pub fn assert_build_targets(&self, metadata: PackageMetadata<'_>, msg: &str) {
let build_targets = self.package_details[metadata.id()]
.build_targets
.as_ref()
.unwrap();
let mut actual: Vec<_> = metadata
.build_targets()
.map(|build_target| {
// Strip off the manifest path from the beginning.
let path = build_target
.path()
.strip_prefix(
metadata
.manifest_path()
.parent()
.expect("manifest path is a file"),
)
.expect("build target path is inside source dir")
.to_path_buf();
(build_target.id(), build_target.kind().clone(), path)
})
.collect();
actual.sort();
assert_eq!(build_targets, &actual, "{}: build targets match", msg,);
}
// ---
// Direct dependencies
// ---
/// Returns true if the deps for this package are available to test against.
pub fn has_deps(&self, id: &PackageId) -> bool {
let details = &self.package_details[id];
details.deps.is_some()
}
pub fn assert_deps(&self, graph: &PackageGraph, id: &PackageId, msg: &str) {
let details = &self.package_details[id];
assert_deps_internal(graph, DependencyDirection::Forward, details, msg);
}
/// Returns true if the reverse deps for this package are available to test against.
pub fn has_reverse_deps(&self, id: &PackageId) -> bool {
let details = &self.package_details[id];
details.reverse_deps.is_some()
}
pub fn assert_reverse_deps(&self, graph: &PackageGraph, id: &PackageId, msg: &str) {
let details = &self.package_details[id];
assert_deps_internal(graph, DependencyDirection::Reverse, details, msg);
}
// ---
// Transitive dependencies
// ---
/// Returns true if the transitive deps for this package are available to test against.
pub fn has_transitive_deps(&self, id: &PackageId) -> bool {
let details = &self.package_details[id];
details.transitive_deps.is_some()
}
pub fn assert_transitive_deps(&self, graph: &PackageGraph, id: &PackageId, msg: &str) {
assert_transitive_deps_internal(
graph,
DependencyDirection::Forward,
&self.package_details[id],
msg,
)
}
/// Returns true if the transitive reverse deps for this package are available to test against.
pub fn has_transitive_reverse_deps(&self, id: &PackageId) -> bool {
let details = &self.package_details[id];
details.transitive_reverse_deps.is_some()
}
pub fn assert_transitive_reverse_deps(&self, graph: &PackageGraph, id: &PackageId, msg: &str) {
assert_transitive_deps_internal(
graph,
DependencyDirection::Reverse,
&self.package_details[id],
msg,
)
}
// ---
// Links
// ---
pub fn assert_link_details(&self, graph: &PackageGraph, msg: &str) {
for ((from, to), details) in &self.link_details {
let metadata = graph
.metadata(from)
.unwrap_or_else(|err| panic!("{}: {}", msg, err));
let mut links: Vec<_> = metadata
.direct_links()
.filter(|link| link.to().id() == to)
.collect();
assert_eq!(
links.len(),
1,
"{}: exactly 1 link between '{}' and '{}'",
msg,
from,
to
);
let link = links.pop().unwrap();
let msg = format!("{}: {} -> {}", msg, from, to);
details.assert_metadata(link, &msg);
}
}
// ---
// Features
// ---
pub fn has_named_features(&self, id: &PackageId) -> bool {
self.package_details[id].named_features.is_some()
}
pub fn assert_named_features(&self, graph: &PackageGraph, id: &PackageId, msg: &str) {
let mut actual: Vec<_> = graph
.metadata(id)
.expect("package id should be valid")
.named_features()
.collect();
actual.sort_unstable();
let expected = self.package_details[id].named_features.as_ref().unwrap();
assert_eq!(expected, &actual, "{}", msg);
}
pub fn assert_feature_graph_warnings(&self, graph: &PackageGraph, msg: &str) {
let mut actual: Vec<_> = graph.feature_graph().build_warnings().to_vec();
actual.sort();
assert_eq!(&self.feature_graph_warnings, &actual, "{}", msg);
}
// ---
// Cycles
// ---
pub fn assert_cycles(&self, graph: &PackageGraph, msg: &str) {
let actual: Vec<_> = graph.cycles().all_cycles().collect();
// Don't sort because the order returned by all_cycles (both the outer and inner vecs) is
// significant.
assert_eq!(&self.cycles, &actual, "{}", msg);
let mut cache = graph.new_depends_cache();
for cycle in actual {
for &id1 in &cycle {
for &id2 in &cycle {
assert!(
graph.depends_on(id1, id2).expect("valid package IDs"),
"{}: within cycle, {} depends on {}",
msg,
id1,
id2
);
assert!(
cache.depends_on(id1, id2).expect("valid package IDs"),
"{}: within cycle, {} depends on {} (using cache)",
msg,
id1,
id2
)
}
}
}
// Just ensure that this doesn't crash for now -- we should add more checks later.
let _: Vec<_> = graph.feature_graph().cycles().all_cycles().collect();
}
}
pub struct PackageDetails {
id: PackageId,
name: &'static str,
version: Version,
authors: Vec<&'static str>,
description: Option<&'static str>,
license: Option<&'static str>,
source: Option<PackageSource<'static>>,
build_targets: Option<
Vec<(
BuildTargetId<'static>,
BuildTargetKind<'static>,
Utf8PathBuf,
)>,
>,
// The vector items are (name, package id).
// XXX add more details about dependency edges here?
deps: Option<Vec<(&'static str, PackageId)>>,
reverse_deps: Option<Vec<(&'static str, PackageId)>>,
transitive_deps: Option<Vec<PackageId>>,
transitive_reverse_deps: Option<Vec<PackageId>>,
named_features: Option<Vec<&'static str>>,
}
impl PackageDetails {
pub fn new(
id: &'static str,
name: &'static str,
version: &'static str,
authors: Vec<&'static str>,
description: Option<&'static str>,
license: Option<&'static str>,
) -> Self {
Self {
id: package_id(id),
name,
version: Version::parse(version).expect("version should be valid"),
authors,
description,
license,
source: None,
build_targets: None,
deps: None,
reverse_deps: None,
transitive_deps: None,
transitive_reverse_deps: None,
named_features: None,
}
}
pub fn with_workspace_path(mut self, path: &'static str) -> Self {
self.source = Some(PackageSource::Workspace(path.into()));
self
}
pub fn with_local_path(mut self, path: &'static str) -> Self {
self.source = Some(PackageSource::Path(path.into()));
self
}
pub fn with_crates_io(self) -> Self {
self.with_external_source(PackageSource::CRATES_IO_REGISTRY)
}
pub fn with_external_source(mut self, source: &'static str) -> Self {
self.source = Some(PackageSource::External(source));
self
}
pub fn with_build_targets(
mut self,
mut build_targets: Vec<(
BuildTargetId<'static>,
BuildTargetKind<'static>,
&'static str,
)>,
) -> Self {
build_targets.sort();
self.build_targets = Some(
build_targets
.into_iter()
.map(|(id, kind, path)| (id, kind, path.to_string().into()))
.collect(),
);
self
}
pub fn with_deps(mut self, mut deps: Vec<(&'static str, &'static str)>) -> Self {
deps.sort_unstable();
self.deps = Some(
deps.into_iter()
.map(|(name, id)| (name, package_id(id)))
.collect(),
);
self
}
pub fn with_reverse_deps(
mut self,
mut reverse_deps: Vec<(&'static str, &'static str)>,
) -> Self {
reverse_deps.sort_unstable();
self.reverse_deps = Some(
reverse_deps
.into_iter()
.map(|(name, id)| (name, package_id(id)))
.collect(),
);
self
}
pub fn with_transitive_deps(mut self, mut transitive_deps: Vec<&'static str>) -> Self {
transitive_deps.sort_unstable();
self.transitive_deps = Some(transitive_deps.into_iter().map(package_id).collect());
self
}
pub fn with_transitive_reverse_deps(
mut self,
mut transitive_reverse_deps: Vec<&'static str>,
) -> Self {
transitive_reverse_deps.sort_unstable();
self.transitive_reverse_deps = Some(
transitive_reverse_deps
.into_iter()
.map(package_id)
.collect(),
);
self
}
pub fn with_named_features(mut self, mut named_features: Vec<&'static str>) -> Self {
named_features.sort_unstable();
self.named_features = Some(named_features);
self
}
pub fn insert_into(self, map: &mut AHashMap<PackageId, PackageDetails>) {
map.insert(self.id.clone(), self);
}
pub fn id(&self) -> &PackageId {
&self.id
}
pub fn deps(&self, direction: DependencyDirection) -> Option<&[(&'static str, PackageId)]> {
match direction {
DependencyDirection::Forward => self.deps.as_deref(),
DependencyDirection::Reverse => self.reverse_deps.as_deref(),
}
}
pub fn transitive_deps(&self, direction: DependencyDirection) -> Option<&[PackageId]> {
match direction {
DependencyDirection::Forward => self.transitive_deps.as_deref(),
DependencyDirection::Reverse => self.transitive_reverse_deps.as_deref(),
}
}
pub fn assert_metadata(&self, metadata: PackageMetadata<'_>, msg: &str) {
assert_eq!(&self.id, metadata.id(), "{}: same package ID", msg);
assert_eq!(self.name, metadata.name(), "{}: same name", msg);
assert_eq!(&self.version, metadata.version(), "{}: same version", msg);
assert_eq!(
&self.authors,
&metadata
.authors()
.iter()
.map(|author| author.as_str())
.collect::<Vec<_>>(),
"{}: same authors",
msg
);
assert_eq!(
&self.description,
&metadata.description(),
"{}: same description",
msg
);
assert_eq!(&self.license, &metadata.license(), "{}: same license", msg);
if let Some(source) = &self.source {
assert_eq!(source, &metadata.source(), "{}: same source", msg);
}
}
}
#[derive(Clone, Debug)]
pub struct LinkDetails {
from: PackageId,
to: PackageId,
platform_results: Vec<(DependencyKind, Platform, PlatformResults)>,
features: Vec<(DependencyKind, Vec<&'static str>)>,
}
impl LinkDetails {
pub fn new(from: PackageId, to: PackageId) -> Self {
Self {
from,
to,
platform_results: vec![],
features: vec![],
}
}
pub fn with_platform_status(
mut self,
dep_kind: DependencyKind,
platform: Platform,
status: PlatformResults,
) -> Self {
self.platform_results.push((dep_kind, platform, status));
self
}
pub fn with_features(
mut self,
dep_kind: DependencyKind,
mut features: Vec<&'static str>,
) -> Self {
features.sort_unstable();
self.features.push((dep_kind, features));
self
}
pub fn insert_into(self, map: &mut AHashMap<(PackageId, PackageId), Self>) {
map.insert((self.from.clone(), self.to.clone()), self);
}
pub fn assert_metadata(&self, link: PackageLink<'_>, msg: &str) {
let required_enabled = |status: EnabledStatus<'_>, platform_spec: &PlatformSpec| {
(
status.required_on(platform_spec),
status.enabled_on(platform_spec),
)
};
for (dep_kind, platform, results) in &self.platform_results {
let platform_spec = platform.clone().into();
let req = link.req_for_kind(*dep_kind);
assert_eq!(
required_enabled(req.status(), &platform_spec),
results.status,
"{}: for platform '{}', kind {}, status is correct",
msg,
platform.triple_str(),
dep_kind,
);
assert_eq!(
required_enabled(req.default_features(), &platform_spec),
results.default_features,
"{}: for platform '{}', kind {}, default features is correct",
msg,
platform.triple_str(),
dep_kind,
);
for (feature, status) in &results.feature_statuses {
assert_eq!(
required_enabled(req.feature_status(feature), &platform_spec),
*status,
"{}: for platform '{}', kind {}, feature '{}' has correct status",
msg,
platform.triple_str(),
dep_kind,
feature
);
}
}
for (dep_kind, features) in &self.features {
let metadata = link.req_for_kind(*dep_kind);
let mut actual_features: Vec<_> = metadata.features().collect();
actual_features.sort_unstable();
assert_eq!(&actual_features, features, "{}: features is correct", msg);
}
}
}
#[derive(Clone, Debug)]
pub struct PlatformResults {
// Each pair stands for (required on, enabled on).
status: (EnabledTernary, EnabledTernary),
default_features: (EnabledTernary, EnabledTernary),
feature_statuses: AHashMap<String, (EnabledTernary, EnabledTernary)>,
}
impl PlatformResults {
pub fn new(
status: (EnabledTernary, EnabledTernary),
default_features: (EnabledTernary, EnabledTernary),
) -> Self {
Self {
status,
default_features,
feature_statuses: AHashMap::new(),
}
}
pub fn with_feature_status(
mut self,
feature: &str,
status: (EnabledTernary, EnabledTernary),
) -> Self {
self.feature_statuses.insert(feature.to_string(), status);
self
}
}