diff --git a/ICSharpCode.Decompiler.Tests/CorrectnessTestRunner.cs b/ICSharpCode.Decompiler.Tests/CorrectnessTestRunner.cs index 499cc16d5..443485a81 100644 --- a/ICSharpCode.Decompiler.Tests/CorrectnessTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/CorrectnessTestRunner.cs @@ -443,9 +443,7 @@ namespace ICSharpCode.Decompiler.Tests decompiledOutputFile = await Tester.CompileCSharp(decompiledCodeFile, options).ConfigureAwait(false); await Tester.RunAndCompareOutput(testFileName, outputFile.PathToAssembly, decompiledOutputFile.PathToAssembly, decompiledCodeFile, (options & CompilerOptions.UseTestRunner) != 0, (options & CompilerOptions.Force32Bit) != 0); - Tester.RepeatOnIOError(() => File.Delete(decompiledCodeFile)); - Tester.RepeatOnIOError(() => File.Delete(decompiledOutputFile.PathToAssembly)); } finally { @@ -473,9 +471,7 @@ namespace ICSharpCode.Decompiler.Tests decompiledOutputFile = await Tester.CompileCSharp(decompiledCodeFile, options).ConfigureAwait(false); await Tester.RunAndCompareOutput(testFileName, outputFile.PathToAssembly, decompiledOutputFile.PathToAssembly, decompiledCodeFile, (options & CompilerOptions.UseTestRunner) != 0, (options & CompilerOptions.Force32Bit) != 0); - Tester.RepeatOnIOError(() => File.Delete(decompiledCodeFile)); - Tester.RepeatOnIOError(() => File.Delete(decompiledOutputFile.PathToAssembly)); } finally { @@ -499,9 +495,7 @@ namespace ICSharpCode.Decompiler.Tests decompiledOutputFile = await Tester.CompileCSharp(decompiledCodeFile, options).ConfigureAwait(false); await Tester.RunAndCompareOutput(testFileName, outputFile, decompiledOutputFile.PathToAssembly, decompiledCodeFile, (options & CompilerOptions.UseTestRunner) != 0, (options & CompilerOptions.Force32Bit) != 0).ConfigureAwait(false); - Tester.RepeatOnIOError(() => File.Delete(decompiledCodeFile)); - Tester.RepeatOnIOError(() => File.Delete(decompiledOutputFile.PathToAssembly)); } finally { diff --git a/ICSharpCode.Decompiler.Tests/Helpers/Tester.VB.cs b/ICSharpCode.Decompiler.Tests/Helpers/Tester.VB.cs index 5ce1bf96b..d871bed94 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/Tester.VB.cs +++ b/ICSharpCode.Decompiler.Tests/Helpers/Tester.VB.cs @@ -47,7 +47,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers if ((flags & CompilerOptions.UseMcsMask) == 0) { CompilerResults results = new CompilerResults(); - results.PathToAssembly = outputFileName ?? Path.GetTempFileName(); + results.PathToAssembly = outputFileName; var (roslynVersion, languageVersion) = (flags & CompilerOptions.UseRoslynMask) switch { 0 => ("legacy", "11"), diff --git a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs index c2ecd3f70..5a55e10c2 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs +++ b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection.PortableExecutable; @@ -296,6 +297,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers static string GetTargetFrameworkAttributeSnippetFile() { + // Note: this leaks a temporary file, we're not attempting to delete it, because it is only one. var tempFile = Path.GetTempFileName(); File.WriteAllText(tempFile, targetFrameworkAttributeSnippet); return tempFile; @@ -395,7 +397,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers if ((flags & CompilerOptions.UseMcsMask) == 0) { CompilerResults results = new CompilerResults(); - results.PathToAssembly = outputFileName ?? Path.GetTempFileName(); + results.PathToAssembly = outputFileName; var (roslynVersion, languageVersion) = (flags & CompilerOptions.UseRoslynMask) switch { 0 => ("legacy", "5"), @@ -488,7 +490,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers else { CompilerResults results = new CompilerResults(); - results.PathToAssembly = outputFileName ?? Path.GetTempFileName(); + results.PathToAssembly = outputFileName; string testBasePath = RoundtripAssembly.TestDir; if (!Directory.Exists(testBasePath)) { @@ -828,11 +830,42 @@ namespace ICSharpCode.Decompiler.Tests.Helpers public class CompilerResults { - public string PathToAssembly { get; set; } + readonly HashSet tempFiles = new(Decompiler.Util.Platform.FileNameComparer); + string pathToAssembly; + + public string PathToAssembly { + get { + if (pathToAssembly == null) + { + pathToAssembly = Path.GetTempFileName(); + tempFiles.Add(pathToAssembly); + } + return pathToAssembly; + } + set { + if (pathToAssembly != null) + { + throw new InvalidOperationException("PathToAssembly can only be set once"); + } + pathToAssembly = value; + } + } public void DeleteTempFiles() { + foreach (var file in tempFiles) + { + Tester.RepeatOnIOError(() => File.Delete(file)); + } + } + public void AddTempFile(string file) + { + if (!Path.IsPathFullyQualified(file)) + { + throw new InvalidOperationException("file must be a fully qualified path"); + } + tempFiles.Add(file); } } } diff --git a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs index 2cf64cf18..237bdd402 100644 --- a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs @@ -278,6 +278,7 @@ namespace ICSharpCode.Decompiler.Tests var decompiled = await Tester.DecompileCSharp(executable, settings).ConfigureAwait(false); CodeAssert.FilesAreEqual(csFile, decompiled); + Tester.RepeatOnIOError(() => File.Delete(decompiled)); } static readonly object copyLock = new object(); diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index bc5b895f8..b2542be8d 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -745,6 +745,7 @@ namespace ICSharpCode.Decompiler.Tests // 3. Compile CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).ToArray()); + Tester.RepeatOnIOError(() => File.Delete(decompiled)); } } } diff --git a/ICSharpCode.Decompiler.Tests/UglyTestRunner.cs b/ICSharpCode.Decompiler.Tests/UglyTestRunner.cs index f33eb21ca..1e2cceb6c 100644 --- a/ICSharpCode.Decompiler.Tests/UglyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/UglyTestRunner.cs @@ -163,6 +163,7 @@ namespace ICSharpCode.Decompiler.Tests var decompiled = await Tester.DecompileCSharp(executable, decompilerSettings).ConfigureAwait(false); CodeAssert.FilesAreEqual(expectedFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).ToArray()); + Tester.RepeatOnIOError(() => File.Delete(decompiled)); } } } diff --git a/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs index 06952a8c5..b5db5269b 100644 --- a/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs @@ -139,6 +139,7 @@ namespace ICSharpCode.Decompiler.Tests var decompiled = await Tester.DecompileCSharp(executable.PathToAssembly, settings).ConfigureAwait(false); CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(options).ToArray()); + Tester.RepeatOnIOError(() => File.Delete(decompiled)); } } }