diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs index 0d138067a5efe2..21fc7d4ec02b58 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs @@ -40,26 +40,17 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc FileSystemEventHandler changeHandler = (o, e) => { Assert.Equal(WatcherChangeTypes.Changed, e.ChangeType); - VerifyExpectedPaths(expectedPaths, e); - eventOccurred.Set(); + + if (expectedPaths == null || expectedPaths.Contains(e.FullPath)) + { + eventOccurred.Set(); + } }; watcher.Changed += changeHandler; return (eventOccurred, changeHandler); } - private static void VerifyExpectedPaths(string[] expectedPaths, FileSystemEventArgs e) - { - string fullPath = Path.GetFullPath(e.FullPath); - if (expectedPaths is not null && !expectedPaths.Contains(fullPath)) - { - // Assert.Contains does not print a full content of collection which makes it hard to diagnose issues like #65601 - throw new XunitException($"Expected path(s):{Environment.NewLine}" - + string.Join(Environment.NewLine, expectedPaths) - + $"{Environment.NewLine}Actual path: {fullPath}"); - } - } - /// /// Watches the Created WatcherChangeType and unblocks the returned AutoResetEvent when a /// Created event is thrown by the watcher. @@ -77,9 +68,11 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc } Assert.Equal(WatcherChangeTypes.Created, e.ChangeType); - VerifyExpectedPaths(expectedPaths, e); - eventOccurred.Set(); + if (expectedPaths == null || expectedPaths.Contains(e.FullPath)) + { + eventOccurred.Set(); + } }; watcher.Created += handler; @@ -101,9 +94,10 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc Assert.Equal(WatcherChangeTypes.Deleted, e.ChangeType); } - VerifyExpectedPaths(expectedPaths, e); - - eventOccurred.Set(); + if (expectedPaths == null || expectedPaths.Contains(e.FullPath)) + { + eventOccurred.Set(); + } }; watcher.Deleted += handler; @@ -126,9 +120,10 @@ public static (AutoResetEvent EventOccured, RenamedEventHandler Handler) WatchRe Assert.Equal(WatcherChangeTypes.Renamed, e.ChangeType); } - VerifyExpectedPaths(expectedPaths, e); - - eventOccurred.Set(); + if (expectedPaths == null || expectedPaths.Contains(e.FullPath)) + { + eventOccurred.Set(); + } }; watcher.Renamed += handler; @@ -236,7 +231,7 @@ public static void ExecuteWithRetry(Action action, int maxAttempts = DefaultAtte /// Optional. Adds path verification to all expected events. public static void ExpectNoEvent(FileSystemWatcher watcher, WatcherChangeTypes unExpectedEvents, Action action, Action cleanup = null, string expectedPath = null, int timeout = WaitForExpectedEventTimeout) { - bool result = ExecuteAndVerifyEvents(watcher, unExpectedEvents, action, false, new string[] { expectedPath }, timeout); + bool result = ExecuteAndVerifyEvents(watcher, unExpectedEvents, action, false, expectedPath == null ? null : new string[] { expectedPath }, timeout); Assert.False(result, "Expected Event occured"); if (cleanup != null)