Skip to content

Commit e1228da

Browse files
committed
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
1 parent 8aa53b6 commit e1228da

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void DeleteDirectory(string directoryPath)
5353
return;
5454
}
5555
NormalizeAttributes(directoryPath);
56-
TryDeleteDirectory(directoryPath, 5, 16, 2);
56+
TryDeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
5757
}
5858

5959
private static void NormalizeAttributes(string directoryPath)
@@ -72,7 +72,7 @@ private static void NormalizeAttributes(string directoryPath)
7272
File.SetAttributes(directoryPath, FileAttributes.Normal);
7373
}
7474

75-
private static Type[] whitelist = new[] { typeof(DirectoryNotFoundException), typeof(IOException), typeof(UnauthorizedAccessException) };
75+
private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };
7676

7777
private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor)
7878
{
@@ -85,7 +85,9 @@ private static void TryDeleteDirectory(string directoryPath, int maxAttempts, in
8585
}
8686
catch (Exception ex)
8787
{
88-
if (!whitelist.Contains(ex.GetType()))
88+
var caughtExceptionType = ex.GetType();
89+
90+
if (!whitelist.Any(knownExceptionType => knownExceptionType.IsAssignableFrom(caughtExceptionType)))
8991
{
9092
throw;
9193
}
@@ -96,13 +98,13 @@ private static void TryDeleteDirectory(string directoryPath, int maxAttempts, in
9698
continue;
9799
}
98100

99-
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted due to a {2}: {3}" +
101+
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted ({2} attempts were made) due to a {3}: {4}" +
100102
"{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." +
101103
"{0}Known and common causes include:" +
102104
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +
103105
"{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus)" +
104106
"{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)",
105-
Environment.NewLine, Path.GetFullPath(directoryPath), ex.GetType(), ex.Message));
107+
Environment.NewLine, Path.GetFullPath(directoryPath), maxAttempts, caughtExceptionType, ex.Message));
106108
}
107109
}
108110
}

0 commit comments

Comments
 (0)