Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique names for generated "test.fs" files #18308

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
wip - see what breaks
majocha committed Feb 10, 2025
commit 45abdfb07e243eed3c645667b9199e8d30e7c8c8
6 changes: 6 additions & 0 deletions src/Compiler/Utilities/range.fs
Original file line number Diff line number Diff line change
@@ -193,6 +193,7 @@ type FileIndexTable() =
//
// TO move forward we should eventually introduce a new type NormalizedFileName that tracks this invariant.
member t.FileToIndex normalize filePath =

match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->
@@ -203,6 +204,11 @@ type FileIndexTable() =
else
filePath


//match normalizedFilePath with
//| "test.fs" | "test.fsx" | "test.fsi" -> failwith "FileToIndex naked test file"
//| _ -> ()

match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ module NameResolutionTests =
[<Fact>]
let FieldNotInRecord () =
FSharp """
module Test
type A = { Hello:string; World:string }
type B = { Size:int; Height:int }
type C = { Wheels:int }
@@ -22,13 +23,14 @@ let r:F = { Size=3; Height=4; Wall=1 }
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 1129, Line 9, Col 31, Line 9, Col 35, "The record type 'F' does not contain a label 'Wall'. Maybe you want one of the following:" + System.Environment.NewLine + " Wallis")
(Error 764, Line 9, Col 11, Line 9, Col 39, "No assignment given for field 'Wallis' of type 'Test.F'")
(Error 1129, Line 10, Col 31, Line 10, Col 35, "The record type 'F' does not contain a label 'Wall'. Maybe you want one of the following:" + System.Environment.NewLine + " Wallis")
(Error 764, Line 10, Col 11, Line 10, Col 39, "No assignment given for field 'Wallis' of type 'Test.F'")
]

[<Fact>]
let RecordFieldProposal () =
FSharp """
module Test
type A = { Hello:string; World:string }
type B = { Size:int; Height:int }
type C = { Wheels:int }
@@ -41,8 +43,8 @@ let r = { Size=3; Height=4; Wall=1 }
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 39, Line 9, Col 29, Line 9, Col 33, "The record label 'Wall' is not defined. Maybe you want one of the following:" + System.Environment.NewLine + " Walls" + System.Environment.NewLine + " Wallis")
(Error 764, Line 9, Col 9, Line 9, Col 37, "No assignment given for field 'Wallis' of type 'Test.F'")
(Error 39, Line 10, Col 29, Line 10, Col 33, "The record label 'Wall' is not defined. Maybe you want one of the following:" + System.Environment.NewLine + " Walls" + System.Environment.NewLine + " Wallis")
(Error 764, Line 10, Col 9, Line 10, Col 37, "No assignment given for field 'Wallis' of type 'Test.F'")
]

let multipleRecdTypeChoiceWarningWith1AlternativeSource = """
11 changes: 6 additions & 5 deletions tests/FSharp.Test.Utilities/Compiler.fs
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ open TestFramework
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open FSharp.Compiler.CodeAnalysis
open System.Threading

module rec Compiler =

@@ -435,19 +436,19 @@ module rec Compiler =
}

let FsxSourceCode source =
SourceCodeFileKind.Fsx({FileName="test.fsx"; SourceText=Some source})
SourceCodeFileKind.Fsx({FileName=FileNames.TestFsx; SourceText=Some source})

let Source source =
SourceCodeFileKind.Create("test.fs", source)
SourceCodeFileKind.Create(FileNames.TestFs, source)

let SourceFromPath path =
SourceCodeFileKind.Create(path)

let FsiSource source =
SourceCodeFileKind.Fsi({FileName="test.fsi"; SourceText=Some source })
SourceCodeFileKind.Fsi({FileName= FileNames.TestFsi; SourceText=Some source })

let FsSource source =
SourceCodeFileKind.Fs({FileName="test.fs"; SourceText=Some source })
SourceCodeFileKind.Fs({FileName= FileNames.TestFs; SourceText=Some source })

let CsSource source =
SourceCodeFileKind.Cs({FileName="test.cs"; SourceText=Some source })
@@ -998,7 +999,7 @@ module rec Compiler =
| None -> File.ReadAllText(fsSource.Source.GetSourceFileName)
| Some text -> text
let options = fsSource.Options |> Array.ofList
let (err: FSharpDiagnostic []) = CompilerAssert.TypeCheckWithOptionsAndName options (fsSource.Name |> Option.defaultValue "test.fs") source
let (err: FSharpDiagnostic []) = CompilerAssert.TypeCheckWithOptionsAndName options (fsSource.Name |> Option.defaultValue FileNames.TestFs) source
err

let private typecheckFSharpSource (fsSource: FSharpCompilationSource) : CompilationResult =
41 changes: 26 additions & 15 deletions tests/FSharp.Test.Utilities/CompilerAssert.fs
Original file line number Diff line number Diff line change
@@ -27,6 +27,17 @@ open Xunit
open TestFramework
open System.Collections.Immutable

type FileNames =
static let testFileName = AsyncLocal<string>()
static let mutable counter = 0
//static do testFileName.Value <- "test"
static member internal MakeTestFileNameUniqueForThisTestCase() =
testFileName.Value <- $"{Interlocked.Increment &counter}{Path.PathSeparator}test"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you didn't want to use Path.PathSeparator, which is "A platform-specific separator character used to separate path strings in environment variables."

static member TestFs with get() = $"{testFileName.Value}.fs"
static member TestFsx with get() = $"{testFileName.Value}.fsx"
static member TestFsi with get() = $"{testFileName.Value}.fsi"
static member Test with get() = testFileName.Value

#if !NETCOREAPP
module AssemblyResolver =
@@ -407,9 +418,9 @@ module CompilerAssertHelpers =
#endif
|]
{
ProjectFileName = "Z:\\test.fsproj"
ProjectFileName = "Z:\\" ++ "test.fsproj"
ProjectId = None
SourceFiles = [|"test.fs"|]
SourceFiles = [| FileNames.TestFs |]
OtherOptions = Array.append testDefaults assemblies
ReferencedProjects = [||]
IsIncompleteTypeCheckEnvironment = false
@@ -742,7 +753,7 @@ Updated automatically, please check diffs in your pull request, changes must be
Assert.Equal(expectedOutput, output)

static member Pass (source: string) =
let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, defaultProjectOptions TargetFramework.Current) |> Async.RunImmediate
let parseResults, fileAnswer = checker.ParseAndCheckFileInProject(FileNames.TestFs, 0, SourceText.ofString source, defaultProjectOptions TargetFramework.Current) |> Async.RunImmediate

Assert.Empty(parseResults.Diagnostics)

@@ -752,11 +763,11 @@ Updated automatically, please check diffs in your pull request, changes must be

Assert.Empty(typeCheckResults.Diagnostics)

static member PassWithOptions options (source: string) =
static member PassWithOptions options (source: string) =
let defaultOptions = defaultProjectOptions TargetFramework.Current
let options = { defaultOptions with OtherOptions = Array.append options defaultOptions.OtherOptions}

let parseResults, fileAnswer = checker.ParseAndCheckFileInProject("test.fs", 0, SourceText.ofString source, options) |> Async.RunImmediate
let parseResults, fileAnswer = checker.ParseAndCheckFileInProject(FileNames.TestFs, 0, SourceText.ofString source, options) |> Async.RunImmediate

Assert.Empty(parseResults.Diagnostics)

@@ -872,7 +883,7 @@ Updated automatically, please check diffs in your pull request, changes must be
let parseResults, fileAnswer =
let defaultOptions = defaultProjectOptions TargetFramework.Current
checker.ParseAndCheckFileInProject(
"test.fs",
FileNames.TestFs,
0,
SourceText.ofString source,
{ defaultOptions with OtherOptions = Array.append options defaultOptions.OtherOptions})
@@ -932,48 +943,48 @@ Updated automatically, please check diffs in your pull request, changes must be
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors))

static member CompileExeWithOptions(options, (source: string)) =
compile true options (SourceCodeFileKind.Create("test.fs", source)) (fun (errors, _, _) ->
compile true options (SourceCodeFileKind.Create(FileNames.TestFs, source)) (fun (errors, _, _) ->
if errors.Length > 0 then
Assert.Fail (sprintf "Compile had warnings and/or errors: %A" errors))

static member CompileExe (source: SourceCodeFileKind) =
CompilerAssert.CompileExeWithOptions([||], source)

static member CompileExe (source: string) =
CompilerAssert.CompileExeWithOptions([||], (SourceCodeFileKind.Create("test.fs", source)))
CompilerAssert.CompileExeWithOptions([||], (SourceCodeFileKind.Create(FileNames.TestFs, source)))

static member CompileExeAndRunWithOptions(options, (source: SourceCodeFileKind)) =
compileExeAndRunWithOptions options source

static member CompileExeAndRunWithOptions(options, (source: string)) =
compileExeAndRunWithOptions options (SourceCodeFileKind.Create("test.fs", source))
compileExeAndRunWithOptions options (SourceCodeFileKind.Create(FileNames.TestFs, source))

static member CompileExeAndRun (source: SourceCodeFileKind) =
compileExeAndRunWithOptions [||] source

static member CompileExeAndRun (source: string) =
compileExeAndRunWithOptions [||] (SourceCodeFileKind.Create("test.fs", source))
compileExeAndRunWithOptions [||] (SourceCodeFileKind.Create(FileNames.TestFs, source))

static member CompileLibraryAndVerifyILWithOptions(options, (source: SourceCodeFileKind), (f: ILVerifier -> unit)) =
compileLibraryAndVerifyILWithOptions options source f

static member CompileLibraryAndVerifyILWithOptions(options, (source: string), (f: ILVerifier -> unit)) =
compileLibraryAndVerifyILWithOptions options (SourceCodeFileKind.Create("test.fs", source)) f
compileLibraryAndVerifyILWithOptions options (SourceCodeFileKind.Create(FileNames.TestFs, source)) f

static member CompileLibraryAndVerifyDebugInfoWithOptions(options, (expectedFile: string), (source: SourceCodeFileKind)) =
compileLibraryAndVerifyDebugInfoWithOptions options expectedFile source

static member CompileLibraryAndVerifyDebugInfoWithOptions(options, (expectedFile: string), (source: string)) =
compileLibraryAndVerifyDebugInfoWithOptions options expectedFile (SourceCodeFileKind.Create("test.fs", source))
compileLibraryAndVerifyDebugInfoWithOptions options expectedFile (SourceCodeFileKind.Create(FileNames.TestFs, source))

static member CompileLibraryAndVerifyIL((source: SourceCodeFileKind), (f: ILVerifier -> unit)) =
compileLibraryAndVerifyILWithOptions [||] source f

static member CompileLibraryAndVerifyIL((source: string), (f: ILVerifier -> unit)) =
compileLibraryAndVerifyILWithOptions [||] (SourceCodeFileKind.Create("test.fs", source)) f
compileLibraryAndVerifyILWithOptions [||] (SourceCodeFileKind.Create(FileNames.TestFs, source)) f

static member CompileLibraryAndVerifyILRealSig((source: string), (f: ILVerifier -> unit)) =
compileLibraryAndVerifyILWithOptions [|"--realsig+"|] (SourceCodeFileKind.Create("test.fs", source)) f
compileLibraryAndVerifyILWithOptions [|"--realsig+"|] (SourceCodeFileKind.Create(FileNames.TestFs, source)) f

static member RunScriptWithOptionsAndReturnResult options (source: string) =
// Initialize output and input streams
@@ -1020,7 +1031,7 @@ Updated automatically, please check diffs in your pull request, changes must be

static member Parse (source: string, ?langVersion: string, ?fileName: string) =
let langVersion = defaultArg langVersion "default"
let sourceFileName = defaultArg fileName "test.fsx"
let sourceFileName = defaultArg fileName (FileNames.TestFsx)
let parsingOptions =
{ FSharpParsingOptions.Default with
SourceFiles = [| sourceFileName |]
2 changes: 1 addition & 1 deletion tests/FSharp.Test.Utilities/ProjectGeneration.fs
Original file line number Diff line number Diff line change
@@ -856,7 +856,7 @@ module Helpers =

let internal singleFileChecker source =

let fileName = "test.fs"
let fileName = FileNames.TestFs

let getSource _ fileName =
FSharpFileSnapshot(
1 change: 1 addition & 0 deletions tests/FSharp.Test.Utilities/XunitHelpers.fs
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@ type ConsoleCapturingTestRunner(test, messageBus, testClass, constructorArgument
task {
use capture = new TestConsole.ExecutionCapture()
use _ = Activity.start test.DisplayName [ ]
FileNames.MakeTestFileNameUniqueForThisTestCase()
let! executionTime = this.BaseInvokeTestMethodAsync aggregator
let output =
seq {
2 changes: 1 addition & 1 deletion tests/fsharp/Compiler/Service/MultiProjectTests.fs
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ let test() =
"""
|> SourceText.ofString
let _, checkAnswer =
CompilerAssert.Checker.ParseAndCheckFileInProject("test.fs", 0, fsText, fsOptions)
CompilerAssert.Checker.ParseAndCheckFileInProject(FileNames.TestFs, 0, fsText, fsOptions)
|> Async.RunImmediate