Skip to content

Commit 1a2528f

Browse files
Refactor DirectoryHelper
Rename TryDeleteDirectory(string, int, int, int) to DeleteDirectory(...) because this method can still throw exceptions and is not fully in line with the try-parse pattern. Introduce TryDeleteDirectory(string) : bool as a try-parse pattern-compliant option to delete directories recursively without throwing exceptions. Remove DeleteSubdirectories(string) method because it is not used anywhere. Use 'var' keyword consistently across all methods. Related to #980
1 parent 61adafd commit 1a2528f

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public static class DirectoryHelper
1515
{ "gitmodules", ".gitmodules" },
1616
};
1717

18+
private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };
19+
1820
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
1921
{
2022
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779
@@ -34,15 +36,6 @@ private static string Rename(string name)
3436
return toRename.ContainsKey(name) ? toRename[name] : name;
3537
}
3638

37-
public static void DeleteSubdirectories(string parentPath)
38-
{
39-
string[] dirs = Directory.GetDirectories(parentPath);
40-
foreach (string dir in dirs)
41-
{
42-
DeleteDirectory(dir);
43-
}
44-
}
45-
4639
public static void DeleteDirectory(string directoryPath)
4740
{
4841
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
@@ -53,28 +46,26 @@ public static void DeleteDirectory(string directoryPath)
5346
return;
5447
}
5548
NormalizeAttributes(directoryPath);
56-
TryDeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
49+
DeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
5750
}
5851

5952
private static void NormalizeAttributes(string directoryPath)
6053
{
61-
string[] files = Directory.GetFiles(directoryPath);
62-
string[] dirs = Directory.GetDirectories(directoryPath);
54+
string[] filePaths = Directory.GetFiles(directoryPath);
55+
string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);
6356

64-
foreach (string file in files)
57+
foreach (string filePath in filePaths)
6558
{
66-
File.SetAttributes(file, FileAttributes.Normal);
59+
File.SetAttributes(filePath, FileAttributes.Normal);
6760
}
68-
foreach (string dir in dirs)
61+
foreach (string subdirectoryPath in subdirectoryPaths)
6962
{
70-
NormalizeAttributes(dir);
63+
NormalizeAttributes(subdirectoryPath);
7164
}
7265
File.SetAttributes(directoryPath, FileAttributes.Normal);
7366
}
7467

75-
private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };
76-
77-
private static void TryDeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor)
68+
private static void DeleteDirectory(string directoryPath, int maxAttempts, int initialTimeout, int timeoutFactor)
7869
{
7970
for (int attempt = 1; attempt <= maxAttempts; attempt++)
8071
{

0 commit comments

Comments
 (0)