Skip to content

Commit

Permalink
Port config fix to 2.2 (dotnet/extensions#1221)
Browse files Browse the repository at this point in the history
- port of dotnet/extensions#1202
- with PR tweaks for 2.2
  - e.g. adjust Microsoft.Extensions.Configuration.FunctionalTests.csproj to match layout here
- update PatchConfig.props and NuGetPackageVerifier.json\n\nCommit migrated from dotnet/extensions@9ebff1a
  • Loading branch information
HaoK authored and dougbu committed Mar 15, 2019
1 parent d14a080 commit 5691c30
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ private static string TrimNewLine(string value)
/// </summary>
public override void Load()
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

if (Source.FileProvider == null)
{
if (Source.Optional)
{
Data = data;
return;
}
else
Expand All @@ -63,10 +64,12 @@ public override void Load()
{
if (Source.IgnoreCondition == null || !Source.IgnoreCondition(file.Name))
{
Data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
}
}
}

Data = data;
}
}
}
35 changes: 35 additions & 0 deletions src/Configuration/Config.KeyPerFile/test/KeyPerFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Xunit;
Expand Down Expand Up @@ -177,6 +179,39 @@ public void CanUnIgnoreDefaultFiles()
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
Assert.Equal("SecretValue2", config["Secret2"]);
}

[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
var testFileProvider = new TestFileProvider(
new TestFile("Number", "-2"),
new TestFile("Text", "Foo"));

var config = new ConfigurationBuilder()
.AddKeyPerFile(o => o.FileProvider = testFileProvider)
.Build();

MyOptions options = null;

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
_ = Task.Run(() => { while (!cts.IsCancellationRequested) config.Reload(); });

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}
}

Assert.Equal(-2, options.Number);
Assert.Equal("Foo", options.Text);
}

private sealed class MyOptions
{
public int Number { get; set; }
public string Text { get; set; }
}
}

class TestFileProvider : IFileProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
<Reference Include="Microsoft.Extensions.Configuration.KeyPerFile" />
</ItemGroup>

Expand Down

0 comments on commit 5691c30

Please sign in to comment.