Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 20295a2

Browse files
committed
Swallow IO exceptions
If deleting a file ends up with a `FileNotFoundException` or deleting a directory ends up with a `DirectoryNotFoundException`, there's no reason for the operation to fail; the file or directory is gone and we shouldn't care why we can't delete it.
1 parent 8b0a1da commit 20295a2

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
151151
}
152152
finally
153153
{
154-
Directory.Delete(tempDir, true);
154+
DeleteHelper.DeleteDirectory(tempDir, true);
155155
if (expectedDynamicRepoLocation != null)
156156
{
157-
Directory.Delete(expectedDynamicRepoLocation, true);
157+
DeleteHelper.DeleteDirectory(expectedDynamicRepoLocation, true);
158158
}
159159

160160
if (expectedDynamicRepoLocation != null)

src/GitTools.Core/GitTools.Core.Shared/IO/Helpers/DeleteHelper.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,41 @@ public static void DeleteGitRepository(string directory)
1111
return;
1212
}
1313

14-
foreach (var fileName in Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories))
14+
try
1515
{
16-
var fileInfo = new FileInfo(fileName)
16+
foreach (var fileName in Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories))
1717
{
18-
IsReadOnly = false
19-
};
18+
var fileInfo = new FileInfo(fileName)
19+
{
20+
IsReadOnly = false
21+
};
2022

21-
fileInfo.Delete();
23+
try
24+
{
25+
fileInfo.Delete();
26+
}
27+
catch (FileNotFoundException)
28+
{
29+
}
30+
}
31+
32+
Directory.Delete(directory, true);
33+
}
34+
catch (DirectoryNotFoundException)
35+
{
2236
}
37+
}
38+
2339

24-
Directory.Delete(directory, true);
40+
public static void DeleteDirectory(string directory, bool recursive)
41+
{
42+
try
43+
{
44+
Directory.Delete(directory, recursive);
45+
}
46+
catch (DirectoryNotFoundException)
47+
{
48+
}
2549
}
2650
}
2751
}

0 commit comments

Comments
 (0)