8000 Fixed issue #970 by ThomasBarnekow · Pull Request #971 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

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 1 commit into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public string SandboxAssumeUnchangedTestRepo()
{
return Sandbox(AssumeUnchangedRepoWorkingDirPath);
}

public string SandboxSubmoduleSmallTestRepo()
{
var submoduleTarget = Path.Combine(ResourcesDirectory.FullName, "submodule_target_wd");
Expand Down
61 changes: 42 additions & 19 deletions LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;

namespace LibGit2Sharp.Tests.TestHelpers
{
Expand Down Expand Up @@ -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) };
Copy link
Contributor Author

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.

Copy link
Member

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?


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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That return statement was missing in your proposal. However, we don't want to continue after a successful attempt.

Copy link
Member

Choose a reason for hiding this comment

The 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));
}
}
}
}
Expand Down
0