Skip to content

Commit

Permalink
Add test configuration source
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed May 12, 2024
1 parent fcf8ebf commit 7bca613
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ public void ConfigurationSourceDevelopmentSecrets_NoSources()
{
var configuration = ConfigurationBuilderBuilder.Create<ConfigurationBuilderBuilderTest>()
.WithEnvironment("Development")
.WithAddPostConfiguration(builder => builder.AddConfigurationSource())
.WithConfigurationSource()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
Expand All @@ -955,4 +955,159 @@ public void ConfigurationSourceDevelopmentSecrets_NoSources()
AssertDevelopment(configuration);
}

[Fact]
public void ConfigurationSourceProduction_configJson()
{
var configuration = ConfigurationBuilderBuilder.Create(
new string[] {
"/ConfigurationSource:0=Other-Config-Source/config.json"
})
.WithConfigurationSource()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
"JsonConfigurationProvider",
"JsonConfigurationProvider",
"ChainedConfigurationProvider",
"EnvironmentVariablesConfigurationProvider",
"CommandLineConfigurationProvider",
}, new[] {
"appsettings.json",
"appsettings.Production.json",
null,
null,
null,
});

Assert.Equal("EnvironmentProductionValue1", configuration["EnvironmentKey"]);
Assert.Equal("Other-Config-Source/config.json", configuration["MainKey"]);
Assert.Equal("config.json", configuration["JsonSection:Other-Config-Source"]);
}

[Fact]
public void ConfigurationSourceProduction_configJsonIni()
{
var configuration = ConfigurationBuilderBuilder.Create(
new string[] {
"/ConfigurationSource:0=Other-Config-Source/config.json",
"/ConfigurationSource:1=Other-Config-Source/config.ini",
})
.WithConfigurationSource()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
"JsonConfigurationProvider",
"JsonConfigurationProvider",
"ChainedConfigurationProvider",
"EnvironmentVariablesConfigurationProvider",
"CommandLineConfigurationProvider",
}, new[] {
"appsettings.json",
"appsettings.Production.json",
null,
null,
null,
});

Assert.Equal("EnvironmentProductionValue1", configuration["EnvironmentKey"]);
Assert.Equal("Other-Config-Source/config.ini", configuration["MainKey"]);
Assert.Equal("config.json", configuration["JsonSection:Other-Config-Source"]);
Assert.Equal("config.ini", configuration["IniSection:Other-Config-Source"]);
}

[Fact]
public void ConfigurationSourceProduction_configIniJson()
{
var configuration = ConfigurationBuilderBuilder.Create(
new string[] {
"/ConfigurationSource:0=Other-Config-Source/config.ini",
"/ConfigurationSource:1=Other-Config-Source/config.json",
})
.WithConfigurationSource()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
"JsonConfigurationProvider",
"JsonConfigurationProvider",
"ChainedConfigurationProvider",
"EnvironmentVariablesConfigurationProvider",
"CommandLineConfigurationProvider",
}, new[] {
"appsettings.json",
"appsettings.Production.json",
null,
null,
null,
});

Assert.Equal("EnvironmentProductionValue1", configuration["EnvironmentKey"]);
Assert.Equal("Other-Config-Source/config.json", configuration["MainKey"]);
Assert.Equal("config.json", configuration["JsonSection:Other-Config-Source"]);
Assert.Equal("config.ini", configuration["IniSection:Other-Config-Source"]);
}

[Fact]
public void ConfigurationSourceProduction_configIniJsonXml()
{
var configuration = ConfigurationBuilderBuilder.Create(
new string[] {
"/ConfigurationSource:0=Other-Config-Source/config.ini",
"/ConfigurationSource:1=Other-Config-Source/config.json",
"/ConfigurationSource:2=Other-Config-Source/config.xml",
})
.WithConfigurationSource()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
"JsonConfigurationProvider",
"JsonConfigurationProvider",
"ChainedConfigurationProvider",
"EnvironmentVariablesConfigurationProvider",
"CommandLineConfigurationProvider",
}, new[] {
"appsettings.json",
"appsettings.Production.json",
null,
null,
null,
});

Assert.Equal("EnvironmentProductionValue1", configuration["EnvironmentKey"]);
Assert.Equal("Other-Config-Source/config.xml", configuration["MainKey"]);
Assert.Equal("config.json", configuration["JsonSection:Other-Config-Source"]);
Assert.Equal("config.ini", configuration["IniSection:Other-Config-Source"]);
Assert.Equal("config.xml", configuration["XmlSection:Other-Config-Source"]);
}

[Fact]
public void ConfigurationSource_Transform()
{
var configuration = ConfigurationBuilderBuilder.Create(
new string[] {
"/configSrc:0=appsettings.Transform.json",
})
.WithConfigurationSource("configSrc")
.WithTransformConfiguration()
.BuildConfigurationRoot();

AssertProviders(configuration, new[] {
"JsonConfigurationProvider",
"JsonConfigurationProvider",
"ChainedConfigurationProvider",
"EnvironmentVariablesConfigurationProvider",
"CommandLineConfigurationProvider",
"TransformConfigurationProvider",
}, new[] {
"appsettings.json",
"appsettings.Production.json",
null,
null,
null,
null,
});

AssertProduction(configuration);
AssertTransform(configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

<ItemGroup>
<Content Include="appsettings.*" CopyToOutputDirectory="PreserveNewest" />
<Content Include="New-Location\*.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="New-Location/*.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Other-Config-Source/**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MainKey = "Other-Config-Source/config.ini"

[IniSection]
Other-Config-Source = config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"MainKey": "Other-Config-Source/config.json",
"JsonSection": {
"Other-Config-Source": "config.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<MainKey>Other-Config-Source/config.xml</MainKey>
<XmlSection>
<Other-Config-Source>config.xml</Other-Config-Source>
</XmlSection>
</configuration>
2 changes: 1 addition & 1 deletion test/NetLah.Extensions.Configuration.Test/appsettings.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
IniKey1="ini Value2"

[IniSection3:Section4]
Key5 = Value6
Key5 = Value6
2 changes: 1 addition & 1 deletion test/NetLah.Extensions.Configuration.Test/appsettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<XmlSection3>
<Key4>Value5</Key4>
</XmlSection3>
</configuration>
</configuration>

0 comments on commit 7bca613

Please sign in to comment.