Skip to content

Commit 9b9d891

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 40de694 commit 9b9d891

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs

Lines changed: 18 additions & 14 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,12 +36,16 @@ private static string Rename(string name)
3436
return toRename.ContainsKey(name) ? toRename[name] : name;
3537
}
3638

37-
public static void DeleteSubdirectories(string parentPath)
39+
public static bool TryDeleteDirectory(string directoryPath)
3840
{
39-
string[] dirs = Directory.GetDirectories(parentPath);
40-
foreach (string dir in dirs)
41+
try
42+
{
43+
DeleteDirectory(directoryPath);
44+
return true;
45+
}
46+
catch (Exception)
4147
{
42-
DeleteDirectory(dir);
48+
return false;
4349
}
4450
}
4551

@@ -53,28 +59,26 @@ public static void DeleteDirectory(string directoryPath)
5359
return;
5460
}
5561
NormalizeAttributes(directoryPath);
56-
TryDeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
62+
DeleteDirectory(directoryPath, maxAttempts: 5, initialTimeout: 16, timeoutFactor: 2);
5763
}
5864

5965
private static void NormalizeAttributes(string directoryPath)
6066
{
61-
string[] files = Directory.GetFiles(directoryPath);
62-
string[] dirs = Directory.GetDirectories(directoryPath);
67+
string[] filePaths = Directory.GetFiles(directoryPath);
68+
string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);
6369

64-
foreach (string file in files)
70+
foreach (string filePath in filePaths)
6571
{
66-
File.SetAttributes(file, FileAttributes.Normal);
72+
File.SetAttributes(filePath, FileAttributes.Normal);
6773
}
68-
foreach (string dir in dirs)
74+
foreach (string subdirectoryPath in subdirectoryPaths)
6975
{
70-
NormalizeAttributes(dir);
76+
NormalizeAttributes(subdirectoryPath);
7177
}
7278
File.SetAttributes(directoryPath, FileAttributes.Normal);
7379
}
7480

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

0 commit comments

Comments
 (0)