-
Notifications
You must be signed in to change notification settings - Fork 899
Fixed issue #970 #971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fixed issue #970 #971
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
|
||
namespace LibGit2Sharp.Tests.TestHelpers | ||
{ | ||
|
@@ -47,40 +49,61 @@ public static void DeleteDirectory(string directoryPath) | |
|
||
if (!Directory.Exists(directoryPath)) | ||
{ | ||
Trace.WriteLine( | ||
string.Format("Directory '{0}' is missing and can't be removed.", | ||
directoryPath)); | ||
|
||
Trace.WriteLine(string.Format("Directory '{0}' is missing and can't be removed.", directoryPath)); | ||
return; | ||
} | ||
NormalizeAttributes(directoryPath); | ||
TryDeleteDirectory(directoryPath, 5, 16, 2); | ||
} | ||
|
||
private static void NormalizeAttributes(string directoryPath) | ||
{ | ||
string[] files = Directory.GetFiles(directoryPath); | ||
string[] dirs = Directory.GetDirectories(directoryPath); | ||
|
||
foreach (string file in files) | ||
{ | ||
File.SetAttributes(file, FileAttributes.Normal); | ||
File.Delete(file); | ||
} | ||
|
||
foreach (string dir in dirs) | ||
{ | ||
DeleteDirectory(dir); | ||
NormalizeAttributes(dir); | ||
} | ||
|
||
File.SetAttributes(directoryPath, FileAttributes.Normal); | ||
try | ||
{ | ||
Directory.Delete(directoryPath, false); | ||
} | ||
catch (IOException) | ||
} | ||
|
||
private static Type[] whitelist = new[] { typeof(DirectoryNotFoundException), typeof(IOException), typeof(UnauthorizedAccessException) }; | ||
|
||
private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor) | ||
{ | ||
for (int attempt = 1; attempt <= maxAttempts; attempt++) | ||
{ | ||
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" + | ||
"{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}", | ||
Environment.NewLine, Path.GetFullPath(directoryPath))); | ||
try | ||
{ | ||
Directory.Delete(directoryPath, true); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed! Good catch! |
||
} | ||
catch (Exception ex) | ||
{ | ||
if (!whitelist.Contains(ex.GetType())) | ||
{ | ||
throw; | ||
} | ||
|
||
if (attempt < maxAttempts) | ||
{ | ||
Thread.Sleep(initialTimeout * (int)Math.Pow(timeoutFactor, attempt - 1)); | ||
continue; | ||
} | ||
|
||
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted due to a {2}: {3}" + | ||
"{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)); | ||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whitelist must include a
DirectoryNotFoundException
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 So that strange exception was thrown again in your environment and eventually magically resolved in a later attempt?