Skip to content

Commit e95cd60

Browse files
Copilotabonie
andcommitted
Add test to verify typecheck-only prevents code execution with #load
Co-authored-by: abonie <[email protected]>
1 parent 523b166 commit e95cd60

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ let ``typecheck-only flag catches type errors in loaded file``() =
9292
|> runFsi
9393
|> shouldFail
9494
|> withStdErrContains "This expression was expected to have type"
95+
finally
96+
try
97+
if Directory.Exists(tempDir) then
98+
Directory.Delete(tempDir, true)
99+
with _ -> ()
100+
101+
[<Fact>]
102+
let ``typecheck-only flag prevents execution with #load``() =
103+
let tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
104+
Directory.CreateDirectory(tempDir) |> ignore
105+
106+
try
107+
let domainPath = Path.Combine(tempDir, "Domain.fsx")
108+
let mainPath = Path.Combine(tempDir, "A.fsx")
109+
110+
File.WriteAllText(domainPath, "type T = { Field: string }\nprintfn \"Domain.fsx output\"")
111+
// Use absolute path in #load directive since runFsi uses EvalInteraction which doesn't preserve script location context
112+
let mainContent = sprintf "#load \"%s\"\nopen Domain\nlet y = { Field = \"test\" }\nprintfn \"A.fsx output\"" (domainPath.Replace("\\", "\\\\"))
113+
File.WriteAllText(mainPath, mainContent)
114+
115+
FsxFromPath mainPath
116+
|> withOptions ["--typecheck-only"]
117+
|> runFsi
118+
|> shouldSucceed
119+
|> verifyNotInOutput "Domain.fsx output"
120+
|> verifyNotInOutput "A.fsx output"
95121
finally
96122
try
97123
if Directory.Exists(tempDir) then

0 commit comments

Comments
 (0)