Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment to use Microsoft.Extensions.Configuration #306

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Reqnroll/Configuration/ConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public static ReqnrollConfiguration GetDefault()

private ReqnrollConfiguration LoadJson(ReqnrollConfiguration reqnrollConfiguration)
{
var jsonContent = File.ReadAllText(_reqnrollJsonLocator.GetReqnrollJsonFilePath());
var jsonFilePath = _reqnrollJsonLocator.GetReqnrollJsonFilePath();

return LoadJson(reqnrollConfiguration, jsonContent);
return new MSE_RuntimeConfigurationLoader().LoadConfiguration(reqnrollConfiguration, jsonFilePath);
}

private ReqnrollConfiguration LoadJson(ReqnrollConfiguration reqnrollConfiguration, string jsonContent)
Expand Down
20 changes: 16 additions & 4 deletions Reqnroll/Configuration/JsonConfig/JsonConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ public ReqnrollConfiguration LoadJson(ReqnrollConfiguration reqnrollConfiguratio

var jsonConfig = jsonContent.FromJson<JsonConfig>();

var configuration = ApplyJsonConfig(reqnrollConfiguration, jsonConfig);
configuration.ConfigSourceText = jsonContent;

return configuration;
}

public ReqnrollConfiguration LoadJson(ReqnrollConfiguration reqnrollConfiguration, JsonConfig jsonConfig)
{
var configuration = ApplyJsonConfig(reqnrollConfiguration, jsonConfig);
configuration.ConfigSourceText = jsonConfig.ToJson();
return configuration;

}
private static ReqnrollConfiguration ApplyJsonConfig(ReqnrollConfiguration reqnrollConfiguration, JsonConfig jsonConfig)
{
var containerRegistrationCollection = reqnrollConfiguration.CustomDependencies;
var generatorContainerRegistrationCollection = reqnrollConfiguration.GeneratorCustomDependencies;
var featureLanguage = reqnrollConfiguration.FeatureLanguage;
Expand Down Expand Up @@ -122,10 +137,7 @@ public ReqnrollConfiguration LoadJson(ReqnrollConfiguration reqnrollConfiguratio
addNonParallelizableMarkerForTags,
obsoleteBehavior,
coloredOutput
)
{
ConfigSourceText = jsonContent
};
);
}
}
}
22 changes: 22 additions & 0 deletions Reqnroll/Configuration/MSE_RuntimeConfigurationLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using Microsoft.Extensions.Configuration;
using Reqnroll.Configuration.JsonConfig;

namespace Reqnroll.Configuration
{
internal class MSE_RuntimeConfigurationLoader
{
public ReqnrollConfiguration LoadConfiguration(ReqnrollConfiguration reqnrollConfiguration, string configPath)
{
var configurationManager = new ConfigurationManager();
configurationManager.AddJsonFile(configPath, optional: true, reloadOnChange: false);
configurationManager.AddEnvironmentVariables(prefix: "REQNROLL__");
//configurationManager.Build();
var config = configurationManager.Get<JsonConfig.JsonConfig>();
return new JsonConfigurationLoader().LoadJson(reqnrollConfiguration, config);
}
}
}
4 changes: 4 additions & 0 deletions Reqnroll/Reqnroll.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

<!-- build time dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="MSBuild.AdditionalTasks" Version="0.1.36" />
</ItemGroup>

Expand Down
4 changes: 4 additions & 0 deletions Reqnroll/Reqnroll.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<dependency id="Gherkin" version="29.0.0"/>
<dependency id="Cucumber.CucumberExpressions" version="17.1.0"/>
<dependency id="Microsoft.Extensions.DependencyModel" version="8.0.2" />
<dependency id="Microsoft.Extensions.Configuration" version="8.0.0" />
<dependency id="Microsoft.Extensions.Configuration.Binder" version="8.0.2" />
<dependency id="Microsoft.Extensions.Configuration.EnvironmentVariables" version="8.0.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="8.0.1" />
<dependency id="System.Runtime.Loader" version="4.3.0" />
<dependency id="SpecFlow.Internal.Json" version="1.0.8" />
</group>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Reqnroll.Configuration.JsonConfig;
using Reqnroll.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;
using System.IO;
using System.Reflection;

namespace Reqnroll.RuntimeTests.Configuration
{
public class MSEConfigurationLoaderTests
{
private string configFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Configuration\\reqnroll_config_test.json");

[Fact]
public void Can_Load_From_JsonFile()
{
var runtimeConfig = new MSE_RuntimeConfigurationLoader().LoadConfiguration(ConfigurationLoader.GetDefault(), configFilePath);
runtimeConfig.FeatureLanguage.TwoLetterISOLanguageName.Should().Be("hu");
}
[Fact]
public void Can_Load_Override_From_Environment()
{
//string config = @"{
// ""language"": { ""feature"": ""de"" }
// }";
Environment.SetEnvironmentVariable("REQNROLL__language__feature", "de");
var runtimeConfig = new MSE_RuntimeConfigurationLoader().LoadConfiguration(ConfigurationLoader.GetDefault(), configFilePath);

runtimeConfig.FeatureLanguage.TwoLetterISOLanguageName.Should().Be("de");
Environment.SetEnvironmentVariable("REQNROLL__language__feature", null);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",
"language": {
"feature": "hu-HU"
},
"bindingAssemblies": [
]
}
10 changes: 10 additions & 0 deletions Tests/Reqnroll.RuntimeTests/Reqnroll.RuntimeTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<NoWarn>1701;1702;CS1998</NoWarn>
</PropertyGroup>

<ItemGroup>
<None Remove="Configuration\reqnroll_config_test.json" />
</ItemGroup>

<ItemGroup>
<Content Include="Configuration\reqnroll_config_test.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<None Include="App.config" />
</ItemGroup>
Expand Down
Loading