diff --git a/tests/FSharp.Compiler.Service.Tests/Common.fs b/tests/FSharp.Compiler.Service.Tests/Common.fs index 1d2b3b7a4077..0cefad2a6463 100644 --- a/tests/FSharp.Compiler.Service.Tests/Common.fs +++ b/tests/FSharp.Compiler.Service.Tests/Common.fs @@ -475,8 +475,8 @@ let assertRange [] module TempDirUtils = - let getTempPath dir = - Path.Combine(TestFramework.tempDirectoryOfThisTestRun, dir) + let getTempPath dir = + Path.Combine(tempDirectoryOfThisTestRun.Value.FullName, dir) /// Returns the file name part of a temp file name created with tryCreateTemporaryFileName () /// and an added process id and thread id to ensure uniqueness between threads. diff --git a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs index 561795cf9c8a..a4446d633bec 100644 --- a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs +++ b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs @@ -4403,7 +4403,7 @@ let ``Test Project33 extension methods`` () = ("GetValue", ["member"; "extmem"])] module internal Project34 = - let directoryPath = createTemporaryDirectory "Project34" + let directoryPath = createTemporaryDirectory().FullName let sourceFileName = Path.Combine(directoryPath, "Program.fs") let dllName = Path.ChangeExtension(sourceFileName, ".dll") let projFileName = Path.ChangeExtension(sourceFileName, ".fsproj") diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs index 118af638a1b9..115964702f84 100644 --- a/tests/FSharp.Test.Utilities/Compiler.fs +++ b/tests/FSharp.Test.Utilities/Compiler.fs @@ -736,7 +736,7 @@ module rec Compiler = let outputDirectory = match fs.OutputDirectory with | Some di -> di - | None -> DirectoryInfo(createTemporaryDirectory "compileFSharp") + | None -> createTemporaryDirectory() let references = processReferences fs.References outputDirectory let compilation = Compilation.CreateFromSources([fs.Source] @ fs.AdditionalSources, output, options, fs.TargetFramework, references, name, outputDirectory) compileFSharpCompilation compilation fs.IgnoreWarnings (FS fs) @@ -786,7 +786,7 @@ module rec Compiler = let outputDirectory = match csSource.OutputDirectory with | Some di -> di - | None -> DirectoryInfo(createTemporaryDirectory "compileCSharp") + | None -> createTemporaryDirectory() let additionalReferences = processReferences csSource.References outputDirectory @@ -927,7 +927,7 @@ module rec Compiler = let outputDirectory = match fsSource.OutputDirectory with | Some di -> di - | None -> DirectoryInfo(createTemporaryDirectory "typecheckResults") + | None -> createTemporaryDirectory() let references = processReferences fsSource.References outputDirectory if references.IsEmpty then Array.empty @@ -1063,7 +1063,7 @@ module rec Compiler = let outputDirectory = match fs.OutputDirectory with | Some di -> di - | None -> DirectoryInfo(createTemporaryDirectory "runFsi") + | None -> createTemporaryDirectory() outputDirectory.Create() let references = processReferences fs.References outputDirectory diff --git a/tests/FSharp.Test.Utilities/CompilerAssert.fs b/tests/FSharp.Test.Utilities/CompilerAssert.fs index 32a173078627..298aaddac8bc 100644 --- a/tests/FSharp.Test.Utilities/CompilerAssert.fs +++ b/tests/FSharp.Test.Utilities/CompilerAssert.fs @@ -589,7 +589,7 @@ module CompilerAssertHelpers = compilationRefs, deps let compileCompilation ignoreWarnings (cmpl: Compilation) f = - let outputDirectory = DirectoryInfo(createTemporaryDirectory "compileCompilation") + let outputDirectory = createTemporaryDirectory() f (compileCompilationAux outputDirectory ignoreWarnings cmpl) // NOTE: This function will not clean up all the compiled projects after itself. @@ -599,7 +599,7 @@ module CompilerAssertHelpers = let outputDirectory = match cmpl with | Compilation(outputDirectory = Some outputDirectory) -> DirectoryInfo(outputDirectory.FullName) - | Compilation _ -> DirectoryInfo(createTemporaryDirectory "returnCompilation") + | Compilation _ -> createTemporaryDirectory() outputDirectory.Create() compileCompilationAux outputDirectory ignoreWarnings cmpl diff --git a/tests/FSharp.Test.Utilities/DirectoryAttribute.fs b/tests/FSharp.Test.Utilities/DirectoryAttribute.fs index 1266bc295a87..f54faec596d8 100644 --- a/tests/FSharp.Test.Utilities/DirectoryAttribute.fs +++ b/tests/FSharp.Test.Utilities/DirectoryAttribute.fs @@ -32,7 +32,7 @@ type DirectoryAttribute(dir: string) = | _ -> None let createCompilationUnit path (filename: string) = - let outputDirectoryPath = createTemporaryDirectory "dir" + let outputDirectoryPath = createTemporaryDirectory().FullName let sourceFilePath = normalizePathSeparator (path ++ filename) let fsBslFilePath = sourceFilePath + baselineSuffix + ".err.bsl" let ilBslFilePath = diff --git a/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj b/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj index d0afb09fa182..fe04b9ed6760 100644 --- a/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj +++ b/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj @@ -32,8 +32,8 @@ - + diff --git a/tests/FSharp.Test.Utilities/TestFramework.fs b/tests/FSharp.Test.Utilities/TestFramework.fs index 0999e38caef6..9cb037030afd 100644 --- a/tests/FSharp.Test.Utilities/TestFramework.fs +++ b/tests/FSharp.Test.Utilities/TestFramework.fs @@ -5,33 +5,30 @@ module TestFramework open System open System.IO open System.Diagnostics -open System.Reflection open Scripting open Xunit open FSharp.Compiler.IO -open FSharp.Test let getShortId() = Guid.NewGuid().ToString().[..7] -// Temporary directory is TempPath + "/FSharp.Test.Utilities/yyy-MM-dd-xxxxxxx/" +// Temporary directory is TempPath + "/FSharp.Test.Utilities/xxxxxxx/" let tempDirectoryOfThisTestRun = let temp = Path.GetTempPath() - let today = DateTime.Now.ToString("yyyy-MM-dd") - let directory = - DirectoryInfo(temp).CreateSubdirectory($"FSharp.Test.Utilities/{today}-{getShortId()}") + lazy DirectoryInfo(temp).CreateSubdirectory($"FSharp.Test.Utilities/{getShortId()}") - directory.FullName +let cleanUpTemporaryDirectoryOfThisTestRun () = + if tempDirectoryOfThisTestRun.IsValueCreated then + try tempDirectoryOfThisTestRun.Value.Delete(true) with _ -> () -let createTemporaryDirectory (part: string) = - DirectoryInfo(tempDirectoryOfThisTestRun) - .CreateSubdirectory($"{part}-{getShortId()}") - .FullName +let createTemporaryDirectory () = + tempDirectoryOfThisTestRun.Value + .CreateSubdirectory($"{getShortId()}") let getTemporaryFileName () = - (createTemporaryDirectory "temp") ++ $"tmp_{getShortId()}" + createTemporaryDirectory().FullName ++ getShortId() let getTemporaryFileNameInDirectory (directory: string) = - directory ++ $"tmp_{getShortId()}" + directory ++ getShortId() // Well, this function is AI generated. let rec copyDirectory (sourceDir: string) (destinationDir: string) (recursive: bool) = @@ -470,11 +467,8 @@ let testConfig sourceDir (relativePathToTestFixture: string) = let cfg = suiteHelpers.Value let testFixtureFullPath = Path.GetFullPath(sourceDir ++ relativePathToTestFixture) - let description = relativePathToTestFixture.Split('\\', '/') |> String.concat "-" - - let tempTestRoot = createTemporaryDirectory description let tempTestDir = - DirectoryInfo(tempTestRoot) + createTemporaryDirectory() .CreateSubdirectory(relativePathToTestFixture) .FullName copyDirectory testFixtureFullPath tempTestDir true @@ -483,7 +477,7 @@ let testConfig sourceDir (relativePathToTestFixture: string) = let createConfigWithEmptyDirectory() = let cfg = suiteHelpers.Value - { cfg with Directory = createTemporaryDirectory "temp" } + { cfg with Directory = createTemporaryDirectory().FullName } type RedirectToType = | Overwrite of FilePath diff --git a/tests/FSharp.Test.Utilities/XunitHelpers.fs b/tests/FSharp.Test.Utilities/XunitHelpers.fs index 72d6c40aa8a3..79eb0363f247 100644 --- a/tests/FSharp.Test.Utilities/XunitHelpers.fs +++ b/tests/FSharp.Test.Utilities/XunitHelpers.fs @@ -8,6 +8,8 @@ open System open Xunit.Sdk open Xunit.Abstractions +open TestFramework + /// Disables custom internal parallelization added with XUNIT_EXTRAS. /// Execute test cases in a class or a module one by one instead of all at once. Allow other collections to run simultaneously. [] @@ -24,6 +26,11 @@ type FSharpXunitFramework(sink: IMessageSink) = log "FSharpXunitFramework installing TestConsole redirection" TestConsole.install() + interface IDisposable with + member _.Dispose() = + cleanUpTemporaryDirectoryOfThisTestRun () + base.Dispose() + #else // To use xUnit means to customize it. The following abomination adds 2 features: @@ -130,7 +137,12 @@ type FSharpXunitFramework(sink: IMessageSink) = // right at the start of the test run is here in the constructor. // This gets executed once per test assembly. log "FSharpXunitFramework with XUNIT_EXTRAS installing TestConsole redirection" - TestConsole.install() + TestConsole.install() + + interface IDisposable with + member _.Dispose() = + cleanUpTemporaryDirectoryOfThisTestRun () + base.Dispose() override this.CreateDiscoverer (assemblyInfo) = { new XunitTestFrameworkDiscoverer(assemblyInfo, this.SourceInformationProvider, this.DiagnosticMessageSink) with