Skip to content

Commit ffa4aad

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

File tree

8 files changed

+24
-19
lines changed

8 files changed

+24
-19
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: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33
use anyhow::{anyhow, bail, ensure, Context, Result};
44
use futures::{future::try_join_all, StreamExt};
55
use reqwest::Url;
6-
use spin_common::{paths::parent_dir, sloth, ui::quoted_path};
6+
use spin_common::{env::env_key, paths::parent_dir, sloth, ui::quoted_path};
77
use spin_locked_app::{
88
locked::{
99
self, ContentPath, ContentRef, LockedApp, LockedComponent, LockedComponentDependency,
@@ -17,7 +17,6 @@ use spin_outbound_networking_config::allowed_hosts::{
1717
AllowedHostsConfig, SERVICE_CHAINING_DOMAIN_SUFFIX,
1818
};
1919
use spin_serde::DependencyName;
20-
use spin_variables::DEFAULT_ENV_PREFIX;
2120
use std::collections::BTreeMap;
2221
use tokio::{io::AsyncWriteExt, sync::Semaphore};
2322

@@ -137,15 +136,9 @@ impl LocalLoader {
137136
})
138137
}
139138

140-
fn env_key_creator(key: &str) -> String {
141-
let upper_key = key.to_ascii_uppercase();
142-
let key = format!("{DEFAULT_ENV_PREFIX}_{upper_key}");
143-
key
144-
}
145-
146139
fn env_checker((key, val): (String, Variable)) -> anyhow::Result<(String, Variable)> {
147140
if val.default.is_none() {
148-
if std::env::var(Self::env_key_creator(key.as_ref())).is_err() {
141+
if std::env::var(env_key(None, key.as_ref())).is_err() {
149142
Err(anyhow::anyhow!(
150143
"Variable data not provided for {}",
151144
quoted_path(key)

crates/variables/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8
1414
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
1515
dotenvy = "0.15"
1616
serde = { workspace = true }
17+
spin-common = { path = "../common" }
1718
spin-expressions = { path = "../expressions" }
1819
spin-factor-variables = { path = "../factor-variables" }
1920
spin-factors = { path = "../factors" }

crates/variables/src/env.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)