Skip to content
Merged
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 @@ -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}");
}
}

/// <summary>
/// Watches the Created WatcherChangeType and unblocks the returned AutoResetEvent when a
/// Created event is thrown by the watcher.
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -236,7 +231,7 @@ public static void ExecuteWithRetry(Action action, int maxAttempts = DefaultAtte
/// <param name="expectedPath">Optional. Adds path verification to all expected events.</param>
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)
Expand Down