From 231aad90e124d13129f7a368d7eeeb24d319afcb Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 21 Mar 2024 19:54:46 -0400 Subject: [PATCH] Add `#[derive(PartialEq, Eq))` for cli bits So we can unit test with `assert_eq!()`. Signed-off-by: Colin Walters --- lib/src/cli.rs | 27 +++++++++++++++++++-------- lib/src/install.rs | 14 +++++++------- lib/src/install/baseline.rs | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/src/cli.rs b/lib/src/cli.rs index 85c1c7357..1e9366ff9 100644 --- a/lib/src/cli.rs +++ b/lib/src/cli.rs @@ -23,7 +23,7 @@ use crate::spec::ImageReference; use crate::utils::sigpolicy_from_opts; /// Perform an upgrade operation -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] pub(crate) struct UpgradeOpts { /// Don't display progress #[clap(long)] @@ -46,7 +46,7 @@ pub(crate) struct UpgradeOpts { } /// Perform an switch operation -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] pub(crate) struct SwitchOpts { /// Don't display progress #[clap(long)] @@ -87,7 +87,7 @@ pub(crate) struct SwitchOpts { } /// Perform an edit operation -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] pub(crate) struct EditOpts { /// Use filename to edit system specification #[clap(long, short = 'f')] @@ -99,7 +99,7 @@ pub(crate) struct EditOpts { } /// Perform an status operation -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] pub(crate) struct StatusOpts { /// Output in JSON format. #[clap(long)] @@ -112,7 +112,7 @@ pub(crate) struct StatusOpts { /// Options for internal testing #[cfg(feature = "install")] -#[derive(Debug, clap::Subcommand)] +#[derive(Debug, clap::Subcommand, PartialEq, Eq)] pub(crate) enum InstallOpts { /// Install to the target block device ToDisk(crate::install::InstallToDiskOpts), @@ -129,7 +129,7 @@ pub(crate) enum InstallOpts { } /// Options for man page generation -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] pub(crate) struct ManOpts { #[clap(long)] /// Output to this directory @@ -137,7 +137,7 @@ pub(crate) struct ManOpts { } /// Options for internal testing -#[derive(Debug, clap::Subcommand)] +#[derive(Debug, clap::Subcommand, PartialEq, Eq)] pub(crate) enum TestingOpts { /// Execute integration tests that require a privileged container RunPrivilegedIntegration {}, @@ -168,7 +168,7 @@ pub(crate) enum TestingOpts { /// /// Changes in `/etc` and `/var` persist. /// -#[derive(Debug, Parser)] +#[derive(Debug, Parser, PartialEq, Eq)] #[clap(name = "bootc")] #[clap(rename_all = "kebab-case")] #[allow(clippy::large_enum_variant)] @@ -576,3 +576,14 @@ fn test_parse_install_args() { assert!(o.target_opts.target_no_signature_verification); assert_eq!(o.filesystem_opts.root_path.as_str(), "/target"); } + +#[test] +fn test_parse_opts() { + assert!(matches!( + Opt::parse_from(["bootc", "status"]), + Opt::Status(StatusOpts { + json: false, + booted: false + }) + )); +} diff --git a/lib/src/install.rs b/lib/src/install.rs index ea2932b56..47e9ccd28 100644 --- a/lib/src/install.rs +++ b/lib/src/install.rs @@ -62,7 +62,7 @@ pub(crate) const ARCH_USES_EFI: bool = cfg!(any(target_arch = "x86_64", target_a /// Kernel argument used to specify we want the rootfs mounted read-write by default const RW_KARG: &str = "rw"; -#[derive(clap::Args, Debug, Clone, Serialize, Deserialize)] +#[derive(clap::Args, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub(crate) struct InstallTargetOpts { // TODO: A size specifier which allocates free space for the root in *addition* to the base container image size // pub(crate) root_additional_size: Option @@ -110,7 +110,7 @@ pub(crate) struct InstallTargetOpts { pub(crate) skip_fetch_check: bool, } -#[derive(clap::Args, Debug, Clone, Serialize, Deserialize)] +#[derive(clap::Args, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub(crate) struct InstallSourceOpts { /// Install the system from an explicitly given source. /// @@ -122,7 +122,7 @@ pub(crate) struct InstallSourceOpts { pub(crate) source_imgref: Option, } -#[derive(clap::Args, Debug, Clone, Serialize, Deserialize)] +#[derive(clap::Args, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub(crate) struct InstallConfigOpts { /// Disable SELinux in the target (installed) system. /// @@ -159,7 +159,7 @@ pub(crate) struct InstallConfigOpts { } /// Perform an installation to a block device. -#[derive(Debug, Clone, clap::Parser, Serialize, Deserialize)] +#[derive(Debug, Clone, clap::Parser, Serialize, Deserialize, PartialEq, Eq)] pub(crate) struct InstallToDiskOpts { #[clap(flatten)] #[serde(flatten)] @@ -206,7 +206,7 @@ impl std::fmt::Display for ReplaceMode { } /// Options for installing to a filesystem -#[derive(Debug, Clone, clap::Args)] +#[derive(Debug, Clone, clap::Args, PartialEq, Eq)] pub(crate) struct InstallTargetFilesystemOpts { /// Path to the mounted root filesystem. /// @@ -232,7 +232,7 @@ pub(crate) struct InstallTargetFilesystemOpts { } /// Perform an installation to a mounted filesystem. -#[derive(Debug, Clone, clap::Parser)] +#[derive(Debug, Clone, clap::Parser, PartialEq, Eq)] pub(crate) struct InstallToFilesystemOpts { #[clap(flatten)] pub(crate) filesystem_opts: InstallTargetFilesystemOpts, @@ -248,7 +248,7 @@ pub(crate) struct InstallToFilesystemOpts { } /// Perform an installation to the host root filesystem. -#[derive(Debug, Clone, clap::Parser)] +#[derive(Debug, Clone, clap::Parser, PartialEq, Eq)] pub(crate) struct InstallToExistingRootOpts { /// Configure how existing data is treated. #[clap(long, default_value = "alongside")] diff --git a/lib/src/install/baseline.rs b/lib/src/install/baseline.rs index d02efcb16..2e0bd072a 100644 --- a/lib/src/install/baseline.rs +++ b/lib/src/install/baseline.rs @@ -64,7 +64,7 @@ impl Default for BlockSetup { } /// Options for installing to a block device -#[derive(Debug, Clone, clap::Args, Serialize, Deserialize)] +#[derive(Debug, Clone, clap::Args, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] pub(crate) struct InstallBlockDeviceOpts { /// Target block device for installation. The entire device will be wiped.