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 and friends to cargo-msrv cli #1025

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.76"
msrv = "1.78"
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bisector = "0.4.0" # bisection with a custom comparator
camino = "1.1" # utf-8 paths
cargo_metadata = "0.18.1" # resolving Cargo manifest metadata (consider `guppy`!)
clap = { version = "4.5.20", features = ["derive"] } # parse CLI arguments
clap-cargo = { version = "0.14.1", features = ["cargo_metadata"] }
dirs = "5.0.1" # common directories
indicatif = "0.17.8" # UI
once_cell = "1.20.2" # lazy data structures and thunking
Expand Down
3 changes: 3 additions & 0 deletions src/cli/shared_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct SharedOpts {
#[arg(long, value_name = "Cargo Manifest", global = true, value_hint = ValueHint::FilePath)]
pub manifest_path: Option<PathBuf>,

#[command(flatten)]
pub workspace: clap_cargo::Workspace,

#[command(flatten)]
pub user_output_opts: UserOutputOpts,

Expand Down
39 changes: 33 additions & 6 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ pub struct EnvironmentContext {
///
/// Does not include a manifest file like Cargo.toml, so it's easy to append
/// a file path like `Cargo.toml` or `Cargo.lock`.
pub crate_path: Utf8PathBuf,
pub root_crate_path: Utf8PathBuf,

/// Resolved workspace
pub workspace_packages: WorkspacePackages,
}

impl<'shared_opts> TryFrom<&'shared_opts SharedOpts> for EnvironmentContext {
Expand Down Expand Up @@ -271,33 +274,57 @@ impl<'shared_opts> TryFrom<&'shared_opts SharedOpts> for EnvironmentContext {
})
}?;

let crate_path = path.try_into().map_err(|err| {
let root_crate_path: Utf8PathBuf = path.try_into().map_err(|err| {
CargoMSRVError::Path(PathError::InvalidUtf8(InvalidUtf8Error::from(err)))
})?;

Ok(Self { crate_path })
let metadata = cargo_metadata::MetadataCommand::new()
.manifest_path(root_crate_path.join("Cargo.toml"))
.exec()?;
let workspace = opts.workspace.partition_packages(&metadata);
let workspace_packages = WorkspacePackages::from_iter(workspace.0.into_iter().cloned());

info!(?workspace_packages, workspace_packages_excluded = ?workspace.1);

Ok(Self {
root_crate_path,
workspace_packages,
})
}
}

impl EnvironmentContext {
/// Path to the crate root
pub fn root(&self) -> &Utf8Path {
&self.crate_path
&self.root_crate_path
}

/// The path to the Cargo manifest
pub fn manifest(&self) -> Utf8PathBuf {
self.crate_path.join("Cargo.toml")
self.root_crate_path.join("Cargo.toml")
}

/// The path to the Cargo lock file
pub fn lock(&self) -> Utf8PathBuf {
self.crate_path.join("Cargo.lock")
self.root_crate_path.join("Cargo.lock")
}
}

// ---

#[derive(Clone, Debug)]
pub struct WorkspacePackages {
_selected: Vec<cargo_metadata::Package>,
}

impl WorkspacePackages {
pub fn from_iter(selected: impl IntoIterator<Item = cargo_metadata::Package>) -> Self {
Self {
_selected: selected.into_iter().collect(),
}
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)]
pub enum OutputFormat {
/// Progress bar rendered to stderr
Expand Down
4 changes: 3 additions & 1 deletion src/sub_command/find/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;
use crate::check::TestRunner;
use crate::context::{
CheckCommandContext, EnvironmentContext, ReleaseSource, RustReleasesContext, ToolchainContext,
WorkspacePackages,
};
use crate::manifest::bare_version::BareVersion;
use crate::reporter::TestReporterWrapper;
Expand Down Expand Up @@ -263,7 +264,8 @@ fn create_test_context() -> FindContext {
rustup_command: None,
},
environment: EnvironmentContext {
crate_path: Utf8PathBuf::new(),
root_crate_path: Utf8PathBuf::new(),
workspace_packages: WorkspacePackages::from_iter([]),
},
}
}
11 changes: 7 additions & 4 deletions src/writer/write_msrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn write_msrv(

#[cfg(test)]
mod tests {
use crate::context::{EnvironmentContext, RustReleasesContext};
use crate::context::{EnvironmentContext, RustReleasesContext, WorkspacePackages};
use crate::error::CargoMSRVError;
use crate::manifest::bare_version::BareVersion;
use crate::reporter::FakeTestReporter;
Expand All @@ -51,7 +51,8 @@ mod tests {
let version = BareVersion::ThreeComponents(2, 0, 5);

let env = EnvironmentContext {
crate_path: root.to_path_buf(),
root_crate_path: root.to_path_buf(),
workspace_packages: WorkspacePackages::from_iter([]),
};

let index = ReleaseIndex::from_iter(vec![rust_releases::Release::new_stable(
Expand Down Expand Up @@ -85,7 +86,8 @@ mod tests {
let version = BareVersion::ThreeComponents(2, 0, 5);

let env = EnvironmentContext {
crate_path: root.to_path_buf(),
root_crate_path: root.to_path_buf(),
workspace_packages: WorkspacePackages::from_iter([]),
};

let index = ReleaseIndex::from_iter(vec![]);
Expand Down Expand Up @@ -116,7 +118,8 @@ mod tests {
let version = BareVersion::ThreeComponents(2, 0, 5);

let env = EnvironmentContext {
crate_path: root.to_path_buf(),
root_crate_path: root.to_path_buf(),
workspace_packages: WorkspacePackages::from_iter([]),
};

write_msrv(
Expand Down
Loading