Skip to content
Merged
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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ root = true
# All files
[*]
indent_style = space
guidelines = 100, 160

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

Nothing yet.
### Added

- Implemented `EnvironmentHelper` and tests.

## [1.21.1] - 2025-11-29

Expand Down
96 changes: 96 additions & 0 deletions src/Logitar/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace Logitar;

/// <summary>
/// Defines helper methods for environment variables.
/// </summary>
public static class EnvironmentHelper
{
/// <summary>
/// Retrieves the boolean value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static bool GetBoolean(string variable, bool defaultValue = false) => TryGetBoolean(variable) ?? defaultValue;
/// <summary>
/// Retrieves the boolean value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static bool? TryGetBoolean(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && bool.TryParse(value.Trim(), out bool boolean) ? boolean : null;
}

/// <summary>
/// Retrieves the enum value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static T GetEnum<T>(string variable, T defaultValue = default) where T : struct, Enum
{
return TryGetEnum<T>(variable) ?? defaultValue;
}
/// <summary>
/// Retrieves the enum value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static T? TryGetEnum<T>(string variable) where T : struct, Enum
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && Enum.TryParse(value, out T enumValue) && Enum.IsDefined(typeof(T), enumValue) ? enumValue : null;
}

/// <summary>
/// Retrieves the integer value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static int GetInt32(string variable, int defaultValue = 0) => TryGetInt32(variable) ?? defaultValue;
/// <summary>
/// Retrieves the integer value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static int? TryGetInt32(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int integer) ? integer : null;
}

/// <summary>
/// Retrieves the string value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static string GetString(string variable, string defaultValue = "") => TryGetString(variable) ?? defaultValue;
/// <summary>
/// Retrieves the string value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static string? TryGetString(string variable) => Environment.GetEnvironmentVariable(variable)?.CleanTrim();

/// <summary>
/// Retrieves the TimeSpan value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static TimeSpan GetTimeSpan(string variable, TimeSpan defaultValue = default) => TryGetTimeSpan(variable) ?? defaultValue;
/// <summary>
/// Retrieves the TimeSpan value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static TimeSpan? TryGetTimeSpan(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && TimeSpan.TryParse(value.Trim(), out TimeSpan timeSpan) ? timeSpan : null;
}
}
187 changes: 187 additions & 0 deletions tests/Logitar.UnitTests/EnvironmentHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
namespace Logitar;

[Trait(Traits.Category, Categories.Unit)]
public class EnvironmentHelperTests
{
private const string VariableName = "MyVariable";

[Theory(DisplayName = "GetBoolean: it should return the found variable value.")]
[InlineData(false)]
[InlineData(true)]
public void Given_Found_When_GetBoolean_Then_BooleanValue(bool value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
bool variable = EnvironmentHelper.GetBoolean(VariableName);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Theory(DisplayName = "GetBoolean: it should return the default variable value.")]
[InlineData(false)]
[InlineData(true)]
public void Given_NotFound_When_GetBoolean_Then_DefaultValue(bool defaultValue)
{
bool variable = EnvironmentHelper.GetBoolean(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Theory(DisplayName = "GetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Sunday)]
public void Given_Found_When_GetEnum_Then_BooleanValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek variable = EnvironmentHelper.GetEnum<DayOfWeek>(VariableName);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Theory(DisplayName = "GetEnum: it should return the default variable value.")]
[InlineData(DayOfWeek.Friday)]
public void Given_NotFound_When_GetEnum_Then_DefaultValue(DayOfWeek defaultValue)
{
DayOfWeek variable = EnvironmentHelper.GetEnum(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Theory(DisplayName = "GetInt32: it should return the found variable value.")]
[InlineData(42)]
public void Given_Found_When_GetInt32_Then_BooleanValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int variable = EnvironmentHelper.GetInt32(VariableName);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Theory(DisplayName = "GetInt32: it should return the default variable value.")]
[InlineData(-1)]
public void Given_NotFound_When_GetInt32_Then_DefaultValue(int defaultValue)
{
int variable = EnvironmentHelper.GetInt32(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Theory(DisplayName = "GetString: it should return the found variable value.")]
[InlineData(" Test ")]
public void Given_Found_When_GetString_Then_BooleanValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
string variable = EnvironmentHelper.GetString(VariableName);
Assert.Equal(value.Trim(), variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Theory(DisplayName = "GetString: it should return the default variable value.")]
[InlineData("<>")]
public void Given_NotFound_When_GetString_Then_DefaultValue(string defaultValue)
{
string variable = EnvironmentHelper.GetString(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Theory(DisplayName = "GetTimeSpan: it should return the found variable value.")]
[InlineData(" 12:34:56 ")]
public void Given_Found_When_GetTimeSpan_Then_BooleanValue(string value)
{
TimeSpan timeSpan = TimeSpan.Parse(value);
Environment.SetEnvironmentVariable(VariableName, value);
TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName);
Assert.Equal(timeSpan, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Theory(DisplayName = "GetTimeSpan: it should return the default variable value.")]
[InlineData("00:00:00")]
public void Given_NotFound_When_GetTimeSpan_Then_DefaultValue(string defaultValue)
{
TimeSpan timeSpan = TimeSpan.Parse(defaultValue);
TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName, timeSpan);
Assert.Equal(timeSpan, variable);
}

[Theory(DisplayName = "TryGetBoolean: it should return the found variable value.")]
[InlineData(false)]
[InlineData(true)]
public void Given_Found_When_TryGetBoolean_Then_BooleanValue(bool value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
bool? variable = EnvironmentHelper.TryGetBoolean(VariableName);
Assert.NotNull(variable);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetBoolean: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetBoolean_Then_BooleanValue()
{
Assert.Null(EnvironmentHelper.TryGetBoolean(VariableName));
}

[Theory(DisplayName = "TryGetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Saturday)]
public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek? variable = EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName);
Assert.NotNull(variable);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetEnum: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetEnum_Then_BooleanValue()
{
Assert.Null(EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName));
}

[Theory(DisplayName = "TryGetInt32: it should return the found variable value.")]
[InlineData(999)]
public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int? variable = EnvironmentHelper.TryGetInt32(VariableName);
Assert.NotNull(variable);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetInt32: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetInt32_Then_BooleanValue()
{
Assert.Null(EnvironmentHelper.TryGetInt32(VariableName));
}

[Theory(DisplayName = "TryGetString: it should return the found variable value.")]
[InlineData(" Test ")]
public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
string? variable = EnvironmentHelper.TryGetString(VariableName);
Assert.NotNull(variable);
Assert.Equal(value.Trim(), variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetString: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetString_Then_BooleanValue()
{
Assert.Null(EnvironmentHelper.TryGetString(VariableName));
}

[Theory(DisplayName = "TryGetTimeSpan: it should return the found variable value.")]
[InlineData(" 7.00:00:00 ")]
public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
TimeSpan? variable = EnvironmentHelper.TryGetTimeSpan(VariableName);
Assert.NotNull(variable);
Assert.Equal(TimeSpan.Parse(value.Trim()), variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetTimeSpan: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetTimeSpan_Then_BooleanValue()
{
Assert.Null(EnvironmentHelper.TryGetTimeSpan(VariableName));
}
}