| 1 | //! Cargo flag for selecting the relevant crate. |
| 2 | |
| 3 | use std::path; |
| 4 | |
| 5 | /// Cargo flag for selecting the relevant crate. |
| 6 | #[derive(Default, Clone, Debug, PartialEq, Eq)] |
| 7 | #[cfg_attr(feature = "clap", derive(clap::Args))] |
| 8 | #[cfg_attr(feature = "clap", command(about = None, long_about = None))] |
| 9 | #[non_exhaustive] |
| 10 | pub struct Manifest { |
| 11 | #[cfg_attr(feature = "clap", arg(long, name = "PATH"))] |
| 12 | /// Path to Cargo.toml |
| 13 | pub manifest_path: Option<path::PathBuf>, |
| 14 | } |
| 15 | |
| 16 | #[cfg(feature = "cargo_metadata")] |
| 17 | impl Manifest { |
| 18 | /// Create a `cargo_metadata::MetadataCommand` |
| 19 | /// |
| 20 | /// Note: Requires the features `cargo_metadata`. |
| 21 | pub fn metadata(&self) -> cargo_metadata::MetadataCommand { |
| 22 | let mut c = cargo_metadata::MetadataCommand::new(); |
| 23 | if let Some(ref manifest_path) = self.manifest_path { |
| 24 | c.manifest_path(manifest_path); |
| 25 | } |
| 26 | c |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[cfg(test)] |
| 31 | mod test { |
| 32 | use super::*; |
| 33 | |
| 34 | #[test] |
| 35 | #[cfg(feature = "clap")] |
| 36 | fn verify_app() { |
| 37 | #[derive(Debug, clap::Parser)] |
| 38 | struct Cli { |
| 39 | #[command(flatten)] |
| 40 | manifest: Manifest, |
| 41 | } |
| 42 | |
| 43 | use clap::CommandFactory; |
| 44 | Cli::command().debug_assert(); |
| 45 | } |
| 46 | |
| 47 | #[cfg(feature = "cargo_metadata")] |
| 48 | #[test] |
| 49 | fn metadata_with_path() { |
| 50 | let manifest = Manifest { |
| 51 | manifest_path: Some(path::PathBuf::from("tests/fixtures/simple/Cargo.toml")), |
| 52 | }; |
| 53 | let metadata = manifest.metadata(); |
| 54 | metadata.exec().unwrap(); |
| 55 | // TODO verify we forwarded correctly. |
| 56 | } |
| 57 | |
| 58 | #[cfg(feature = "cargo_metadata")] |
| 59 | #[test] |
| 60 | fn metadata_without_path() { |
| 61 | let cwd = path::PathBuf::from("tests/fixtures/simple"); |
| 62 | let manifest = Manifest { |
| 63 | manifest_path: None, |
| 64 | }; |
| 65 | let mut metadata = manifest.metadata(); |
| 66 | metadata.current_dir(cwd).exec().unwrap(); |
| 67 | // TODO verify we forwarded correctly. |
| 68 | } |
| 69 | } |