diff --git a/scarb-metadata/CHANGELOG.md b/scarb-metadata/CHANGELOG.md index ee495de6a..67e67b285 100644 --- a/scarb-metadata/CHANGELOG.md +++ b/scarb-metadata/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. ## Unreleased +- Added `profile`, `dev` and `release` arguments to `MetadataCommand`. ## 1.11.0 (2024-01-31) - Added `experimental_features` field to `PackageMetadata`. diff --git a/scarb-metadata/src/command/metadata_command.rs b/scarb-metadata/src/command/metadata_command.rs index a4ea7bca5..89df4515b 100644 --- a/scarb-metadata/src/command/metadata_command.rs +++ b/scarb-metadata/src/command/metadata_command.rs @@ -93,6 +93,21 @@ impl MetadataCommand { self } + /// Defines profile to use for `scarb metadata` command. + pub fn profile(&mut self, profile: impl AsRef) -> &mut Self { + self.env("SCARB_PROFILE", profile) + } + + /// Defines profile to use for `scarb metadata` command as "dev". + pub fn dev(&mut self) -> &mut Self { + self.profile("dev") + } + + /// Defines profile to use for `scarb metadata` command as "release". + pub fn release(&mut self) -> &mut Self { + self.profile("release") + } + /// Inserts or updates an environment variable mapping. pub fn env(&mut self, key: impl AsRef, val: impl AsRef) -> &mut Self { self.inner.env(key, val); @@ -268,8 +283,12 @@ fn parse_stream(stdout: String) -> Result { #[cfg(test)] mod tests { use semver::Version; + use std::ffi::OsStr; - use crate::{CairoVersionInfo, Metadata, MetadataCommandError, VersionInfo, WorkspaceMetadata}; + use crate::{ + CairoVersionInfo, Metadata, MetadataCommand, MetadataCommandError, VersionInfo, + WorkspaceMetadata, + }; macro_rules! check_parse_stream { ($input:expr, $expected:pat) => {{ @@ -406,4 +425,28 @@ mod tests { extra: Default::default(), } } + + #[test] + fn can_define_profile() { + let mut cmd = MetadataCommand::new(); + cmd.profile("test"); + assert_profile(cmd, "test"); + + let mut cmd = MetadataCommand::new(); + cmd.dev(); + assert_profile(cmd, "dev"); + + let mut cmd = MetadataCommand::new(); + cmd.profile("test"); + cmd.release(); + assert_profile(cmd, "release"); + } + + fn assert_profile(cmd: MetadataCommand, profile: impl AsRef) { + let cmd = cmd.scarb_command(); + let (_key, Some(val)) = cmd.get_envs().find(|(k, _)| k == &"SCARB_PROFILE").unwrap() else { + panic!("profile not defined") + }; + assert_eq!(val, profile.as_ref()); + } }