Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workspace_default_members field #248

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ pub struct Metadata {
pub packages: Vec<Package>,
/// A list of all workspace members
pub workspace_members: Vec<PackageId>,
/// A list of all workspace members
///
/// This is always `None` if running with a version of Cargo older than 1.71.
pub workspace_default_members: Option<Vec<PackageId>>,
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
/// Dependencies graph
pub resolve: Option<Resolve>,
/// Workspace root
Expand Down Expand Up @@ -190,6 +194,20 @@ impl Metadata {
.filter(|&p| self.workspace_members.contains(&p.id))
.collect()
}

/// Get the workspace default packages.
///
/// This will always return `None` if running with a version of Cargo older than 1.71.
pub fn workspace_default_packages(&self) -> Option<Vec<&Package>> {
self.workspace_default_members
.as_ref()
.map(|workspace_default_members| {
self.packages
.iter()
.filter(|&p| workspace_default_members.contains(&p.id))
.collect()
})
}
}

impl<'a> std::ops::Index<&'a PackageId> for Metadata {
Expand Down
3 changes: 3 additions & 0 deletions tests/selftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ fn metadata_deps() {
let workspace_packages = metadata.workspace_packages();
assert_eq!(workspace_packages.len(), 1);
assert_eq!(&workspace_packages[0].id, this_id);
if let Some(default_packages) = metadata.workspace_default_packages() {
assert_eq!(default_packages, workspace_packages);
}

let lib = this
.targets
Expand Down
6 changes: 6 additions & 0 deletions tests/test_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn all_the_fields() {
// path added in 1.51
// default_run added in 1.55
// rust_version added in 1.58
// workspace_default_members added in 1.71
eprintln!("Skipping all_the_fields test, cargo {} is too old.", ver);
return;
}
Expand All @@ -196,6 +197,11 @@ fn all_the_fields() {
);
assert_eq!(meta.workspace_members.len(), 1);
assert!(meta.workspace_members[0].to_string().starts_with("all"));
if ver >= semver::Version::parse("1.71.0").unwrap() {
assert_eq!(meta.workspace_default_members, Some(meta.workspace_members));
} else {
assert_eq!(meta.workspace_default_members, None);
}

assert_eq!(meta.packages.len(), 9);
let all = meta.packages.iter().find(|p| p.name == "all").unwrap();
Expand Down