Skip to content

Commit

Permalink
Merge pull request #13 from wemogy/main
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
SebastianKuesters authored Jun 18, 2024
2 parents a371b6f + 35ee498 commit 58d5c0f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/Wemogy.Configuration.Tests/ConfigurationExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace Wemogy.Configuration.Tests;

public class ConfigurationExtensionsTests
{
[Fact]
public void GetRequiredValue_ThrowsException_WhenKeyIsMissing()
{
// Arrange
var configuration = new ConfigurationBuilder().Build();

// Act
void Act() => configuration.GetRequiredValue("MissingKey");

// Assert
Assert.Throws<InvalidOperationException>((Action)Act);
}

[Fact]
public void GetRequiredValue_ReturnsValue_WhenKeyIsPresent()
{
// Arrange
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("ExistingKey", "ExistingValue")
})
.Build();

// Act
var value = configuration.GetRequiredValue("ExistingKey");

// Assert
Assert.Equal("ExistingValue", value);
}
}
20 changes: 20 additions & 0 deletions src/Wemogy.Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Microsoft.Extensions.Configuration;

namespace Wemogy.Configuration
{
public static class ConfigurationExtensions
{
public static string GetRequiredValue(this IConfiguration configuration, string key)
{
var value = configuration[key];

if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidOperationException($"Configuration value for key '{key}' is required.");
}

return value;
}
}
}
2 changes: 1 addition & 1 deletion src/Wemogy.Configuration/ConfigurationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IConfiguration BuildConfiguration(HostBuilderContext hostingContex
/// 4. Environment Variables
/// </summary>
/// <returns>Built configuration</returns>
public static IConfiguration BuildConfiguration(string environmentName = null)
public static IConfiguration BuildConfiguration(string? environmentName = null)
{
return new ConfigurationBuilder()
.AddDefaultJsonFiles(environmentName)
Expand Down
1 change: 1 addition & 0 deletions src/Wemogy.Configuration/Wemogy.Configuration.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 58d5c0f

Please sign in to comment.