From 85ae7dc62b6a2a70877019cd39239bdd95b2faf1 Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Thu, 19 Dec 2024 15:28:10 -0500 Subject: [PATCH] chore(env): add VERCEL_ENV into default global env map --- crates/turborepo-env/src/lib.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/turborepo-env/src/lib.rs b/crates/turborepo-env/src/lib.rs index 177955a6fe809..29425f9aa3ef0 100644 --- a/crates/turborepo-env/src/lib.rs +++ b/crates/turborepo-env/src/lib.rs @@ -13,7 +13,7 @@ use thiserror::Error; pub mod platform; -const DEFAULT_ENV_VARS: [&str; 1] = ["VERCEL_ANALYTICS_ID"]; +const DEFAULT_ENV_VARS: &[&str] = ["VERCEL_ANALYTICS_ID", "VERCEL_ENV"].as_slice(); #[derive(Clone, Debug, Error)] pub enum Error { @@ -22,7 +22,7 @@ pub enum Error { } // TODO: Consider using immutable data structures here -#[derive(Clone, Debug, Default, Serialize)] +#[derive(Clone, Debug, Default, Serialize, PartialEq)] #[serde(transparent)] pub struct EnvironmentVariableMap(HashMap); @@ -278,7 +278,7 @@ pub fn get_global_hashable_env_vars( env_at_execution_start: &EnvironmentVariableMap, global_env: &[String], ) -> Result { - let default_env_var_map = env_at_execution_start.from_wildcards(&DEFAULT_ENV_VARS[..])?; + let default_env_var_map = env_at_execution_start.from_wildcards(DEFAULT_ENV_VARS)?; let user_env_var_set = env_at_execution_start.wildcard_map_from_wildcards_unresolved(global_env)?; @@ -335,4 +335,27 @@ mod tests { assert_eq!(actual.get("Turbo"), None); } } + + #[test_case(&[], &["VERCEL_ANALYTICS_ID", "VERCEL_ENV"] ; "defaults")] + #[test_case(&["!VERCEL*"], &[] ; "removing defaults")] + #[test_case(&["FOO*", "!FOOD"], &["FOO", "FOOBAR", "VERCEL_ANALYTICS_ID", "VERCEL_ENV"] ; "intersecting globs")] + fn test_global_env(inputs: &[&str], expected: &[&str]) { + let env_at_start = EnvironmentVariableMap( + vec![ + ("VERCEL_ENV", "prod"), + ("VERCEL_ANALYTICS_ID", "1"), + ("FOO", "bar"), + ("FOOBAR", "baz"), + ("FOOD", "cheese"), + ] + .into_iter() + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect(), + ); + let inputs = inputs.iter().map(|s| s.to_string()).collect::>(); + let actual = get_global_hashable_env_vars(&env_at_start, &inputs).unwrap(); + let mut actual = actual.all.keys().map(|s| s.as_str()).collect::>(); + actual.sort(); + assert_eq!(actual, expected); + } }