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+
privatestaticreadonlyType[]whitelist={typeof(IOException),typeof(UnauthorizedAccessException)};
19+
1820
publicstaticvoidCopyFilesRecursively(DirectoryInfosource,DirectoryInfotarget)
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
returntoRename.ContainsKey(name)?toRename[name]:name;
3537
}
3638

37-
publicstaticvoidDeleteSubdirectories(stringparentPath)
38-
{
39-
string[]dirs=Directory.GetDirectories(parentPath);
40-
foreach(stringdirindirs)
41-
{
42-
DeleteDirectory(dir);
43-
}
44-
}
45-
4639
publicstaticvoidDeleteDirectory(stringdirectoryPath)
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
privatestaticvoidNormalizeAttributes(stringdirectoryPath)
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(stringfileinfiles)
57+
foreach(stringfilePathinfilePaths)
6558
{
66-
File.SetAttributes(file,FileAttributes.Normal);
59+
File.SetAttributes(filePath,FileAttributes.Normal);
6760
}
68-
foreach(stringdirindirs)
61+
foreach(stringsubdirectoryPathinsubdirectoryPaths)
6962
{
70-
NormalizeAttributes(dir);
63+
NormalizeAttributes(subdirectoryPath);
7164
}
7265
File.SetAttributes(directoryPath,FileAttributes.Normal);
7366
}
7467

75-
privatestaticreadonlyType[]whitelist={typeof(IOException),typeof(UnauthorizedAccessException)};
76-
77-
privatestaticvoidTryDeleteDirectory(stringdirectoryPath,intmaxAttempts,intinitialTimeout,inttimeoutFactor)
68+
privatestaticvoidDeleteDirectory(stringdirectoryPath,intmaxAttempts,intinitialTimeout,inttimeoutFactor)
7869
{
7970
for(intattempt=1;attempt<=maxAttempts;attempt++)
8071
{

0 commit comments

Comments
(0)