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

Fixing PhysicalFilesWatcher path maintenance (Issue #71386) #72041

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void ThrowsOnFileNotFoundWhenNotIgnored()

Assert.Throws<FileNotFoundException>(() => configurationBuilder.Build());
}

[Fact]
public void CanHandleExceptionIfFileNotFound()
{
Expand Down Expand Up @@ -689,7 +689,7 @@ await WaitForChange(
Assert.Equal("IniValue1", config["Key"]);
Assert.True(token.HasChanged);
}

[Theory]
[ActiveIssue("File watching is flaky (particularly on non windows. https://github.com/dotnet/runtime/issues/33992")]
[InlineData(false)]
Expand Down Expand Up @@ -987,6 +987,41 @@ void ReloadLoop()
}
}

[Fact]
public async Task ReloadingFileFiresOnChangeEvent()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be a Theory and you should put ./reload.json and reload.json as InlineData.

{
_fileSystem.WriteFile("reload.json", @"{""JsonKey1"": ""JsonValue1""}");
_fileSystem.WriteFile("reload2.json", @"{""JsonKey2"": ""JsonValue2""}");

var config = CreateBuilder()
.AddJsonFile("./reload.json", optional: false, reloadOnChange: true)
.Build();

var config2 = CreateBuilder()
.AddJsonFile("reload2.json", optional: false, reloadOnChange: true)
.Build();

Assert.Equal("JsonValue1", config["JsonKey1"]);
Assert.Equal("JsonValue2", config2["JsonKey2"]);

var token = config.GetReloadToken();
var token2 = config2.GetReloadToken();

// Update files
_fileSystem.WriteFile("reload.json", @"{""JsonKey1"": ""JsonValue3""}");
_fileSystem.WriteFile("reload2.json", @"{""JsonKey2"": ""JsonValue4""}");

await WaitForChange(
() => config["JsonKey1"] == "JsonValue3"
&& config2["JsonKey2"] == "JsonValue4",
"Reload failed after touching files.");

Assert.Equal("JsonValue3", config["JsonKey1"]);
Assert.Equal("JsonValue4", config2["JsonKey2"]);
Assert.True(token.HasChanged);
Assert.True(token2.HasChanged);
}

public void Dispose()
{
_fileProvider.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ private IChangeToken GetOrAddChangeToken(string pattern)

internal IChangeToken GetOrAddFilePathChangeToken(string filePath)
{
#if NETCOREAPP
var absolutePath = Path.GetFullPath(filePath, _root);
filePath = absolutePath.Substring(_root.Length);
#endif
Comment on lines +178 to +181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eerhardt suggestion, this should work for .NET Framework too:

Suggested change
#if NETCOREAPP
var absolutePath = Path.GetFullPath(filePath, _root);
filePath = absolutePath.Substring(_root.Length);
#endif
#if NETCOREAPP
string absolutePath = Path.GetFullPath(filePath, _root);
#else
string absolutePath = Path.GetFullPath(Path.Combine(_root, filePath));
#endif
filePath = absolutePath.Substring(_root.Length);

if (!_filePathTokenLookup.TryGetValue(filePath, out ChangeTokenInfo tokenInfo))
{
var cancellationTokenSource = new CancellationTokenSource();
Expand Down