11module FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests
22
3+ open System
34open System.IO
45open Xunit
56open FSharp.Test
@@ -51,19 +52,50 @@ let x = 21+21
5152let ``typecheck - only flag catches type errors in scripts with #load`` () =
5253 let tempDir = Path.Combine( Path.GetTempPath(), Path.GetRandomFileName())
5354 Directory.CreateDirectory( tempDir) |> ignore
55+ let originalDir = Environment.CurrentDirectory
5456
5557 try
58+ Environment.CurrentDirectory <- tempDir
5659 let domainPath = Path.Combine( tempDir, " Domain.fsx" )
5760 let mainPath = Path.Combine( tempDir, " A.fsx" )
5861
5962 File.WriteAllText( domainPath, " type T = { Field: string }\n printfn \" D\" " )
60- File.WriteAllText( mainPath, sprintf " #load \" %s \"\n open Domain\n let y = { Field = 1 }\n printfn \" A\" " domainPath )
63+ File.WriteAllText( mainPath, " #load \" Domain.fsx \"\n open Domain\n let y = { Field = 1 }\n printfn \" A\" " )
6164
6265 FsxFromPath mainPath
6366 |> withOptions [ " --typecheck-only" ]
6467 |> runFsi
6568 |> shouldFail
6669 |> withStdErrContains " This expression was expected to have type"
6770 finally
68- if Directory.Exists( tempDir) then
69- Directory.Delete( tempDir, true )
71+ try
72+ Environment.CurrentDirectory <- originalDir
73+ if Directory.Exists( tempDir) then
74+ Directory.Delete( tempDir, true )
75+ with _ -> ()
76+
77+ [<Fact>]
78+ let ``typecheck - only flag catches type errors in loaded file`` () =
79+ let tempDir = Path.Combine( Path.GetTempPath(), Path.GetRandomFileName())
80+ Directory.CreateDirectory( tempDir) |> ignore
81+ let originalDir = Environment.CurrentDirectory
82+
83+ try
84+ Environment.CurrentDirectory <- tempDir
85+ let domainPath = Path.Combine( tempDir, " Domain.fsx" )
86+ let mainPath = Path.Combine( tempDir, " A.fsx" )
87+
88+ File.WriteAllText( domainPath, " type T = { Field: string }\n let x: int = \" error\"\n printfn \" D\" " )
89+ File.WriteAllText( mainPath, " #load \" Domain.fsx\"\n open Domain\n let y = { Field = \" ok\" }\n printfn \" A\" " )
90+
91+ FsxFromPath mainPath
92+ |> withOptions [ " --typecheck-only" ]
93+ |> runFsi
94+ |> shouldFail
95+ |> withStdErrContains " This expression was expected to have type"
96+ finally
97+ try
98+ Environment.CurrentDirectory <- originalDir
99+ if Directory.Exists( tempDir) then
100+ Directory.Delete( tempDir, true )
101+ with _ -> ()
0 commit comments