Skip to content

Commit

Permalink
cleaup test dir
Browse files Browse the repository at this point in the history
  • Loading branch information
majocha committed Nov 5, 2024
1 parent 0608088 commit f779f42
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions tests/FSharp.Compiler.Service.Tests/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ let assertRange

[<AutoOpen>]
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions tests/FSharp.Test.Utilities/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/FSharp.Test.Utilities/CompilerAssert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/FSharp.Test.Utilities/DirectoryAttribute.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
</Compile>
<None Include="ScriptingShims.fsx" />
<Compile Include="TestConsole.fs" />
<Compile Include="XunitHelpers.fs" />
<Compile Include="TestFramework.fs" />
<Compile Include="XunitHelpers.fs" />
<Compile Include="ILChecker.fs" />
<Compile Include="Utilities.fs" />
<Compile Include="CompilerAssert.fs" />
Expand Down
30 changes: 12 additions & 18 deletions tests/FSharp.Test.Utilities/TestFramework.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion tests/FSharp.Test.Utilities/XunitHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method, AllowMultiple = false)>]
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f779f42

Please sign in to comment.