diff --git a/tests/FSharp.Compiler.ComponentTests/InteractiveSession/Misc.fs b/tests/FSharp.Compiler.ComponentTests/InteractiveSession/Misc.fs index 97d0dcd865b..74298498dbb 100644 --- a/tests/FSharp.Compiler.ComponentTests/InteractiveSession/Misc.fs +++ b/tests/FSharp.Compiler.ComponentTests/InteractiveSession/Misc.fs @@ -1997,9 +1997,13 @@ if toEnd <> [| 3; 4; 5 |] then exit 1 let name = "World" let count = 42 let greeting = $"Hello, {name}! Count: {count}" +printfn "%s" greeting if greeting <> "Hello, World! Count: 42" then exit 1 +System.Globalization.CultureInfo.CurrentCulture <- System.Globalization.CultureInfo.InvariantCulture + let formatted = $"Pi is approximately {System.Math.PI:F2}" +printfn "%s" formatted if not (formatted.Contains("3.14")) then exit 1 () """ diff --git a/tests/FSharp.Compiler.ComponentTests/Miscellaneous/FsharpSuiteMigrated.fs b/tests/FSharp.Compiler.ComponentTests/Miscellaneous/FsharpSuiteMigrated.fs index 0b6e230a271..e0cfdff2ffd 100644 --- a/tests/FSharp.Compiler.ComponentTests/Miscellaneous/FsharpSuiteMigrated.fs +++ b/tests/FSharp.Compiler.ComponentTests/Miscellaneous/FsharpSuiteMigrated.fs @@ -23,7 +23,7 @@ module ScriptRunner = let private getOrCreateEngine(args,version) sessionIsolation = match sessionIsolation with | ScriptSessionIsolation.Isolated -> - new FSharpScript(args, true, version) + getIsolatedSessionForEval args version | ScriptSessionIsolation.Shared -> getSessionForEval args version diff --git a/tests/FSharp.Test.Utilities/Compiler.fs b/tests/FSharp.Test.Utilities/Compiler.fs index afbe687012e..af1f36ca80f 100644 --- a/tests/FSharp.Test.Utilities/Compiler.fs +++ b/tests/FSharp.Test.Utilities/Compiler.fs @@ -1133,13 +1133,11 @@ module rec Compiler = let outputWritten, errorsWritten = capture.OutText, capture.ErrorText processScriptResults fs result outputWritten errorsWritten - let scriptingShim = Path.Combine(__SOURCE_DIRECTORY__,"ScriptingShims.fsx") let private evalScriptFromDisk (fs: FSharpCompilationSource) (script:FSharpScript) : CompilationResult = let fileNames = (fs.Source :: fs.AdditionalSources) |> List.map (fun x -> x.GetSourceFileName) - |> List.insertAt 0 scriptingShim |> List.map (sprintf " @\"%s\"") |> String.Concat @@ -1158,13 +1156,21 @@ module rec Compiler = let internal sessionCache = Collections.Concurrent.ConcurrentDictionary * LangVersion, FSharpScript>() + + let internal createSessionWithShadowedExit args version = + let script = new FSharpScript(additionalArgs=args,quiet=true,langVersion=version) + script.ApplyExitShadowing() + script + + let getIsolatedSessionForEval args version = + createSessionWithShadowedExit args version let getSessionForEval args version = let key = Set args, version match sessionCache.TryGetValue(key) with | true, script -> script | _ -> - let script = new FSharpScript(additionalArgs=args,quiet=true,langVersion=version) + let script = createSessionWithShadowedExit args version sessionCache.TryAdd(key, script) |> ignore script diff --git a/tests/FSharp.Test.Utilities/CompilerAssert.fs b/tests/FSharp.Test.Utilities/CompilerAssert.fs index 760a8278d04..d09896563e5 100644 --- a/tests/FSharp.Test.Utilities/CompilerAssert.fs +++ b/tests/FSharp.Test.Utilities/CompilerAssert.fs @@ -22,6 +22,7 @@ open FSharp.Compiler.BuildGraph open System.Runtime.Loader #endif open FSharp.Test.Utilities +open FSharp.Test.ScriptHelpers open Microsoft.CodeAnalysis open Microsoft.CodeAnalysis.CSharp open Xunit @@ -1006,41 +1007,25 @@ Updated automatically, please check diffs in your pull request, changes must be // Save CurrentUICulture and GraphNode.culture to restore after FSI session // FSI may change these via --preferreduilang option, and the change persists // in the static GraphNode.culture which affects async computations in other tests - let originalUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture - let originalGraphNodeCulture = GraphNode.culture - + let originalUICulture = CultureInfo.CurrentUICulture + let originalGraphNodeCulture = GraphNode.culture try - // Initialize output and input streams - use inStream = new StringReader("") use outStream = new StringWriter() use errStream = new StringWriter() + use script = new FSharpScript(additionalArgs = Array.append [| "--noninteractive" |] options, quiet = false, outWriter = outStream, errWriter = errStream) + script.ApplyExitShadowing() + let result, errors = script.Eval(source) - // Build command line arguments & start FSI session - let argv = [| "C:\\fsi.exe" |] -#if NETCOREAPP - let args = Array.append argv [|"--noninteractive"; "--targetprofile:netcore"|] -#else - let args = Array.append argv [|"--noninteractive"; "--targetprofile:mscorlib"|] -#endif - let allArgs = Array.append args options - - let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration() - use fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream, collectible = true) - - let ch, errors = fsiSession.EvalInteractionNonThrowing source - - let errorMessages = ResizeArray() - errors - |> Seq.iter (fun error -> errorMessages.Add(error.Message)) + let errorMessages = ResizeArray(errors |> Seq.map _.Message) - match ch with - | Choice2Of2 ex -> errorMessages.Add(ex.Message) + match result with + | Result.Error ex -> errorMessages.Add(ex.Message) | _ -> () errorMessages, string outStream, string errStream finally // Restore CurrentUICulture and GraphNode.culture to prevent culture leaking between tests - System.Threading.Thread.CurrentThread.CurrentUICulture <- originalUICulture + CultureInfo.CurrentUICulture <- originalUICulture GraphNode.culture <- originalGraphNodeCulture static member RunScriptWithOptions options (source: string) (expectedErrorMessages: string list) = diff --git a/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj b/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj index fde44ceb83d..cb5f54ad381 100644 --- a/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj +++ b/tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj @@ -25,16 +25,15 @@ scriptlib.fsx - + - diff --git a/tests/FSharp.Test.Utilities/ScriptHelpers.fs b/tests/FSharp.Test.Utilities/ScriptHelpers.fs index a0c5b178488..cb2956ab8ac 100644 --- a/tests/FSharp.Test.Utilities/ScriptHelpers.fs +++ b/tests/FSharp.Test.Utilities/ScriptHelpers.fs @@ -19,7 +19,7 @@ type LangVersion = | Preview | Latest -type FSharpScript(?additionalArgs: string[], ?quiet: bool, ?langVersion: LangVersion) = +type FSharpScript(?additionalArgs: string[], ?quiet: bool, ?langVersion: LangVersion, ?outWriter: IO.TextWriter, ?errWriter: IO.TextWriter) = let additionalArgs = defaultArg additionalArgs [||] let quiet = defaultArg quiet true @@ -44,13 +44,23 @@ type FSharpScript(?additionalArgs: string[], ?quiet: bool, ?langVersion: LangVer let argv = Array.append baseArgs additionalArgs - let fsi = FsiEvaluationSession.Create (config, argv, TextReader.Null, stdout, stderr) + let outWriter = defaultArg outWriter stdout + let errWriter = defaultArg errWriter stderr + let fsi = FsiEvaluationSession.Create (config, argv, TextReader.Null, outWriter, errWriter) member _.ValueBound = fsi.ValueBound member _.Fsi = fsi - member this.Eval(code: string, ?cancellationToken: CancellationToken, ?desiredCulture: Globalization.CultureInfo) = + member _.ApplyExitShadowing() = + fsi.EvalInteraction """ +let exit (code:int) = + if code = 0 then + () + else failwith $"Script called function 'exit' with code={code}." + """ + + member _.Eval(code: string, ?cancellationToken: CancellationToken, ?desiredCulture: Globalization.CultureInfo) = let originalCulture = Thread.CurrentThread.CurrentCulture let originalUICulture = Thread.CurrentThread.CurrentUICulture Thread.CurrentThread.CurrentCulture <- Option.defaultValue Globalization.CultureInfo.InvariantCulture desiredCulture diff --git a/tests/FSharp.Test.Utilities/ScriptingShims.fsx b/tests/FSharp.Test.Utilities/ScriptingShims.fsx deleted file mode 100644 index be9ce9142c5..00000000000 --- a/tests/FSharp.Test.Utilities/ScriptingShims.fsx +++ /dev/null @@ -1,8 +0,0 @@ -namespace global - -[] -module GlobalShims = - let exit (code:int) = - if code = 0 then - () - else failwith $"Script called function 'exit' with code={code}." diff --git a/tests/FSharp.Test.Utilities/TestFramework.fs b/tests/FSharp.Test.Utilities/TestFramework.fs index dcb64909ad2..2348b09df8e 100644 --- a/tests/FSharp.Test.Utilities/TestFramework.fs +++ b/tests/FSharp.Test.Utilities/TestFramework.fs @@ -60,8 +60,6 @@ let rec copyDirectory (sourceDir: string) (destinationDir: string) (recursive: b [] module Commands = - let gate = obj() - // Execute the process pathToExe passing the arguments: arguments with the working directory: workingDir timeout after timeout milliseconds -1 = wait forever // returns exit code, stdio and stderr as string arrays let executeProcess pathToExe arguments workingDir = diff --git a/tests/fsharp/TypeProviderTests.fs b/tests/fsharp/TypeProviderTests.fs index d447c4c7d25..e0057aef22f 100644 --- a/tests/fsharp/TypeProviderTests.fs +++ b/tests/fsharp/TypeProviderTests.fs @@ -7,9 +7,6 @@ #load "../FSharp.Test.Utilities/TestFramework.fs" #load "single-test.fs" #else -// Disable parallel execution because this module contains FSI stdin tests -// that can interfere with other FSI stdin tests in CoreTests -[] module FSharp.Test.FSharpSuite.TypeProviderTests #endif diff --git a/tests/fsharp/single-test.fs b/tests/fsharp/single-test.fs index 41f6e032609..e8d26711979 100644 --- a/tests/fsharp/single-test.fs +++ b/tests/fsharp/single-test.fs @@ -9,8 +9,6 @@ open FSharp.Compiler.IO let testConfig = testConfig __SOURCE_DIRECTORY__ -let log = printfn - type Permutation = #if NETCOREAPP | FSC_NETCORE of optimized: bool * buildOnly: bool diff --git a/tests/fsharp/tests.fs b/tests/fsharp/tests.fs index 0bfd7191781..330ffe93590 100644 --- a/tests/fsharp/tests.fs +++ b/tests/fsharp/tests.fs @@ -27,11 +27,6 @@ let FSI = FSI_NETFX #endif // ^^^^^^^^^^^^ To run these tests in F# Interactive , 'build net40', then send this chunk, then evaluate body of a test ^^^^^^^^^^^^ -let log = printfn - -// Disable parallel execution for CoreTests because the printing and FSI tests -// spawn external FSI processes with stdin redirection that can interfere with each other -[] module CoreTests =