Skip to content

Commit 1ec1f0a

Browse files
committed
Make SeqCliConfigFilename test-friendlier with pluggable environment
1 parent f5d6826 commit 1ec1f0a

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/SeqCli/Config/RuntimeConfigurationLoader.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.IO;
18+
using System.Linq;
1719

1820
namespace SeqCli.Config;
1921

@@ -43,8 +45,13 @@ public static SeqCliConfig Load()
4345
/// </summary>
4446
public static string SeqCliConfigFilename()
4547
{
46-
var customConfigFilename = Environment.GetEnvironmentVariable("SEQCLI_CONFIG_FILE");
47-
if (!string.IsNullOrEmpty(customConfigFilename))
48+
var environment = Environment.GetEnvironmentVariables();
49+
return SeqCliConfigFilename(environment.Keys.Cast<string>().ToDictionary(k => k, k => (string?)environment[k]));
50+
}
51+
52+
internal static string SeqCliConfigFilename(Dictionary<string, string?> environment)
53+
{
54+
if (environment.TryGetValue("SEQCLI_CONFIG_FILE", out var customConfigFilename) && !string.IsNullOrEmpty(customConfigFilename))
4855
{
4956
return customConfigFilename;
5057
}
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
1+
using System.Collections.Generic;
22
using System.IO;
33
using SeqCli.Config;
44
using Xunit;
5-
using Xunit.Sdk;
65

76
namespace SeqCli.Tests.Config;
87

@@ -17,31 +16,13 @@ public void DefaultConfigFilename()
1716
}
1817

1918
[Fact]
20-
[EnvironmentOverridenConfigFilenameBeforeAfter(SeqCliConfigFile = "MyCustomSeqCli.json")]
2119
public void EnvironmentOverridenConfigFilename()
2220
{
23-
var configFile = RuntimeConfigurationLoader.SeqCliConfigFilename();
24-
var customConfigFile = Path.Combine(Path.GetTempPath(), "MyCustomSeqCli.json");
21+
var customConfigFile = Path.GetTempFileName();
22+
var environment = new Dictionary<string, string>();
23+
environment.Add("SEQCLI_CONFIG_FILE", customConfigFile);
24+
25+
var configFile = RuntimeConfigurationLoader.SeqCliConfigFilename(environment);
2526
Assert.Equal(customConfigFile, configFile);
2627
}
2728
}
28-
29-
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
30-
public class EnvironmentOverridenConfigFilenameBeforeAfter : BeforeAfterTestAttribute
31-
{
32-
private string originalValue;
33-
public string SeqCliConfigFile { get; set; }
34-
35-
public override void Before(System.Reflection.MethodInfo methodUnderTest)
36-
{
37-
originalValue = Environment.GetEnvironmentVariable("SEQCLI_CONFIG_FILE");
38-
39-
var configFile = Path.Combine(Path.GetTempPath(), SeqCliConfigFile);
40-
Environment.SetEnvironmentVariable("SEQCLI_CONFIG_FILE", configFile);
41-
}
42-
43-
public override void After(System.Reflection.MethodInfo methodUnderTest)
44-
{
45-
Environment.SetEnvironmentVariable("SEQCLI_CONFIG_FILE", originalValue);
46-
}
47-
}

0 commit comments

Comments
 (0)