8000 Refactor DirectoryHelper by ThomasBarnekow · Pull Request #1022 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

Refactor DirectoryHelper #1022

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
Apr 18, 2015
Merged
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
29 changes: 10 additions & 19 deletions LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static class DirectoryHelper
{ "gitmodules", ".gitmodules" },
};

private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };
Copy link
Member

Choose a reason for hiding this comment

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

Why was this moved? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To have all fields at the top of the class. The other existing field also did not directly precede the (only) method in which it was used. In other words, consistency ...

Copy link
Member

Choose a reason for hiding this comment

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

Fine by me.


public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents CFFE -of-a-directory-in-c/58779#58779
Expand All @@ -34,15 +36,6 @@ private static string Rename(string name)
return toRename.ContainsKey(name) ? toRename[name] : name;
}

public static void DeleteSubdirectories(string parentPath)
{
string[] dirs = Directory.GetDirectories(parentPath);
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
}

public static void DeleteDirectory(string directoryPath)
{
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
Expand All @@ -53,28 +46,26 @@ public static void DeleteDirectory(string directoryPath)
return;
}
NormalizeAttributes(directoryPath);
TryDeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
DeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
}

private static void NormalizeAttributes(string directoryPath)
{
string[] files = Directory.GetFiles(directoryPath);
string[] dirs = Directory.GetDirectories(directoryPath);
string[] filePaths = Directory.GetFiles(directoryPath);
string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);

foreach (string file in files)
foreach (string filePath in filePaths)
{
File.SetAttributes(file, FileAttributes.Normal);
File.SetAttributes(filePath, FileAttributes.Normal);
}
foreach (string dir in dirs)
foreach (string subdirectoryPath in subdirectoryPaths)
{
NormalizeAttributes(dir);
NormalizeAttributes(subdirectoryPath);
}
File.SetAttributes(directoryPath, FileAttributes.Normal);
}

private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };

private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor)
private static void DeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor)
{
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
Expand Down
0