Skip to content

Commit

Permalink
Fixes error in file deletion after test finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine Zhou committed Jan 23, 2025
1 parent 0179067 commit 787295c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions Tst/UnitTests/PCheckerLogGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private void AssertLog(string generatedDir, string expectedDir)
{
Assert.Fail($"Test Failed \nMissing expected file in {generatedDir}: {file}");
}
Console.WriteLine("Test Succeeded");
}

private static bool IsJsonContentIncluded(string generatedFilePath, string expectedFilePath)
Expand Down
35 changes: 32 additions & 3 deletions Tst/UnitTests/TestAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using NUnit.Framework;
using UnitTests.Core;

Expand All @@ -26,14 +27,42 @@ public static void AssertTestCase(CompilerTestCase testCase)
public static void SafeDeleteDirectory(DirectoryInfo toDelete)
{
var safeBase = new DirectoryInfo(Constants.SolutionDirectory);

for (var scratch = toDelete; scratch.Parent != null; scratch = scratch.Parent)
{
if (string.Compare(scratch.FullName, safeBase.FullName, StringComparison.InvariantCultureIgnoreCase) ==
0)
if (string.Compare(scratch.FullName, safeBase.FullName, StringComparison.InvariantCultureIgnoreCase) == 0)
{
if (toDelete.Exists)
{
toDelete.Delete(true);
const int maxRetries = 5;
const int delay = 200;

for (int i = 0; i < maxRetries; i++)
{
try
{
toDelete.Delete(true);
return;
}
catch (IOException)
{
if (i == maxRetries - 1)
{
throw;
}

Thread.Sleep(delay);
}
catch (UnauthorizedAccessException)
{
if (i == maxRetries - 1)
{
throw;
}

Thread.Sleep(delay);
}
}
}

return;
Expand Down

0 comments on commit 787295c

Please sign in to comment.