Skip to content

Commit 2164d35

Browse files
authored
Implemented EnvironmentHelper and tests. (#212)
1 parent 6e96a9b commit 2164d35

4 files changed

Lines changed: 287 additions & 1 deletion

File tree

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ root = true
55
# All files
66
[*]
77
indent_style = space
8+
guidelines = 100, 160
89

910
# XML project files
1011
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
Nothing yet.
10+
### Added
11+
12+
- Implemented `EnvironmentHelper` and tests.
1113

1214
## [1.21.1] - 2025-11-29
1315

src/Logitar/EnvironmentHelper.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
namespace Logitar;
2+
3+
/// <summary>
4+
/// Defines helper methods for environment variables.
5+
/// </summary>
6+
public static class EnvironmentHelper
7+
{
8+
/// <summary>
9+
/// Retrieves the boolean value of an environment variable.
10+
/// </summary>
11+
/// <param name="variable">The name of the variable.</param>
12+
/// <param name="defaultValue">The default value.</param>
13+
/// <returns>The value of the environment value.</returns>
14+
public static bool GetBoolean(string variable, bool defaultValue = false) => TryGetBoolean(variable) ?? defaultValue;
15+
/// <summary>
16+
/// Retrieves the boolean value of an environment variable. Returns null if it is not found.
17+
/// </summary>
18+
/// <param name="variable">The name of the variable.</param>
19+
/// <returns>The value of the environment value, or null if not found.</returns>
20+
public static bool? TryGetBoolean(string variable)
21+
{
22+
string value = Environment.GetEnvironmentVariable(variable);
23+
return !string.IsNullOrWhiteSpace(value) && bool.TryParse(value.Trim(), out bool boolean) ? boolean : null;
24+
}
25+
26+
/// <summary>
27+
/// Retrieves the enum value of an environment variable.
28+
/// </summary>
29+
/// <param name="variable">The name of the variable.</param>
30+
/// <param name="defaultValue">The default value.</param>
31+
/// <returns>The value of the environment value.</returns>
32+
public static T GetEnum<T>(string variable, T defaultValue = default) where T : struct, Enum
33+
{
34+
return TryGetEnum<T>(variable) ?? defaultValue;
35+
}
36+
/// <summary>
37+
/// Retrieves the enum value of an environment variable. Returns null if it is not found.
38+
/// </summary>
39+
/// <param name="variable">The name of the variable.</param>
40+
/// <returns>The value of the environment value, or null if not found.</returns>
41+
public static T? TryGetEnum<T>(string variable) where T : struct, Enum
42+
{
43+
string value = Environment.GetEnvironmentVariable(variable);
44+
return !string.IsNullOrWhiteSpace(value) && Enum.TryParse(value, out T enumValue) && Enum.IsDefined(typeof(T), enumValue) ? enumValue : null;
45+
}
46+
47+
/// <summary>
48+
/// Retrieves the integer value of an environment variable.
49+
/// </summary>
50+
/// <param name="variable">The name of the variable.</param>
51+
/// <param name="defaultValue">The default value.</param>
52+
/// <returns>The value of the environment value.</returns>
53+
public static int GetInt32(string variable, int defaultValue = 0) => TryGetInt32(variable) ?? defaultValue;
54+
/// <summary>
55+
/// Retrieves the integer value of an environment variable. Returns null if it is not found.
56+
/// </summary>
57+
/// <param name="variable">The name of the variable.</param>
58+
/// <returns>The value of the environment value, or null if not found.</returns>
59+
public static int? TryGetInt32(string variable)
60+
{
61+
string value = Environment.GetEnvironmentVariable(variable);
62+
return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int integer) ? integer : null;
63+
}
64+
65+
/// <summary>
66+
/// Retrieves the string value of an environment variable.
67+
/// </summary>
68+
/// <param name="variable">The name of the variable.</param>
69+
/// <param name="defaultValue">The default value.</param>
70+
/// <returns>The value of the environment value.</returns>
71+
public static string GetString(string variable, string defaultValue = "") => TryGetString(variable) ?? defaultValue;
72+
/// <summary>
73+
/// Retrieves the string value of an environment variable. Returns null if it is not found.
74+
/// </summary>
75+
/// <param name="variable">The name of the variable.</param>
76+
/// <returns>The value of the environment value, or null if not found.</returns>
77+
public static string? TryGetString(string variable) => Environment.GetEnvironmentVariable(variable)?.CleanTrim();
78+
79+
/// <summary>
80+
/// Retrieves the TimeSpan value of an environment variable.
81+
/// </summary>
82+
/// <param name="variable">The name of the variable.</param>
83+
/// <param name="defaultValue">The default value.</param>
84+
/// <returns>The value of the environment value.</returns>
85+
public static TimeSpan GetTimeSpan(string variable, TimeSpan defaultValue = default) => TryGetTimeSpan(variable) ?? defaultValue;
86+
/// <summary>
87+
/// Retrieves the TimeSpan value of an environment variable. Returns null if it is not found.
88+
/// </summary>
89+
/// <param name="variable">The name of the variable.</param>
90+
/// <returns>The value of the environment value, or null if not found.</returns>
91+
public static TimeSpan? TryGetTimeSpan(string variable)
92+
{
93+
string value = Environment.GetEnvironmentVariable(variable);
94+
return !string.IsNullOrWhiteSpace(value) && TimeSpan.TryParse(value.Trim(), out TimeSpan timeSpan) ? timeSpan : null;
95+
}
96+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
namespace Logitar;
2+
3+
[Trait(Traits.Category, Categories.Unit)]
4+
public class EnvironmentHelperTests
5+
{
6+
private const string VariableName = "MyVariable";
7+
8+
[Theory(DisplayName = "GetBoolean: it should return the found variable value.")]
9+
[InlineData(false)]
10+
[InlineData(true)]
11+
public void Given_Found_When_GetBoolean_Then_BooleanValue(bool value)
12+
{
13+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
14+
bool variable = EnvironmentHelper.GetBoolean(VariableName);
15+
Assert.Equal(value, variable);
16+
Environment.SetEnvironmentVariable(VariableName, null);
17+
}
18+
19+
[Theory(DisplayName = "GetBoolean: it should return the default variable value.")]
20+
[InlineData(false)]
21+
[InlineData(true)]
22+
public void Given_NotFound_When_GetBoolean_Then_DefaultValue(bool defaultValue)
23+
{
24+
bool variable = EnvironmentHelper.GetBoolean(VariableName, defaultValue);
25+
Assert.Equal(defaultValue, variable);
26+
}
27+
28+
[Theory(DisplayName = "GetEnum: it should return the found variable value.")]
29+
[InlineData(DayOfWeek.Sunday)]
30+
public void Given_Found_When_GetEnum_Then_BooleanValue(DayOfWeek value)
31+
{
32+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
33+
DayOfWeek variable = EnvironmentHelper.GetEnum<DayOfWeek>(VariableName);
34+
Assert.Equal(value, variable);
35+
Environment.SetEnvironmentVariable(VariableName, null);
36+
}
37+
38+
[Theory(DisplayName = "GetEnum: it should return the default variable value.")]
39+
[InlineData(DayOfWeek.Friday)]
40+
public void Given_NotFound_When_GetEnum_Then_DefaultValue(DayOfWeek defaultValue)
41+
{
42+
DayOfWeek variable = EnvironmentHelper.GetEnum(VariableName, defaultValue);
43+
Assert.Equal(defaultValue, variable);
44+
}
45+
46+
[Theory(DisplayName = "GetInt32: it should return the found variable value.")]
47+
[InlineData(42)]
48+
public void Given_Found_When_GetInt32_Then_BooleanValue(int value)
49+
{
50+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
51+
int variable = EnvironmentHelper.GetInt32(VariableName);
52+
Assert.Equal(value, variable);
53+
Environment.SetEnvironmentVariable(VariableName, null);
54+
}
55+
56+
[Theory(DisplayName = "GetInt32: it should return the default variable value.")]
57+
[InlineData(-1)]
58+
public void Given_NotFound_When_GetInt32_Then_DefaultValue(int defaultValue)
59+
{
60+
int variable = EnvironmentHelper.GetInt32(VariableName, defaultValue);
61+
Assert.Equal(defaultValue, variable);
62+
}
63+
64+
[Theory(DisplayName = "GetString: it should return the found variable value.")]
65+
[InlineData(" Test ")]
66+
public void Given_Found_When_GetString_Then_BooleanValue(string value)
67+
{
68+
Environment.SetEnvironmentVariable(VariableName, value);
69+
string variable = EnvironmentHelper.GetString(VariableName);
70+
Assert.Equal(value.Trim(), variable);
71+
Environment.SetEnvironmentVariable(VariableName, null);
72+
}
73+
74+
[Theory(DisplayName = "GetString: it should return the default variable value.")]
75+
[InlineData("<>")]
76+
public void Given_NotFound_When_GetString_Then_DefaultValue(string defaultValue)
77+
{
78+
string variable = EnvironmentHelper.GetString(VariableName, defaultValue);
79+
Assert.Equal(defaultValue, variable);
80+
}
81+
82+
[Theory(DisplayName = "GetTimeSpan: it should return the found variable value.")]
83+
[InlineData(" 12:34:56 ")]
84+
public void Given_Found_When_GetTimeSpan_Then_BooleanValue(string value)
85+
{
86+
TimeSpan timeSpan = TimeSpan.Parse(value);
87+
Environment.SetEnvironmentVariable(VariableName, value);
88+
TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName);
89+
Assert.Equal(timeSpan, variable);
90+
Environment.SetEnvironmentVariable(VariableName, null);
91+
}
92+
93+
[Theory(DisplayName = "GetTimeSpan: it should return the default variable value.")]
94+
[InlineData("00:00:00")]
95+
public void Given_NotFound_When_GetTimeSpan_Then_DefaultValue(string defaultValue)
96+
{
97+
TimeSpan timeSpan = TimeSpan.Parse(defaultValue);
98+
TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName, timeSpan);
99+
Assert.Equal(timeSpan, variable);
100+
}
101+
102+
[Theory(DisplayName = "TryGetBoolean: it should return the found variable value.")]
103+
[InlineData(false)]
104+
[InlineData(true)]
105+
public void Given_Found_When_TryGetBoolean_Then_BooleanValue(bool value)
106+
{
107+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
108+
bool? variable = EnvironmentHelper.TryGetBoolean(VariableName);
109+
Assert.NotNull(variable);
110+
Assert.Equal(value, variable);
111+
Environment.SetEnvironmentVariable(VariableName, null);
112+
}
113+
114+
[Fact(DisplayName = "TryGetBoolean: it should return null when the variable is not found.")]
115+
public void Given_NotFound_When_TryGetBoolean_Then_BooleanValue()
116+
{
117+
Assert.Null(EnvironmentHelper.TryGetBoolean(VariableName));
118+
}
119+
120+
[Theory(DisplayName = "TryGetEnum: it should return the found variable value.")]
121+
[InlineData(DayOfWeek.Saturday)]
122+
public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
123+
{
124+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
125+
DayOfWeek? variable = EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName);
126+
Assert.NotNull(variable);
127+
Assert.Equal(value, variable);
128+
Environment.SetEnvironmentVariable(VariableName, null);
129+
}
130+
131+
[Fact(DisplayName = "TryGetEnum: it should return null when the variable is not found.")]
132+
public void Given_NotFound_When_TryGetEnum_Then_BooleanValue()
133+
{
134+
Assert.Null(EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName));
135+
}
136+
137+
[Theory(DisplayName = "TryGetInt32: it should return the found variable value.")]
138+
[InlineData(999)]
139+
public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
140+
{
141+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
142+
int? variable = EnvironmentHelper.TryGetInt32(VariableName);
143+
Assert.NotNull(variable);
144+
Assert.Equal(value, variable);
145+
Environment.SetEnvironmentVariable(VariableName, null);
146+
}
147+
148+
[Fact(DisplayName = "TryGetInt32: it should return null when the variable is not found.")]
149+
public void Given_NotFound_When_TryGetInt32_Then_BooleanValue()
150+
{
151+
Assert.Null(EnvironmentHelper.TryGetInt32(VariableName));
152+
}
153+
154+
[Theory(DisplayName = "TryGetString: it should return the found variable value.")]
155+
[InlineData(" Test ")]
156+
public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
157+
{
158+
Environment.SetEnvironmentVariable(VariableName, value.ToString());
159+
string? variable = EnvironmentHelper.TryGetString(VariableName);
160+
Assert.NotNull(variable);
161+
Assert.Equal(value.Trim(), variable);
162+
Environment.SetEnvironmentVariable(VariableName, null);
163+
}
164+
165+
[Fact(DisplayName = "TryGetString: it should return null when the variable is not found.")]
166+
public void Given_NotFound_When_TryGetString_Then_BooleanValue()
167+
{
168+
Assert.Null(EnvironmentHelper.TryGetString(VariableName));
169+
}
170+
171+
[Theory(DisplayName = "TryGetTimeSpan: it should return the found variable value.")]
172+
[InlineData(" 7.00:00:00 ")]
173+
public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
174+
{
175+
Environment.SetEnvironmentVariable(VariableName, value);
176+
TimeSpan? variable = EnvironmentHelper.TryGetTimeSpan(VariableName);
177+
Assert.NotNull(variable);
178+
Assert.Equal(TimeSpan.Parse(value.Trim()), variable);
179+
Environment.SetEnvironmentVariable(VariableName, null);
180+
}
181+
182+
[Fact(DisplayName = "TryGetTimeSpan: it should return null when the variable is not found.")]
183+
public void Given_NotFound_When_TryGetTimeSpan_Then_BooleanValue()
184+
{
185+
Assert.Null(EnvironmentHelper.TryGetTimeSpan(VariableName));
186+
}
187+
}

0 commit comments

Comments
 (0)