Skip to content

Commit f4998bf

Browse files
committed
resolves test fail issue + improved reability
Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent 69bb966 commit f4998bf

File tree

8 files changed

+23
-18
lines changed

8 files changed

+23
-18
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/src/env.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Environment variable utilities
2+
3+
/// Defines the default environment variable prefix used by Spin.
4+
pub const DEFAULT_ENV_PREFIX: &str = "SPIN_VARIABLE";
5+
6+
/// Creates an environment variable key based on the given prefix and key.
7+
pub fn env_key(prefix: Option<String>, key: &str) -> String {
8+
let prefix = prefix.unwrap_or_else(|| DEFAULT_ENV_PREFIX.to_string());
9+
let upper_key = key.to_ascii_uppercase();
10+
let key = format!("{prefix}_{upper_key}");
11+
key
12+
}

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
pub mod arg_parser;
1212
pub mod data_dir;
13+
pub mod env;
1314
pub mod paths;
1415
pub mod sha256;
1516
pub mod sloth;

crates/factors-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = { workspace = true }
66

77
[dependencies]
88
spin-app = { path = "../app" }
9+
spin-common = { path = "../common" }
910
spin-factors = { path = "../factors" }
1011
spin-loader = { path = "../loader" }
1112
spin-telemetry = { path = "../telemetry", features = ["testing"] }

crates/factors-test/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use spin_app::locked::LockedApp;
2+
use spin_common::env::env_key;
23
use spin_factors::{
34
anyhow::{self, Context},
45
wasmtime::{component::Linker, Config, Engine},
@@ -100,6 +101,8 @@ impl<T: RuntimeFactors> TestEnvironment<T> {
100101
pub async fn build_locked_app(manifest: &toml::Table) -> anyhow::Result<LockedApp> {
101102
let toml_str = toml::to_string(manifest).context("failed serializing manifest")?;
102103
let dir = tempfile::tempdir().context("failed creating tempdir")?;
104+
// `foo` variable is set to require. As we're not providing a default value, env is checked.
105+
_ = std::env::set_var(env_key(None, "foo"), "baz");
103106
let path = dir.path().join("spin.toml");
104107
std::fs::write(&path, toml_str).context("failed writing manifest")?;
105108
spin_loader::from_file(&path, FilesMountStrategy::Direct, None).await

crates/loader/src/local.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use spin_locked_app::{
1616
use spin_manifest::schema::v2::{self, AppManifest, KebabId, WasiFilesMount};
1717
use spin_outbound_networking_config::allowed_hosts::{AllowedHostConfig, AllowedHostsConfig};
1818
use spin_serde::DependencyName;
19-
use spin_variables::DEFAULT_ENV_PREFIX;
2019
use std::collections::BTreeMap;
2120
use tokio::{io::AsyncWriteExt, sync::Semaphore};
2221

@@ -142,15 +141,9 @@ impl LocalLoader {
142141
})
143142
}
144143

145-
fn env_key_creator(key: &str) -> String {
146-
let upper_key = key.to_ascii_uppercase();
147-
let key = format!("{DEFAULT_ENV_PREFIX}_{upper_key}");
148-
key
149-
}
150-
151144
fn env_checker((key, val): (String, Variable)) -> anyhow::Result<(String, Variable)> {
152145
if val.default.is_none() {
153-
if std::env::var(Self::env_key_creator(key.as_ref())).is_err() {
146+
if std::env::var(env_key(None, key.as_ref())).is_err() {
154147
Err(anyhow::anyhow!(
155148
"Variable data not provided for {}",
156149
quoted_path(key)

crates/variables-azure/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ azure_core = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4ca
1313
azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
1414
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
1515
serde = { workspace = true }
16+
spin-common = { path = "../common" }
1617
spin-expressions = { path = "../expressions" }
1718
spin-factors = { path = "../factors" }
1819
spin-world = { path = "../world" }

crates/variables-env/src/lib.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use serde::Deserialize;
9+
use spin_common::env::env_key;
910
use spin_expressions::{Key, Provider};
1011
use spin_factors::anyhow::{self, Context as _};
1112
use spin_world::async_trait;
@@ -25,8 +26,6 @@ pub struct EnvVariablesConfig {
2526
pub dotenv_path: Option<PathBuf>,
2627
}
2728

28-
pub const DEFAULT_ENV_PREFIX: &str = "SPIN_VARIABLE";
29-
3029
type EnvFetcherFn = Box<dyn Fn(&str) -> Result<String, VarError> + Send + Sync>;
3130

3231
/// A [`Provider`] that uses environment variables.
@@ -71,14 +70,7 @@ impl EnvVariablesProvider {
7170

7271
/// Gets the value of a variable from the environment.
7372
fn get_sync(&self, key: &Key) -> anyhow::Result<Option<String>> {
74-
let prefix = self
75-
.prefix
76-
.clone()
77-
.unwrap_or_else(|| DEFAULT_ENV_PREFIX.to_string());
78-
79-
let upper_key = key.as_ref().to_ascii_uppercase();
80-
let env_key = format!("{prefix}_{upper_key}");
81-
73+
let env_key = env_key(self.prefix.clone(), key.as_ref());
8274
self.query_env(&env_key)
8375
}
8476

0 commit comments

Comments
 (0)