From e1228da6145524ac2f33e5fbbe43c75ff5125573 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 23 Feb 2015 22:15:15 +0100 Subject: [PATCH] Minor changes to DirectoryHelper.DeleteDirectory() - Allow derived exceptions from known ones to be whitelisted - Trace the number of failed attempts made to delete the directory - Use named parameters to make the retry parameters less "magic" to the reader --- LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs b/LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs index d63bce0b7..71ce0ede1 100644 --- a/LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs +++ b/LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs @@ -53,7 +53,7 @@ public static void DeleteDirectory(string directoryPath) return; } NormalizeAttributes(directoryPath); - TryDeleteDirectory(directoryPath, 5, 16, 2); + TryDeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2); } private static void NormalizeAttributes(string directoryPath) @@ -72,7 +72,7 @@ private static void NormalizeAttributes(string directoryPath) File.SetAttributes(directoryPath, FileAttributes.Normal); } - private static Type[] whitelist = new[] { typeof(DirectoryNotFoundException), typeof(IOException), typeof(UnauthorizedAccessException) }; + private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) }; private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor) { @@ -85,7 +85,9 @@ private static void TryDeleteDirectory(string directoryPath, int maxAttempts, in } catch (Exception ex) { - if (!whitelist.Contains(ex.GetType())) + var caughtExceptionType = ex.GetType(); + + if (!whitelist.Any(knownExceptionType => knownExceptionType.IsAssignableFrom(caughtExceptionType))) { throw; } @@ -96,13 +98,13 @@ private static void TryDeleteDirectory(string directoryPath, int maxAttempts, in continue; } - Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted due to a {2}: {3}" + + Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted ({2} attempts were made) due to a {3}: {4}" + "{0}Most of the time, this is due to an external process accessing the files in the temporary repositories created during the test runs, and keeping a handle on the directory, thus preventing the deletion of those files." + "{0}Known and common causes include:" + "{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" + "{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus)" + "{0}- TortoiseGit (change the 'Icon Overlays' settings, e.g., adding the bin folder of LibGit2Sharp.Tests to 'Exclude paths' and appending an '*' to exclude all subfolders as well)", - Environment.NewLine, Path.GetFullPath(directoryPath), ex.GetType(), ex.Message)); + Environment.NewLine, Path.GetFullPath(directoryPath), maxAttempts, caughtExceptionType, ex.Message)); } } }