Skip to content

Commit 75ab724

Browse files
dsymedsyme
authored andcommitted
add compile tests
1 parent 7e18b8a commit 75ab724

File tree

7 files changed

+42
-27
lines changed

7 files changed

+42
-27
lines changed

docs/content/compiler.fsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ First, we need to reference the libraries that contain F# interactive service:
2828
*)
2929

3030
#r "FSharp.Compiler.Service.dll"
31-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
3231
open System.IO
32+
open Microsoft.FSharp.Compiler.SourceCodeServices
3333

34-
let scs = SimpleSourceCodeServices()
34+
// Create an interactive checker instance
35+
let checker = FSharpChecker.Create()
3536

3637
(**
3738
Now write content to a temporary file:
3839
3940
*)
4041
let fn = Path.GetTempFileName()
41-
let fn2 = Path.ChangeExtension(fn, ".fs")
42+
let fn2 = Path.ChangeExtension(fn, ".fsx")
4243
let fn3 = Path.ChangeExtension(fn, ".dll")
4344

4445
File.WriteAllText(fn2, """
@@ -54,7 +55,7 @@ let x = 3 + 4
5455
Now invoke the compiler:
5556
*)
5657

57-
let errors1, exitCode1 = scs.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
58+
let errors1, exitCode1 = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
5859

5960
(**
6061
@@ -67,7 +68,7 @@ module M
6768
let x = 1.0 + "" // a type error
6869
""")
6970

70-
let errors1b, exitCode1b = scs.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
71+
let errors1b, exitCode1b = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
7172

7273
(**
7374
@@ -83,11 +84,11 @@ You still have to pass the "-o" option to name the output file, but the output f
8384
The 'None' option indicates that the initiatlization code for the assembly is not executed.
8485
*)
8586
let errors2, exitCode2, dynAssembly2 =
86-
scs.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], execute=None)
87+
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], execute=None)
8788

8889
(*
8990
Passing 'Some' for the 'execute' parameter executes the initiatlization code for the assembly.
9091
*)
9192
let errors3, exitCode3, dynAssembly3 =
92-
scs.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], Some(stdout,stderr))
93+
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], Some(stdout,stderr))
9394

docs/content/tokenizer.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ file name of the source code. The defined symbols are required because the
2828
tokenizer handles `#if` directives. The file name is required only to specify
2929
locations of the source code (and it does not have to exist):
3030
*)
31-
let sourceTok = FSharpSourceTokenizer([], "C:\\test.fsx")
31+
let sourceTok = FSharpSourceTokenizer([], Some "C:\\test.fsx")
3232
(**
3333
Using the `sourceTok` object, we can now (repeatedly) tokenize lines of
3434
F# source code.

docs/content/typedtree.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ let rec visitExpr f (e:FSharpExpr) =
241241
visitExpr f baseCallExpr
242242
List.iter (visitObjMember f) overrides
243243
List.iter (snd >> List.iter (visitObjMember f)) interfaceImplementations
244-
| BasicPatterns.TraitCall(sourceTypes, traitName, typeArgs, typeInstantiation, argExprs) ->
244+
| BasicPatterns.TraitCall(sourceTypes, traitName, typeArgs, typeInstantiation, argTypes, argExprs) ->
245245
visitExprs f argExprs
246246
| BasicPatterns.ValueSet(valToSet, valueExpr) ->
247247
visitExpr f valueExpr

paket.dependencies

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
frameworks: net46, netstandard16, netcoreapp10
2+
13
source https://www.myget.org/F/dotnet-core/api/v3/index.json
24
source https://www.nuget.org/api/v2/
35

samples/FscExe/FscMain.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ open System.IO
88
open System.Reflection
99
open System.Runtime.CompilerServices
1010
open Microsoft.FSharp.Compiler
11+
open Microsoft.FSharp.Compiler.SourceCodeServices
1112
open Microsoft.FSharp.Compiler.AbstractIL.IL // runningOnMono
1213
open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
1314
open Microsoft.FSharp.Compiler.ErrorLogger
14-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
1515
open Microsoft.FSharp.Compiler.Range
1616

1717
#if FX_RESHAPED_REFLECTION
@@ -83,7 +83,7 @@ module FSharpResidentCompiler =
8383
let! (pwd,argv, reply: AsyncReplyChannel<_>) = inbox.Receive()
8484
if !progress then printfn "server agent: got compilation request, argv = %A" argv
8585
Environment.CurrentDirectory <- pwd
86-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv);
86+
let errors, exitCode = FSharpChecker.Create().Compile (argv);
8787
for error in errors do eprintfn "%s" (error.ToString())
8888
if !progress then printfn "server: finished compilation request, argv = %A" argv
8989
let output = outputCollector.GetTextAndClear()
@@ -286,7 +286,7 @@ module Driver =
286286
match exitCodeOpt with
287287
| Some exitCode -> exitCode
288288
| None ->
289-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv)
289+
let errors, exitCode = FSharpChecker.Create().Compile (argv)
290290
for error in errors do eprintfn "%s" (error.ToString())
291291
exitCode
292292

@@ -295,7 +295,7 @@ module Driver =
295295
0
296296

297297
else
298-
let errors, exitCode = SimpleSourceCodeServices().Compile (argv)
298+
let errors, exitCode = FSharpChecker.Create().Compile (argv)
299299
for error in errors do eprintfn "%s" (error.ToString())
300300
exitCode
301301

tests/service/EditorTests.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open System
3131
open System.IO
3232
open Microsoft.FSharp.Compiler
3333
open Microsoft.FSharp.Compiler.SourceCodeServices
34-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
3534
open FSharp.Compiler.Service.Tests.Common
3635

3736
let stringMethods =

tests/service/FscTests.fs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ open System.IO
1515

1616
open Microsoft.FSharp.Compiler
1717
open Microsoft.FSharp.Compiler.SourceCodeServices
18-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
1918
open FSharp.Compiler.Service.Tests
2019
open FSharp.Compiler.Service.Tests.Common
2120

@@ -111,7 +110,6 @@ type DebugMode =
111110
| Full
112111

113112
let checker = FSharpChecker.Create()
114-
let compiler = new SimpleSourceCodeServices()
115113

116114
/// Ensures the default FSharp.Core referenced by the F# compiler service (if none is
117115
/// provided explicitly) is available in the output directory.
@@ -127,10 +125,10 @@ let ensureDefaultFSharpCoreAvailable tmpDir =
127125
File.Copy(fsCoreDefaultReference(), Path.Combine(tmpDir, Path.GetFileName(fsCoreDefaultReference())), overwrite = true)
128126
#endif
129127

130-
let compile isDll debugMode (assemblyName : string) (code : string) (dependencies : string list) =
128+
let compile isDll debugMode (assemblyName : string) (ext: string) (code : string) (dependencies : string list) =
131129
let tmp = Path.Combine(Path.GetTempPath(),"test"+string(hash (isDll,debugMode,assemblyName,code,dependencies)))
132130
try Directory.CreateDirectory(tmp) |> ignore with _ -> ()
133-
let sourceFile = Path.Combine(tmp, assemblyName + ".fs")
131+
let sourceFile = Path.Combine(tmp, assemblyName + "." + ext)
134132
let outFile = Path.Combine(tmp, assemblyName + if isDll then ".dll" else ".exe")
135133
let pdbFile = Path.Combine(tmp, assemblyName + pdbExtension isDll)
136134
do File.WriteAllText(sourceFile, code)
@@ -164,17 +162,17 @@ let compile isDll debugMode (assemblyName : string) (code : string) (dependencie
164162
ensureDefaultFSharpCoreAvailable tmp
165163

166164
printfn "args: %A" args
167-
let errorInfo, id = compiler.Compile args
165+
let errorInfo, id = checker.Compile args
168166
for err in errorInfo do
169167
printfn "error: %A" err
170168
if id <> 0 then raise <| CompilationError(assemblyName, id, errorInfo)
171169
Assert.AreEqual (errorInfo.Length, 0)
172170
outFile
173171

174172
//sizeof<nativeint>
175-
let compileAndVerify isDll debugMode assemblyName code dependencies =
173+
let compileAndVerify isDll debugMode assemblyName ext code dependencies =
176174
let verifier = new PEVerifier ()
177-
let outFile = compile isDll debugMode assemblyName code dependencies
175+
let outFile = compile isDll debugMode assemblyName ext code dependencies
178176
verifier.Verify outFile
179177
outFile
180178

@@ -186,7 +184,7 @@ let compileAndVerifyAst (name : string, ast : Ast.ParsedInput list, references :
186184

187185
ensureDefaultFSharpCoreAvailable outDir
188186

189-
let errors, id = compiler.Compile(ast, name, outFile, references, executable = false)
187+
let errors, id = checker.Compile(ast, name, outFile, references, executable = false)
190188
for err in errors do printfn "error: %A" err
191189
Assert.AreEqual (errors.Length, 0)
192190
if id <> 0 then raise <| CompilationError(name, id, errors)
@@ -225,7 +223,7 @@ module Foo
225223
printfn "done!" // make the code have some initialization effect
226224
"""
227225

228-
compileAndVerify true PdbOnly "Foo" code [] |> ignore
226+
compileAndVerify true PdbOnly "Foo" "fs" code [] |> ignore
229227

230228
[<Test>]
231229
let ``3. Simple FSC executable test`` () =
@@ -236,7 +234,7 @@ module Bar
236234
let main _ = printfn "Hello, World!" ; 42
237235
238236
"""
239-
let outFile = compileAndVerify false PdbOnly "Bar" code []
237+
let outFile = compileAndVerify false PdbOnly "Bar" "fs" code []
240238

241239
use proc = Process.Start(outFile, "")
242240
proc.WaitForExit()
@@ -295,7 +293,7 @@ module Bar
295293
296294
"""
297295
try
298-
compile false PdbOnly "Bar" code [] |> ignore
296+
compile false PdbOnly "Bar" "fs" code [] |> ignore
299297
with
300298
| :? CompilationError as exn ->
301299
Assert.AreEqual(6,exn.Data2.[0].StartLineAlternate)
@@ -307,19 +305,34 @@ let ``Check cols are indexed by 1`` () =
307305
let code = "let x = 1 + a"
308306

309307
try
310-
compile false PdbOnly "Foo" code [] |> ignore
308+
compile false PdbOnly "Foo" "fs" code [] |> ignore
311309
with
312310
| :? CompilationError as exn ->
313311
Assert.True(exn.Data2.[0].ToString().Contains("Foo.fs (1,13)-(1,14)"))
314312
| _ -> failwith "No compilation error"
315313

316314

315+
[<Test>]
316+
let ``Check compile of bad fsx`` () =
317+
let code = """
318+
#load "missing.fsx"
319+
#r "missing.dll"
320+
"""
321+
322+
try
323+
compile false PdbOnly "Foo" "fsx" code [] |> ignore
324+
with
325+
| :? CompilationError as exn ->
326+
Assert.True(exn.Data2.[0].ToString().Contains("Could not load file '"))
327+
Assert.True(exn.Data2.[0].ToString().Contains("missing.fsx"))
328+
Assert.True(exn.Data2.[1].ToString().Contains("Could not locate the assembly \"missing.dll\""))
329+
| _ -> failwith "No compilation error"
330+
317331

318332
#if STRESS
319333
// For this stress test the aim is to check if we have a memory leak
320334

321335
module StressTest1 =
322-
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices
323336
open System.IO
324337

325338
[<Test>]

0 commit comments

Comments
 (0)