diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs index 290d33aaa21294..de58daa3906d4e 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs @@ -160,13 +160,21 @@ internal PhysicalFilesWatcher CreateFileWatcher() { string root = PathUtils.EnsureTrailingSlash(Path.GetFullPath(Root)); - // When both UsePollingFileWatcher & UseActivePolling are set, we won't use a FileSystemWatcher. - FileSystemWatcher watcher = UsePollingFileWatcher && UseActivePolling ? null : + FileSystemWatcher watcher; #if NETCOREAPP - OperatingSystem.IsBrowser() ? throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher))) : new FileSystemWatcher(root); -#else - new FileSystemWatcher(root); + // For browser we will proactively fallback to polling since FileSystemWatcher is not supported. + if (OperatingSystem.IsBrowser()) + { + UsePollingFileWatcher = true; + UseActivePolling = true; + watcher = null; + } + else #endif + { + // When UsePollingFileWatcher & UseActivePolling are set, we won't use a FileSystemWatcher. + watcher = UsePollingFileWatcher && UseActivePolling ? null : new FileSystemWatcher(root); + } return new PhysicalFilesWatcher(root, watcher, UsePollingFileWatcher, _filters) { diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx index 1eb0026a4f2a76..61a2f55a718489 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Resources/Strings.resx @@ -130,6 +130,6 @@ Unexpected type of FileSystemInfo - The type {0} is not supported on this platform, use polling instead. + The type '{0}' is not supported on this platform, use polling instead. - \ No newline at end of file + diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/AssemblyInfo.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/AssemblyInfo.cs index b95172c7efbafa..9e6b00f9cf467a 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/AssemblyInfo.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/AssemblyInfo.cs @@ -2,6 +2,4 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using Xunit; - -[assembly: SkipOnPlatform(TestPlatforms.Browser, "Microsoft.Extensions.FileProviders.Physical is not supported on Browser")] +using Xunit; diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Microsoft.Extensions.FileProviders.Physical.Tests.csproj b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Microsoft.Extensions.FileProviders.Physical.Tests.csproj index 6b4ab37b34045f..515300a27aec6c 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Microsoft.Extensions.FileProviders.Physical.Tests.csproj +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Microsoft.Extensions.FileProviders.Physical.Tests.csproj @@ -1,16 +1,16 @@ - + Microsoft.Extensions.FileProviders.Physical $(NetCoreAppCurrent);net461 true - true false + diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs index 96bf27b7b5acab..80e94fc31f910c 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFileProviderTests.cs @@ -325,6 +325,7 @@ public void GetFileInfoReturnsFileInfoWhenExclusionDisabled() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser always uses Active Polling which doesn't return the same instance between multiple calls to Watch(string)")] public void TokenIsSameForSamePath() { using (var root = new DisposableFileSystem()) @@ -348,6 +349,7 @@ public void TokenIsSameForSamePath() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokensFiredOnFileChange() { using (var root = new DisposableFileSystem()) @@ -378,6 +380,7 @@ public async Task TokensFiredOnFileChange() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenCallbackInvokedOnFileChange() { using (var root = new DisposableFileSystem()) @@ -414,6 +417,7 @@ public async Task TokenCallbackInvokedOnFileChange() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task WatcherWithPolling_ReturnsTrueForFileChangedWhenFileSystemWatcherDoesNotRaiseEvents() { using (var root = new DisposableFileSystem()) @@ -444,6 +448,7 @@ public async Task WatcherWithPolling_ReturnsTrueForFileChangedWhenFileSystemWatc [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task WatcherWithPolling_ReturnsTrueForFileRemovedWhenFileSystemWatcherDoesNotRaiseEvents() { using (var root = new DisposableFileSystem()) @@ -476,6 +481,7 @@ public async Task WatcherWithPolling_ReturnsTrueForFileRemovedWhenFileSystemWatc [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokensFiredOnFileDeleted() { using (var root = new DisposableFileSystem()) @@ -787,6 +793,7 @@ public void GetDirectoryContentsReturnsFilesWhenExclusionDisabled() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task FileChangeTokenNotNotifiedAfterExpiry() { using (var root = new DisposableFileSystem()) @@ -819,6 +826,7 @@ public async Task FileChangeTokenNotNotifiedAfterExpiry() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser always uses Active Polling which doesn't return the same instance between multiple calls to Watch(string)")] public void TokenIsSameForSamePathCaseInsensitive() { using (var root = new DisposableFileSystem()) @@ -835,6 +843,7 @@ public void TokenIsSameForSamePathCaseInsensitive() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task CorrectTokensFiredForMultipleFiles() { using (var root = new DisposableFileSystem()) @@ -868,6 +877,7 @@ public async Task CorrectTokensFiredForMultipleFiles() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenNotAffectedByExceptions() { using (var root = new DisposableFileSystem()) @@ -975,6 +985,7 @@ public void NoopChangeTokenForAbsolutePathFilters() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenFiredOnCreation() { using (var root = new DisposableFileSystem()) @@ -1000,6 +1011,7 @@ public async Task TokenFiredOnCreation() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenFiredOnDeletion() { using (var root = new DisposableFileSystem()) @@ -1025,6 +1037,7 @@ public async Task TokenFiredOnDeletion() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenFiredForFilesUnderPathEndingWithSlash() { using (var root = new DisposableFileSystem()) @@ -1079,6 +1092,7 @@ public async Task TokenFiredForRelativePathStartingWithSlash_Windows(string slas [InlineData("///")] // Testing Unix specific behaviour on leading slashes. [PlatformSpecific(TestPlatforms.AnyUnix)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenFiredForRelativePathStartingWithSlash_Unix(string slashes) { await TokenFiredForRelativePathStartingWithSlash(slashes); @@ -1121,6 +1135,7 @@ public async Task TokenNotFiredForInvalidPathStartingWithSlash_Windows(string sl [InlineData("/\0/")] // Testing Unix specific behaviour on leading slashes. [PlatformSpecific(TestPlatforms.AnyUnix)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenNotFiredForInvalidPathStartingWithSlash_Unix(string slashes) { await TokenNotFiredForInvalidPathStartingWithSlash(slashes); @@ -1152,6 +1167,7 @@ private async Task TokenNotFiredForInvalidPathStartingWithSlash(string slashes) [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenFiredForGlobbingPatternsPointingToSubDirectory() { using (var root = new DisposableFileSystem()) @@ -1185,6 +1201,7 @@ public async Task TokenFiredForGlobbingPatternsPointingToSubDirectory() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "Browser always uses Active Polling which doesn't return the same instance between multiple calls to Watch(string)")] public void TokensWithForwardAndBackwardSlashesAreSame() { using (var root = new DisposableFileSystem()) @@ -1201,6 +1218,7 @@ public void TokensWithForwardAndBackwardSlashesAreSame() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokensFiredForOldAndNewNamesOnRename() { using (var root = new DisposableFileSystem()) @@ -1230,6 +1248,7 @@ public async Task TokensFiredForOldAndNewNamesOnRename() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokensFiredForNewDirectoryContentsOnRename() { var tcsShouldNotFire = new TaskCompletionSource(); @@ -1303,6 +1322,7 @@ void Fail(object state) [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokenNotFiredForFileNameStartingWithPeriod() { using (var root = new DisposableFileSystem()) @@ -1370,6 +1390,7 @@ public async Task TokensNotFiredForHiddenAndSystemFiles() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task TokensFiredForAllEntriesOnError() { using (var root = new DisposableFileSystem()) @@ -1398,6 +1419,7 @@ public async Task TokensFiredForAllEntriesOnError() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task WildCardToken_RaisesEventsForNewFilesAdded() { // Arrange @@ -1424,6 +1446,7 @@ public async Task WildCardToken_RaisesEventsForNewFilesAdded() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task WildCardToken_RaisesEventsWhenFileSystemWatcherDoesNotFire() { // Arrange @@ -1473,6 +1496,7 @@ public void UsePollingFileWatcher_FileWatcherNull_SetsSuccessfully() } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void UsePollingFileWatcher_FileWatcherNotNull_SetterThrows() { // Arrange @@ -1493,6 +1517,7 @@ public void UsePollingFileWatcher_FileWatcherNotNull_SetterThrows() } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void UsePollingFileWatcher_FileWatcherNotNull_ReturnsFalse() { // Arrange @@ -1589,6 +1614,7 @@ public void CreateFileWatcher_CreatesWatcherWithPollingAndActiveFlags() [InlineData(false)] [InlineData(true)] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task CanDeleteWatchedDirectory(bool useActivePolling) { using (var root = new DisposableFileSystem()) diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFilesWatcherTests.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFilesWatcherTests.cs index 77771f5390adc5..2e48a7cb0c0f70 100644 --- a/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFilesWatcherTests.cs +++ b/src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/PhysicalFilesWatcherTests.cs @@ -18,6 +18,7 @@ public class PhysicalFilesWatcherTests private const int WaitTimeForTokenToFire = 500; [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void CreateFileChangeToken_DoesNotAllowPathsAboveRoot() { using (var root = new DisposableFileSystem()) @@ -37,6 +38,7 @@ public void CreateFileChangeToken_DoesNotAllowPathsAboveRoot() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public async Task HandlesOnRenamedEventsThatMatchRootPath() { using (var root = new DisposableFileSystem()) @@ -129,6 +131,7 @@ public void RaiseChangeEvents_CancelsAndRemovesMultipleChangedTokens() } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void GetOrAddFilePathChangeToken_AddsPollingChangeTokenWithCancellationToken_WhenActiveCallbackIsTrue() { using (var root = new DisposableFileSystem()) @@ -155,6 +158,7 @@ public void GetOrAddFilePathChangeToken_AddsPollingChangeTokenWithCancellationTo } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void GetOrAddFilePathChangeToken_AddsPollingChangeTokenWhenPollingIsEnabled() { using (var root = new DisposableFileSystem()) @@ -179,6 +183,7 @@ public void GetOrAddFilePathChangeToken_AddsPollingChangeTokenWhenPollingIsEnabl } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void GetOrAddFilePathChangeToken_DoesNotAddsPollingChangeTokenWhenCallbackIsDisabled() { using (var root = new DisposableFileSystem()) @@ -193,6 +198,7 @@ public void GetOrAddFilePathChangeToken_DoesNotAddsPollingChangeTokenWhenCallbac } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void GetOrAddWildcardChangeToken_AddsPollingChangeTokenWithCancellationToken_WhenActiveCallbackIsTrue() { using (var root = new DisposableFileSystem())