chore: add hrynco common library solution
- add the standalone HrynCo.Common solution and projects - include the shared common library source and tests - add package metadata and a repo gitignore
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
namespace HrynCo.Common.Tests;
|
||||
|
||||
using HrynCo.Common;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Xunit;
|
||||
|
||||
public sealed class ConfigurationFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void CreateConfiguration_ShouldLoadJsonAndEnvironmentValues()
|
||||
{
|
||||
string basePath = CreateTempDirectory();
|
||||
string originalEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine(basePath, "appsettings.json"), """
|
||||
{
|
||||
"Title": "base",
|
||||
"Nested": {
|
||||
"Value": "base"
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
File.WriteAllText(Path.Combine(basePath, "appsettings.Test.json"), """
|
||||
{
|
||||
"Nested": {
|
||||
"Value": "override"
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
File.WriteAllText(Path.Combine(basePath, "local.settings.json"), """
|
||||
{
|
||||
"Local": "enabled"
|
||||
}
|
||||
""");
|
||||
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Test");
|
||||
Environment.SetEnvironmentVariable("TITLE", "from-env");
|
||||
|
||||
IConfigurationRoot configuration = ConfigurationFactory.CreateConfiguration(basePath);
|
||||
|
||||
configuration["Title"].Should().Be("from-env");
|
||||
configuration["Nested:Value"].Should().Be("override");
|
||||
configuration["Local"].Should().Be("enabled");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", originalEnvironment);
|
||||
Environment.SetEnvironmentVariable("TITLE", null);
|
||||
Directory.Delete(basePath, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateConfiguration_ShouldThrowWhenBasePathIsMissing()
|
||||
{
|
||||
string missingPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
|
||||
|
||||
Action act = () => ConfigurationFactory.CreateConfiguration(missingPath, "Production");
|
||||
|
||||
act.Should().Throw<DirectoryNotFoundException>()
|
||||
.WithMessage($"Directory {missingPath} does not exist. Wrong value of the basePath parameter.");
|
||||
}
|
||||
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
|
||||
Directory.CreateDirectory(path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user