Skip to content

Commit f5d6826

Browse files
committed
test: Use Before and After hooks to set SEQCLI_CONFIG_FILE environment
1 parent 26a4cd6 commit f5d6826

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

test/SeqCli.Tests/Config/ConfigFileLocationTests.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using SeqCli.Config;
44
using Xunit;
5+
using Xunit.Sdk;
56

67
namespace SeqCli.Tests.Config;
78

@@ -16,15 +17,31 @@ public void DefaultConfigFilename()
1617
}
1718

1819
[Fact]
20+
[EnvironmentOverridenConfigFilenameBeforeAfter(SeqCliConfigFile = "MyCustomSeqCli.json")]
1921
public void EnvironmentOverridenConfigFilename()
2022
{
21-
var tempConfigFile = Path.GetTempFileName();
22-
Environment.SetEnvironmentVariable("SEQCLI_CONFIG_FILE", tempConfigFile);
2323
var configFile = RuntimeConfigurationLoader.SeqCliConfigFilename();
24-
// Clean up immediately to avoid affecting environment for other tests
25-
// TODO: Or better move to public void Dispose() ?
26-
Environment.SetEnvironmentVariable("SEQCLI_CONFIG_FILE", null);
24+
var customConfigFile = Path.Combine(Path.GetTempPath(), "MyCustomSeqCli.json");
25+
Assert.Equal(customConfigFile, configFile);
26+
}
27+
}
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+
}
2742

28-
Assert.Equal(tempConfigFile, configFile);
43+
public override void After(System.Reflection.MethodInfo methodUnderTest)
44+
{
45+
Environment.SetEnvironmentVariable("SEQCLI_CONFIG_FILE", originalValue);
2946
}
3047
}

0 commit comments

Comments
 (0)