Skip to content
Merged

Dev #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions CSM Foundation Core.UnitTests/Utils/SystemUtils_UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ 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);

Assert.Equal(_varValue, SystemUtils.GetVar(_varKey));
}

[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);

Assert.Equal(_varValue, Environment.GetEnvironmentVariable(_varKey));
}

[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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -88,4 +88,18 @@ 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() {
if (!OperatingSystem.IsWindows())
return;

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));
}
}
16 changes: 16 additions & 0 deletions CSM Foundation Core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion CSM Foundation Core/CSM Foundation Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CSM_Foundation_Core</RootNamespace>
<PackageId>CSM.Foundation.Core</PackageId>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Authors>Renato Urrea</Authors>
<Company>Cosmos (CSM)</Company>
<RepositoryUrl>https://github.com/Cosmos-CSM/csm_foundation_core</RepositoryUrl>
Expand Down
38 changes: 36 additions & 2 deletions CSM Foundation Core/Core/Utils/SystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ public static class SystemUtils {
return var;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="varKey">
/// Variable key.
/// </param>
/// <returns>
/// Variable value.
/// </returns>
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;
}

/// <summary>
/// Sets a system environment variable.
/// </summary>
Expand All @@ -85,8 +116,11 @@ public static class SystemUtils {
/// <param name="varValue">
/// Variable value.
/// </param>
public static void SetVar(string varKey, string? varValue) {
Environment.SetEnvironmentVariable(varKey, varValue);
/// <param name="target">
/// Environment variable target context.
/// </param>
public static void SetVar(string varKey, string? varValue, EnvironmentVariableTarget target = EnvironmentVariableTarget.Process) {
Environment.SetEnvironmentVariable(varKey, varValue, target);
}


Expand Down
Loading