Skip to content

Commit

Permalink
Add test to ensure the bug does not come back
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Apr 26, 2024
1 parent b6d2144 commit e84db1f
Showing 1 changed file with 31 additions and 0 deletions.
Expand Up @@ -4,8 +4,10 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.Test;
using Microsoft.Extensions.FileProviders;
using Xunit;

namespace Microsoft.Extensions.Configuration
Expand Down Expand Up @@ -219,5 +221,34 @@ public void ThrowFormatExceptionWhenFileIsEmpty()
var exception = Assert.Throws<FormatException>(() => LoadProvider(@""));
Assert.Contains("Could not parse the JSON file.", exception.Message);
}

[Fact]
public void AddJsonFile_FileProvider_Is_Not_Disposed_When_SourcesGetReloaded()
{
string filePath = Path.Combine(Path.GetTempPath(), $"{nameof(AddJsonFile_FileProvider_Is_Not_Disposed_When_SourcesGetReloaded)}.json");
File.WriteAllText(filePath, @"{ ""some"": ""value"" }");

IConfigurationBuilder builder = new ConfigurationManager();

builder.AddJsonFile(filePath, optional: false);

FileConfigurationSource fileConfigurationSource = (FileConfigurationSource)builder.Sources.Last();
PhysicalFileProvider fileProvider = (PhysicalFileProvider)fileConfigurationSource.FileProvider;

Assert.False(GetIsDisposed(fileProvider));

builder.Properties.Add("simplest", "repro");

Assert.False(GetIsDisposed(fileProvider));

fileProvider.Dispose();
Assert.True(GetIsDisposed(fileProvider));
}

private static bool GetIsDisposed(PhysicalFileProvider fileProvider)
{
System.Reflection.FieldInfo isDisposedField = typeof(PhysicalFileProvider).GetField("_disposed", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
return (bool)isDisposedField.GetValue(fileProvider);
}
}
}

0 comments on commit e84db1f

Please sign in to comment.