|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Diagnostics;
|
4 | 4 | using System.IO;
|
| 5 | +using System.Linq; |
| 6 | +using System.Threading; |
5 | 7 |
|
6 | 8 | namespace LibGit2Sharp.Tests.TestHelpers
|
7 | 9 | {
|
@@ -47,40 +49,61 @@ public static void DeleteDirectory(string directoryPath)
|
47 | 49 |
|
48 | 50 | if (!Directory.Exists(directoryPath))
|
49 | 51 | {
|
50 |
| - Trace.WriteLine( |
51 |
| - string.Format("Directory '{0}' is missing and can't be removed.", |
52 |
| - directoryPath)); |
53 |
| - |
| 52 | + Trace.WriteLine(string.Format("Directory '{0}' is missing and can't be removed.", directoryPath)); |
54 | 53 | return;
|
55 | 54 | }
|
| 55 | + NormalizeAttributes(directoryPath); |
| 56 | + TryDeleteDirectory(directoryPath, 5, 16, 2); |
| 57 | + } |
56 | 58 |
|
| 59 | + private static void NormalizeAttributes(string directoryPath) |
| 60 | + { |
57 | 61 | string[] files = Directory.GetFiles(directoryPath);
|
58 | 62 | string[] dirs = Directory.GetDirectories(directoryPath);
|
59 | 63 |
|
60 | 64 | foreach (string file in files)
|
61 | 65 | {
|
62 | 66 | File.SetAttributes(file, FileAttributes.Normal);
|
63 |
| - File.Delete(file); |
64 | 67 | }
|
65 |
| - |
66 | 68 | foreach (string dir in dirs)
|
67 | 69 | {
|
68 |
| - DeleteDirectory(dir); |
| 70 | + NormalizeAttributes(dir); |
69 | 71 | }
|
70 |
| - |
71 | 72 | File.SetAttributes(directoryPath, FileAttributes.Normal);
|
72 |
| - try |
73 |
| - { |
74 |
| - Directory.Delete(directoryPath, false); |
75 |
| - } |
76 |
| - catch (IOException) |
| 73 | + } |
| 74 | + |
| 75 | + private static Type[] whitelist = new[] { typeof(DirectoryNotFoundException), typeof(IOException), typeof(UnauthorizedAccessException) }; |
| 76 | + |
| 77 | + private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor) |
| 78 | + { |
| 79 | + for (int attempt = 1; attempt <= maxAttempts; attempt++) |
77 | 80 | {
|
78 |
| - Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" + |
79 |
| - "{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." + |
80 |
| - "{0}Known and common causes include:" + |
81 |
| - "{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" + |
82 |
| - "{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus){0}", |
83 |
| - Environment.NewLine, Path.GetFullPath(directoryPath))); |
| 81 | + try |
| 82 | + { |
| 83 | + Directory.Delete(directoryPath, true); |
| 84 | + return; |
| 85 | + } |
| 86 | + catch (Exception ex) |
| 87 | + { |
| 88 | + if (!whitelist.Contains(ex.GetType())) |
| 89 | + { |
| 90 | + throw; |
| 91 | + } |
| 92 | + |
| 93 | + if (attempt < maxAttempts) |
| 94 | + { |
| 95 | + Thread.Sleep(initialTimeout * (int)Math.Pow(timeoutFactor, attempt - 1)); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted due to a {2}: {3}" + |
| 100 | + "{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." + |
| 101 | + "{0}Known and common causes include:" + |
| 102 | + "{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" + |
| 103 | + "{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus)" + |
| 104 | + "{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)); |
| 106 | + } |
84 | 107 | }
|
85 | 108 | }
|
86 | 109 | }
|
|
0 commit comments