Skip to content

Commit

Permalink
Ensure all expected paths are logged on test failure (#65666)
Browse files Browse the repository at this point in the history
* move duplicated logic to a method

* ensure all paths are logged, so we can diagnose #65601

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
adamsitnik and stephentoub committed Feb 22, 2022
1 parent eeff298 commit 4fd5132
Showing 1 changed file with 19 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Xunit;
using Xunit.Sdk;
using Xunit.Abstractions;
using System.Linq;

namespace System.IO.Tests
{
Expand Down Expand Up @@ -39,17 +40,26 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc
FileSystemEventHandler changeHandler = (o, e) =>
{
Assert.Equal(WatcherChangeTypes.Changed, e.ChangeType);
if (expectedPaths != null)
{
Assert.Contains(Path.GetFullPath(e.FullPath), expectedPaths);
}
VerifyExpectedPaths(expectedPaths, e);
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 @@ -67,18 +77,7 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc
}
Assert.Equal(WatcherChangeTypes.Created, e.ChangeType);
if (expectedPaths != null)
{
try
{
Assert.Contains(Path.GetFullPath(e.FullPath), expectedPaths);
}
catch (Exception ex)
{
_output?.WriteLine(ex.ToString());
throw;
}
}
VerifyExpectedPaths(expectedPaths, e);
eventOccurred.Set();
};
Expand All @@ -102,18 +101,8 @@ public static (AutoResetEvent EventOccured, FileSystemEventHandler Handler) Watc
Assert.Equal(WatcherChangeTypes.Deleted, e.ChangeType);
}
if (expectedPaths != null)
{
try
{
Assert.Contains(Path.GetFullPath(e.FullPath), expectedPaths);
}
catch (Exception ex)
{
_output?.WriteLine(ex.ToString());
throw;
}
}
VerifyExpectedPaths(expectedPaths, e);
eventOccurred.Set();
};

Expand All @@ -137,18 +126,8 @@ public static (AutoResetEvent EventOccured, RenamedEventHandler Handler) WatchRe
Assert.Equal(WatcherChangeTypes.Renamed, e.ChangeType);
}
if (expectedPaths != null)
{
try
{
Assert.Contains(Path.GetFullPath(e.FullPath), expectedPaths);
}
catch (Exception ex)
{
_output?.WriteLine(ex.ToString());
throw;
}
}
VerifyExpectedPaths(expectedPaths, e);
eventOccurred.Set();
};

Expand Down

0 comments on commit 4fd5132

Please sign in to comment.