Skip to content

Commit

Permalink
Add profile arguments to MetadataCommand (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr authored Feb 1, 2024
1 parent b20ccd3 commit 0437160
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions scarb-metadata/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
45 changes: 44 additions & 1 deletion scarb-metadata/src/command/metadata_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ impl MetadataCommand {
self
}

/// Defines profile to use for `scarb metadata` command.
pub fn profile(&mut self, profile: impl AsRef<OsStr>) -> &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<OsStr>, val: impl AsRef<OsStr>) -> &mut Self {
self.inner.env(key, val);
Expand Down Expand Up @@ -268,8 +283,12 @@ fn parse_stream(stdout: String) -> Result<ParseResult, MetadataCommandError> {
#[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) => {{
Expand Down Expand Up @@ -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<OsStr>) {
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());
}
}

0 comments on commit 0437160

Please sign in to comment.