From 94f8f0c8345d700a5864b0206600ed86c190dd5e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20Cant=C3=BA?= Date: Thu, 5 Aug 2021 08:42:45 -0700 Subject: [PATCH] Do not prevent Extensions.FileProviders tests from running in browser (#56257) * Do not prevent Extensions.FileProviders tests from running in CI * Remove SkipOnPlatform * Do not throw PNSE for PhysicalFileProvider in browser, fallback to use polling instead * Use SkipOnPlatform only on tests that use FileSystemWatcher * Set UsePollingFileWatcher and UseActivePolling for browser to avoid ArgumentNullException in PhysicalFilesWatcher * Fix Moq CI failures * Use Assert.Same instead of Assert.Equals * Disable tests for IChangeToken equality between multiple Watch() calls in Browser --- .../src/PhysicalFileProvider.cs | 18 ++++++++++----- .../src/Resources/Strings.resx | 4 ++-- .../tests/AssemblyInfo.cs | 4 +--- ....Extensions.FileProviders.Physical.Tests.csproj | 4 ++-- .../tests/PhysicalFileProviderTests.cs | 26 ++++++++++++++++++++++ .../tests/PhysicalFilesWatcherTests.cs | 6 +++++ 6 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs b/src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs index 290d33a..de58daa 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 @@ namespace Microsoft.Extensions.FileProviders { 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 1eb0026..61a2f55 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 b95172c..9e6b00f 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 6b4ab37..515300a 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 96bf27b..80e94fc 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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders [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 @@ namespace Microsoft.Extensions.FileProviders } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void UsePollingFileWatcher_FileWatcherNotNull_SetterThrows() { // Arrange @@ -1493,6 +1517,7 @@ namespace Microsoft.Extensions.FileProviders } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void UsePollingFileWatcher_FileWatcherNotNull_ReturnsFalse() { // Arrange @@ -1589,6 +1614,7 @@ namespace Microsoft.Extensions.FileProviders [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 77771f5..2e48a7c 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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests 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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests [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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests } [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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests } [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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests } [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 @@ namespace Microsoft.Extensions.FileProviders.Physical.Tests } [Fact] + [SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")] public void GetOrAddWildcardChangeToken_AddsPollingChangeTokenWithCancellationToken_WhenActiveCallbackIsTrue() { using (var root = new DisposableFileSystem()) -- 2.7.4