Skip to content

Commit

Permalink
Scarb UI to_env_vars trait (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr authored Apr 15, 2024
1 parent 3a25eb1 commit 2a0e93c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
9 changes: 2 additions & 7 deletions scarb/src/bin/scarb/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ pub fn run(args: TestArgs, config: &Config) -> Result<()> {
.match_many(&ws)?
.iter()
.try_for_each(|package| {
ops::execute_test_subcommand(
package,
&args.args,
&ws,
args.features.clone().try_into()?,
)
.map(|_| ())
ops::execute_test_subcommand(package, &args.args, &ws, args.features.clone())
.map(|_| ())
})
}
31 changes: 5 additions & 26 deletions scarb/src/ops/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use std::{env, iter};

use anyhow::{bail, Result};
use camino::Utf8PathBuf;
use scarb_ui::args::{FeaturesSpec, ToEnvVars};
use tracing::debug;

use scarb_ui::components::Status;

use crate::core::{Config, Package, ScriptDefinition, Workspace};
use crate::internal::fsx::is_executable;
use crate::ops::{self, FeaturesSelector};
use crate::ops::{self, FeaturesOpts};
use crate::process::exec_replace;
use crate::subcommands::{get_env_vars, EXTERNAL_CMD_PREFIX, SCARB_MANIFEST_PATH_ENV};

Expand Down Expand Up @@ -49,13 +50,15 @@ pub fn execute_test_subcommand(
package: &Package,
args: &[OsString],
ws: &Workspace<'_>,
features: ops::FeaturesOpts,
features: FeaturesSpec,
) -> Result<()> {
let package_name = &package.id.name;
let mut env = HashMap::from_iter([(
SCARB_MANIFEST_PATH_ENV.into(),
package.manifest_path().to_string(),
)]);
// Validate features opts.
let _: FeaturesOpts = features.clone().try_into()?;
env.extend(features.to_env_vars());
if let Some(script_definition) = package.manifest.scripts.get("test") {
debug!("using `test` script: {script_definition}");
Expand Down Expand Up @@ -111,27 +114,3 @@ fn find_external_subcommand(cmd: &str, config: &Config) -> Result<Option<PathBuf
.map(|dir| dir.join(&command_exe))
.find(|file| is_executable(file)))
}

pub trait ToEnv {
fn to_env_vars(&self) -> HashMap<String, String>;
}

impl ToEnv for ops::FeaturesOpts {
fn to_env_vars(&self) -> HashMap<String, String> {
let mut env = HashMap::new();
match &self.features {
FeaturesSelector::AllFeatures => {
env.insert("SCARB_ALL_FEATURES".into(), true.to_string());
}
FeaturesSelector::Features(features) if !features.is_empty() => {
env.insert("SCARB_FEATURES".into(), features.join(","));
}
_ => {}
};
env.insert(
"SCARB_NO_DEFAULT_FEATURES".into(),
self.no_default_features.to_string(),
);
env
}
}
19 changes: 19 additions & 0 deletions utils/scarb-ui/src/args/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ pub struct FeaturesSpec {
)]
pub no_default_features: bool,
}

impl super::ToEnvVars for FeaturesSpec {
fn to_env_vars(self) -> Vec<(String, String)> {
let mut env = vec![("SCARB_FEATURES".to_string(), self.features.join(","))];
if self.all_features {
env.push((
"SCARB_ALL_FEATURES".to_string(),
self.all_features.to_string(),
));
}
if self.no_default_features {
env.push((
"SCARB_NO_DEFAULT_FEATURES".to_string(),
self.no_default_features.to_string(),
));
}
env
}
}
8 changes: 8 additions & 0 deletions utils/scarb-ui/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ pub use verbosity::*;
mod features;
mod packages_filter;
mod verbosity;

/// This trait can be used to convert CLI argument into a set of environment variables.
///
/// This is useful when you want to pass CLI arguments and pass them to Scarb called in a child process.
pub trait ToEnvVars {
/// Convert to a set of environment variables.
fn to_env_vars(self) -> Vec<(String, String)>;
}
9 changes: 9 additions & 0 deletions utils/scarb-ui/src/args/packages_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ impl PackagesSource for Metadata {
}
}

impl super::ToEnvVars for PackagesFilter {
fn to_env_vars(self) -> Vec<(String, String)> {
vec![(
"SCARB_PACKAGES_FILTER".into(),
self.to_env().to_string_lossy().to_string(),
)]
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down

0 comments on commit 2a0e93c

Please sign in to comment.