From 4ed50c1c2a455717ee82df923a25b9fc8493beac Mon Sep 17 00:00:00 2001 From: Juan Renato Urrea Ortiz Date: Thu, 25 Dec 2025 01:08:08 -0800 Subject: [PATCH 1/2] updated to get global vars --- .../Utils/SystemUtils_UnitTests.cs | 24 +++++++++--- CSM Foundation Core/CHANGELOG.md | 16 ++++++++ .../CSM Foundation Core.csproj | 2 +- CSM Foundation Core/Core/Utils/SystemUtils.cs | 38 ++++++++++++++++++- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs b/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs index 334f7c4..7d7df1c 100644 --- a/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs +++ b/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs @@ -14,7 +14,7 @@ public class SystemUtils_UnitTests { [Fact(DisplayName = "[GetVar(varKey)]: Correctly gets a previous store variable")] - public void GetVar_A() { + public void GetPreviousStoredVar() { Environment.SetEnvironmentVariable(_varKey, _varValue); @@ -22,7 +22,7 @@ public void GetVar_A() { } [Fact(DisplayName = "[SetVar(varKey, varValue)]: Correctly sets a variable and can be gathered by the system")] - public void SetVar_A() { + public void SetsVar() { SystemUtils.SetVar(_varKey, _varValue); @@ -30,7 +30,7 @@ public void SetVar_A() { } [Fact(DisplayName = "[GetEnv(varKey)]: Correctly gets the environment (DEVELOPMENT | DEV)")] - public void GetEnv_A() { + public void GetsVarFromDevelopment() { SystemUtils.SetVar(Constants.Framework.ASPNETCORE_ENVIRONEMNT, Constants.Environments.DEVELOPMENT); SystemEnvs developmentEnv = SystemUtils.GetEnv(); @@ -45,7 +45,7 @@ public void GetEnv_A() { } [Fact(DisplayName = "[GetEnv(varKey)]: Correctly gets the environment (PRODUCTION | PROD)")] - public void GetEnv_B() { + public void GetsVarFromProd() { SystemUtils.SetVar(Constants.Framework.ASPNETCORE_ENVIRONEMNT, Constants.Environments.PRODUCTION); SystemEnvs productionEnv = SystemUtils.GetEnv(); @@ -60,7 +60,7 @@ public void GetEnv_B() { } [Fact(DisplayName = "[GetEnv(varKey)]: Correctly gets the environment (QUALITY | QA)")] - public void GetEnv_C() { + public void GetsVarFromQuality() { SystemUtils.SetVar(Constants.Framework.ASPNETCORE_ENVIRONEMNT, Constants.Environments.QUALITY); SystemEnvs qualityEnv = SystemUtils.GetEnv(); @@ -75,7 +75,7 @@ public void GetEnv_C() { } [Fact(DisplayName = "[GetEnv(varKey)]: Correctly gets the environment (LABORATORY | LAB)")] - public void GetEnv_D() { + public void GetsVarFromLab() { SystemUtils.SetVar(Constants.Framework.ASPNETCORE_ENVIRONEMNT, Constants.Environments.LABORATORY); SystemEnvs laboratoryEnv = SystemUtils.GetEnv(); @@ -88,4 +88,16 @@ public void GetEnv_D() { Assert.Equal(SystemEnvs.LAB, laboratoryEnv); } + + [Fact(DisplayName = "[GetGlobalVar(varKey)]: Correctly gets the expected variable value from the target")] + public void GetsGlobalVarMapped() { + + string expectedValue = "USER_VAR"; + + // --> First we want to get from User not Process + SystemUtils.SetVar(_varKey, expectedValue, EnvironmentVariableTarget.User); + SystemUtils.SetVar(_varKey, "PROCESS_VAR", EnvironmentVariableTarget.Process); + + Assert.Equal(expectedValue, SystemUtils.GetGlobalVar(_varKey)); + } } diff --git a/CSM Foundation Core/CHANGELOG.md b/CSM Foundation Core/CHANGELOG.md index 6c95b02..421d6d8 100644 --- a/CSM Foundation Core/CHANGELOG.md +++ b/CSM Foundation Core/CHANGELOG.md @@ -1,5 +1,21 @@ # CSM Foundation Core CHANGELOG +## [2.1.1] - 24.12-2025 + +### Changed + +- Changed a SystemUtils method [SetVar] to allow env context target. + +### Added + +- Added a new method to [SystemUtils] named [GetGlobalVar] that maps all available environment variables target contexts to get the value. + +#### Dependencies + +| Package | Previous Version | New Version | +|:----------------------------------------|:----------------:|:---------------:| +| | | | + ## [2.1.0] - 22.12-2025 ### Changed diff --git a/CSM Foundation Core/CSM Foundation Core.csproj b/CSM Foundation Core/CSM Foundation Core.csproj index a91a6c0..695fd6f 100644 --- a/CSM Foundation Core/CSM Foundation Core.csproj +++ b/CSM Foundation Core/CSM Foundation Core.csproj @@ -4,7 +4,7 @@ net8.0 CSM_Foundation_Core CSM.Foundation.Core - 2.1.0 + 2.1.1 Renato Urrea Cosmos (CSM) https://github.com/Cosmos-CSM/csm_foundation_core diff --git a/CSM Foundation Core/Core/Utils/SystemUtils.cs b/CSM Foundation Core/Core/Utils/SystemUtils.cs index f723533..474ecdc 100644 --- a/CSM Foundation Core/Core/Utils/SystemUtils.cs +++ b/CSM Foundation Core/Core/Utils/SystemUtils.cs @@ -76,6 +76,37 @@ public static class SystemUtils { return var; } + /// + /// Gets a variable from all the available environemnt targets, using the following order: 1. Machine, 2. User, 3. Process. + /// If it doesn't get a value will fallback to the next available target, until it gets a value. + /// + /// + /// Variable key. + /// + /// + /// Variable value. + /// + public static string? GetGlobalVar(string varKey) { + EnvironmentVariableTarget[] targetsSequence = [ + EnvironmentVariableTarget.Machine, + EnvironmentVariableTarget.User, + EnvironmentVariableTarget.Process, + ]; + + string? envVarValue = null; + foreach (EnvironmentVariableTarget target in targetsSequence) { + if(!string.IsNullOrWhiteSpace(envVarValue)) + break; + + + envVarValue = GetVar(varKey, target); + if(!string.IsNullOrWhiteSpace(envVarValue)) + break; + } + + return envVarValue; + } + /// /// Sets a system environment variable. /// @@ -85,8 +116,11 @@ public static class SystemUtils { /// /// Variable value. /// - public static void SetVar(string varKey, string? varValue) { - Environment.SetEnvironmentVariable(varKey, varValue); + /// + /// Environment variable target context. + /// + public static void SetVar(string varKey, string? varValue, EnvironmentVariableTarget target = EnvironmentVariableTarget.Process) { + Environment.SetEnvironmentVariable(varKey, varValue, target); } From 15762303d0ddaa3ce4a0b43c8161a5893aaefcfa Mon Sep 17 00:00:00 2001 From: Juan Renato Urrea Ortiz Date: Thu, 25 Dec 2025 01:13:21 -0800 Subject: [PATCH 2/2] disabling test when no windows based env --- CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs b/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs index 7d7df1c..b2dedb0 100644 --- a/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs +++ b/CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs @@ -91,7 +91,9 @@ public void GetsVarFromLab() { [Fact(DisplayName = "[GetGlobalVar(varKey)]: Correctly gets the expected variable value from the target")] public void GetsGlobalVarMapped() { - + if (!OperatingSystem.IsWindows()) + return; + string expectedValue = "USER_VAR"; // --> First we want to get from User not Process