Skip to content

Commit 826dfeb

Browse files
committed
Clean up @test_*_tmp artifacts after each test run
1 parent a82d993 commit 826dfeb

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tests/IronPython.Tests/Cases/CommonCases.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.IO;
67
using System.Threading;
78

89
using NUnit.Framework;
@@ -36,6 +37,42 @@ protected int TestImpl(TestInfo testcase) {
3637
return -1;
3738
} finally {
3839
m?.ReleaseMutex();
40+
CleanupTempFiles(testcase);
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Removes @test_*_tmp files/directories left behind by test.support.TESTFN.
46+
/// </summary>
47+
private static void CleanupTempFiles(TestInfo testcase) {
48+
var testDir = Path.GetDirectoryName(testcase.Path);
49+
if (testDir is null) return;
50+
51+
// Clean test directory and also the StdLib test directory
52+
CleanupTempFilesInDir(testDir);
53+
var stdlibTestDir = Path.Combine(CaseExecuter.FindRoot(), "src", "core", "IronPython.StdLib", "lib", "test");
54+
if (stdlibTestDir != testDir) {
55+
CleanupTempFilesInDir(stdlibTestDir);
56+
}
57+
}
58+
59+
private static void CleanupTempFilesInDir(string dir) {
60+
if (!Directory.Exists(dir)) return;
61+
62+
try {
63+
foreach (var entry in Directory.EnumerateFileSystemEntries(dir, "@test_*_tmp*")) {
64+
try {
65+
if (File.GetAttributes(entry).HasFlag(FileAttributes.Directory)) {
66+
Directory.Delete(entry, recursive: true);
67+
} else {
68+
File.Delete(entry);
69+
}
70+
} catch {
71+
// ignore locked/in-use files
72+
}
73+
}
74+
} catch {
75+
// ignore enumeration errors
3976
}
4077
}
4178
}

0 commit comments

Comments
 (0)