Skip to content

Commit bd5aa05

Browse files
CopilotT-Gro
andcommitted
Update TypeCheckOnlyTests to use console output instead of file side effects
Co-authored-by: T-Gro <[email protected]>
1 parent 7ea403c commit bd5aa05

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

tests/FSharp.Compiler.ComponentTests/Scripting/TypeCheckOnlyTests.fs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,21 @@ let x: int = "string" // Type error
2828

2929
[<Fact>]
3030
let ``typecheck-only flag prevents execution side effects``() =
31-
let testFilePath = System.IO.Path.GetTempFileName()
32-
System.IO.File.Delete(testFilePath) // Make sure file doesn't exist initially
33-
34-
Fsx $"""
35-
System.IO.File.WriteAllText("{testFilePath}", "should not be created")
31+
Fsx """
32+
printfn "MyCrazyString"
3633
let x = 42
3734
"""
3835
|> withOptions ["--typecheck-only"]
39-
|> eval
36+
|> runFsi
4037
|> shouldSucceed
41-
42-
// Verify file was not created
43-
Assert.False(System.IO.File.Exists(testFilePath), "File should not have been created when using --typecheck-only")
38+
|> VerifyNotInOutput "MyCrazyString"
4439

4540
[<Fact>]
4641
let ``script executes without typecheck-only flag``() =
47-
let testFilePath = System.IO.Path.GetTempFileName()
48-
System.IO.File.Delete(testFilePath) // Make sure file doesn't exist initially
49-
50-
Fsx $"""
51-
System.IO.File.WriteAllText("{testFilePath}", "file was created")
42+
Fsx """
43+
printfn "MyCrazyString"
5244
let x = 42
5345
"""
54-
|> eval
46+
|> runFsi
5547
|> shouldSucceed
56-
57-
// Verify file was created when not using --typecheck-only
58-
Assert.True(System.IO.File.Exists(testFilePath), "File should have been created when not using --typecheck-only")
59-
60-
// Clean up
61-
if System.IO.File.Exists(testFilePath) then
62-
System.IO.File.Delete(testFilePath)
48+
|> verifyOutput "MyCrazyString"

tests/FSharp.Test.Utilities/Compiler.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,18 @@ Actual:
14211421
failwith $"""Output does not match expected:{Environment.NewLine}{item}{Environment.NewLine}Actual:{Environment.NewLine}{actual}{Environment.NewLine}"""
14221422
cResult
14231423

1424+
let verifyNotInOutput (expected: string) (cResult: CompilationResult) : CompilationResult =
1425+
match getOutput cResult with
1426+
| None -> cResult
1427+
| Some actual ->
1428+
if actual.Contains(expected) then
1429+
failwith $"""Output should not contain:{Environment.NewLine}{expected}{Environment.NewLine}Actual:{Environment.NewLine}{actual}{Environment.NewLine}"""
1430+
else
1431+
cResult
1432+
1433+
let VerifyNotInOutput (expected: string) (cResult: CompilationResult) : CompilationResult =
1434+
verifyNotInOutput expected cResult
1435+
14241436
type ImportScope = { Kind: ImportDefinitionKind; Name: string }
14251437

14261438
type PdbVerificationOption =
@@ -1906,6 +1918,13 @@ Actual:
19061918
let withStdOutContains (substring: string) (result: CompilationResult) : CompilationResult =
19071919
checkOutputInOrder "STDOUT" [substring] (fun o -> o.StdOut) result
19081920

1921+
let withStdOutNotContains (substring: string) (result: CompilationResult) : CompilationResult =
1922+
let checkOutput = fun o -> o.StdOut
1923+
let output = checkOutput result.Output
1924+
if output.Contains(substring) then
1925+
failwith $"""STDOUT should not contain:{Environment.NewLine}{substring}{Environment.NewLine}Actual:{Environment.NewLine}{output}{Environment.NewLine}"""
1926+
result
1927+
19091928
let withStdOutContainsAllInOrder (substrings: string list) (result: CompilationResult) : CompilationResult =
19101929
checkOutputInOrder "STDOUT" substrings (fun o -> o.StdOut) result
19111930

0 commit comments

Comments
 (0)