forked from dotnet/fsharp
/
FSharpScriptTests.fs
435 lines (388 loc) · 17.8 KB
/
FSharpScriptTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.Scripting.UnitTests
open System
open System.Diagnostics
open System.IO
open System.Reflection
open System.Runtime.InteropServices
open System.Threading
open System.Threading.Tasks
open FSharp.Compiler.Interactive.Shell
open FSharp.Compiler.Scripting
open Xunit
type InteractiveTests() =
[<Fact>]
member __.``Eval object value``() =
use script = new FSharpScript()
let opt = script.Eval("1+1") |> getValue
let value = opt.Value
Assert.Equal(typeof<int>, value.ReflectionType)
Assert.Equal(2, value.ReflectionValue :?> int)
[<Fact>]
member __.``Declare and eval object value``() =
use script = new FSharpScript()
let opt = script.Eval("let x = 1 + 2\r\nx") |> getValue
let value = opt.Value
Assert.Equal(typeof<int>, value.ReflectionType)
Assert.Equal(3, value.ReflectionValue :?> int)
[<Fact>]
member __.``Capture console input``() =
use input = new RedirectConsoleInput()
use script = new FSharpScript()
input.ProvideInput "stdin:1234\r\n"
let opt = script.Eval("System.Console.ReadLine()") |> getValue
let value = opt.Value
Assert.Equal(typeof<string>, value.ReflectionType)
Assert.Equal("stdin:1234", downcast value.ReflectionValue)
[<Fact>]
member __.``Capture console output/error``() =
use output = new RedirectConsoleOutput()
use script = new FSharpScript()
use sawOutputSentinel = new ManualResetEvent(false)
use sawErrorSentinel = new ManualResetEvent(false)
output.OutputProduced.Add (fun line -> if line = "stdout:1234" then sawOutputSentinel.Set() |> ignore)
output.ErrorProduced.Add (fun line -> if line = "stderr:5678" then sawErrorSentinel.Set() |> ignore)
script.Eval("printfn \"stdout:1234\"; eprintfn \"stderr:5678\"") |> ignoreValue
Assert.True(sawOutputSentinel.WaitOne(TimeSpan.FromSeconds(5.0)), "Expected to see output sentinel value written")
Assert.True(sawErrorSentinel.WaitOne(TimeSpan.FromSeconds(5.0)), "Expected to see error sentinel value written")
[<Fact>]
member __.``Maintain state between submissions``() =
use script = new FSharpScript()
script.Eval("let add x y = x + y") |> ignoreValue
let opt = script.Eval("add 2 3") |> getValue
let value = opt.Value
Assert.Equal(typeof<int>, value.ReflectionType)
Assert.Equal(5, downcast value.ReflectionValue)
[<Fact>]
member __.``Assembly reference event successful``() =
use script = new FSharpScript()
let testCode = """
#r "System.dll"
let stacktype= typeof<System.Collections.Stack>
stacktype.Name = "Stack"
"""
let opt = script.Eval(testCode) |> getValue
let value = opt.Value
Assert.Equal(true, downcast value.ReflectionValue)
[<Fact>]
member __.``Assembly reference unsuccessful``() =
use script = new FSharpScript()
let testAssembly = "not-an-assembly-that-can-be-found.dll"
let _result, errors = script.Eval(sprintf "#r \"%s\"" testAssembly)
Assert.Equal(1, errors.Length)
[<Fact>]
member _.``Compilation errors report a specific exception``() =
use script = new FSharpScript()
let result, _errors = script.Eval("abc")
match result with
| Ok(_) -> Assert.False(true, "expected a failure")
| Error(ex) -> Assert.IsAssignableFrom(typeof<FsiCompilationException>, ex)
[<Fact>]
member _.``Runtime exceptions are propagated``() =
use script = new FSharpScript()
let result, errors = script.Eval("System.IO.File.ReadAllText(\"not-a-file-path-that-can-be-found-on-disk.txt\")")
Assert.Empty(errors)
match result with
| Ok(_) -> Assert.True(false, "expected a failure")
| Error(ex) -> Assert.IsAssignableFrom(typeof<FileNotFoundException>, ex)
[<Fact>]
member _.``Script with #r "" errors``() =
use script = new FSharpScript()
let result, errors = script.Eval("#r \"\"")
Assert.NotEmpty(errors)
match result with
| Ok(_) -> Assert.False(true, "expected a failure")
| Error(ex) -> Assert.IsAssignableFrom(typeof<FsiCompilationException>, ex)
[<Fact>]
member _.``Script with #r " " errors``() =
use script = new FSharpScript()
let result, errors = script.Eval("#r \" \"")
Assert.NotEmpty(errors)
match result with
| Ok(_) -> Assert.False(true, "expected a failure")
| Error(ex) -> Assert.IsAssignableFrom(typeof<FsiCompilationException>, ex)
[<Fact>]
member _.``Script using System.Configuration succeeds``() =
use script = new FSharpScript()
let result, errors = script.Eval("""
#r "nuget:System.Configuration.ConfigurationManager,5.0.0"
open System.Configuration
System.Configuration.ConfigurationManager.AppSettings.Item "Environment" <- "LOCAL" """)
Assert.Empty(errors)
match result with
| Ok(_) -> ()
| Error(ex) -> Assert.True(true, "expected no failures")
[<Theory>]
[<InlineData("""#i""", "input.fsx (1,1)-(1,3) interactive warning Invalid directive '#i '")>] // No argument
[<InlineData("""#i "" """, "input.fsx (1,1)-(1,6) interactive error #i is not supported by the registered PackageManagers")>] // empty argument
[<InlineData("""#i " " """, "input.fsx (1,1)-(1,14) interactive error #i is not supported by the registered PackageManagers")>] // whitespace only argument
member _.``Script with #i syntax errors fail``(code, error0) =
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.NotEmpty(errors)
Assert.Equal(errors.[0].ToString(), error0)
[<Theory>]
[<InlineData("""#i " """, // Single quote
"input.fsx (1,4)-(1,5) parse error End of file in string begun at or before here",
"input.fsx (1,1)-(1,6) interactive warning Invalid directive '#i '")>]
member _.``Script with more #i syntax errors fail``(code, error0, error1) =
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.NotEmpty(errors)
Assert.Equal(errors.Length, 2)
Assert.Equal(error0, errors.[0].ToString())
Assert.Equal(error1, errors.[1].ToString())
[<Theory>]
[<InlineData("""#i "Obviously I am not a package manager" """,
"input.fsx (1,1)-(1,42) interactive error #i is not supported by the registered PackageManagers")>]
member _.``Script with #i and no package manager specified``(code, error0) =
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.NotEmpty(errors)
Assert.Equal(errors.Length, 1)
Assert.Equal(errors.[0].ToString(), error0)
[<Theory>]
[<InlineData("""#i "nuget:foo" """,
"input.fsx (1,1)-(1,15) interactive error Invalid URI: The format of the URI could not be determined.")>]
member _.``Script with #i and forgot to add quotes``(code, error) =
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.NotEmpty(errors)
Assert.Equal(1, errors.Length)
Assert.Equal(error, errors.[0].ToString())
[<Fact>]
member _.``#i to a directory that exists``() =
let path = Path.GetTempPath()
let code = sprintf "#i @\"nuget:%s\" " path
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.Empty(errors)
Assert.Equal(0, errors.Length)
[<Fact>]
member _.``#i to a directory that doesn't exist``() =
let path =
let newGuid() = Guid.NewGuid().ToString().Replace("{", "").Replace("}", "")
Path.Combine(Path.GetTempPath(), newGuid(), newGuid())
let code = sprintf "#i @\"nuget:%s\"" path
let error = sprintf "interactive error The source directory '%s' not found" path
use script = new FSharpScript()
let result, errors = script.Eval(code)
Assert.NotEmpty(errors)
Assert.Equal(1, errors.Length)
Assert.True(errors.[0].ToString().EndsWith(error))
/// Native dll resolution is not implemented on desktop
#if NETSTANDARD
[<Fact>]
member __.``ML - use assembly with native dependencies``() =
let code = @"
#r ""nuget:Microsoft.ML,version=1.4.0-preview""
#r ""nuget:Microsoft.ML.AutoML,version=0.16.0-preview""
#r ""nuget:Microsoft.Data.Analysis,version=0.4.0""
open System
open System.IO
open System.Linq
open Microsoft.Data.Analysis
let Shuffle (arr:int[]) =
let rnd = Random()
for i in 0 .. arr.Length - 1 do
let r = i + rnd.Next(arr.Length - i)
let temp = arr.[r]
arr.[r] <- arr.[i]
arr.[i] <- temp
arr
let housingPath = ""housing.csv""
let housingData = DataFrame.LoadCsv(housingPath)
let randomIndices = (Shuffle(Enumerable.Range(0, (int (housingData.Rows.Count) - 1)).ToArray()))
let testSize = int (float (housingData.Rows.Count) * 0.1)
let trainRows = randomIndices.[testSize..]
let testRows = randomIndices.[..testSize]
let housing_train = housingData.[trainRows]
open Microsoft.ML
open Microsoft.ML.Data
open Microsoft.ML.AutoML
let mlContext = MLContext()
let experiment = mlContext.Auto().CreateRegressionExperiment(maxExperimentTimeInSeconds = 15u)
let result = experiment.Execute(housing_train, labelColumnName = ""median_house_value"")
let details = result.RunDetails
printfn ""%A"" result
123
"
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let opt = script.Eval(code) |> getValue
let value = opt.Value
Assert.Equal(123, value.ReflectionValue :?> int32)
#endif
[<Fact>]
member __.``Eval script with package manager invalid key``() =
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let result, _errors = script.Eval(@"#r ""nugt:FSharp.Data""")
match result with
| Ok(_) -> Assert.False(true, "expected a failure")
| Error(ex) -> Assert.IsAssignableFrom(typeof<FsiCompilationException>, ex)
[<Fact>]
member __.``Eval script with invalid PackageName should fail immediately``() =
use output = new RedirectConsoleOutput()
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let mutable found = 0
let outp = System.Collections.Generic.List<string>()
output.OutputProduced.Add(
fun line ->
if line.Contains("error NU1101:") && line.Contains("FSharp.Really.Not.A.Package") then
found <- found + 1
outp.Add(line))
let result, errors = script.Eval("""#r "nuget:FSharp.Really.Not.A.Package" """)
Assert.True( (found = 0), "Did not expect to see output contains 'error NU1101:' and 'FSharp.Really.Not.A.Package'")
Assert.True( errors |> Seq.exists (fun error -> error.Message.Contains("error NU1101:")), "Expect to error containing 'error NU1101:'")
Assert.True( errors |> Seq.exists (fun error -> error.Message.Contains("FSharp.Really.Not.A.Package")), "Expect to error containing 'FSharp.Really.Not.A.Package'")
[<Fact>]
member __.``Eval script with invalid PackageName should fail immediately and resolve one time only``() =
use output = new RedirectConsoleOutput()
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let mutable foundResolve = 0
output.OutputProduced.Add (fun line -> if line.Contains("error NU1101:") then foundResolve <- foundResolve + 1)
let result, errors =
script.Eval("""
#r "nuget:FSharp.Really.Not.A.Package"
#r "nuget:FSharp.Really.Not.Another.Package"
""")
Assert.True( (foundResolve = 0), (sprintf "Did not expected to see 'error NU1101:' in output" ))
Assert.Equal(2, (errors |> Seq.filter (fun error -> error.Message.Contains("error NU1101:")) |> Seq.length))
Assert.Equal(1, (errors |> Seq.filter (fun error -> error.Message.Contains("FSharp.Really.Not.A.Package")) |> Seq.length))
Assert.Equal(1, (errors |> Seq.filter (fun error -> error.Message.Contains("FSharp.Really.Not.Another.Package")) |> Seq.length))
[<Fact>]
member __.``ML - use assembly with ref dependencies``() =
let code = """
#r "nuget:Microsoft.ML.OnnxTransformer,1.4.0"
#r "nuget:System.Memory,4.5.4"
open System
open System.Numerics.Tensors
let inputValues = [| 12.0; 10.0; 17.0; 5.0 |]
let tInput = new DenseTensor<float>(inputValues.AsMemory(), new ReadOnlySpan<int>([|4|]))
tInput.Length
"""
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let opt = script.Eval(code) |> getValue
let value = opt.Value
Assert.Equal(4L, downcast value.ReflectionValue)
[<Fact>]
member __.``System.Device.Gpio - Ensure we reference the runtime version of the assembly``() =
let code = """
#r "nuget:System.Device.Gpio, 1.0.0"
typeof<System.Device.Gpio.GpioController>.Assembly.Location
"""
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let opt = script.Eval(code) |> getValue
let value = opt.Value
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
Assert.True( (value.ReflectionValue :?> string).EndsWith(@"runtimes\win\lib\netstandard2.0\System.Device.Gpio.dll") )
else if RuntimeInformation.IsOSPlatform(OSPlatform.Linux) then
Assert.True( (value.ReflectionValue :?> string).EndsWith(@"runtimes/linux/lib/netstandard2.0/System.Device.Gpio.dll") )
else
// Only Windows/Linux supported.
()
[<Fact>]
member __.``Simple pinvoke should not be impacted by native resolver``() =
let code = @"
open System
open System.Runtime.InteropServices
module Imports =
[<DllImport(""kernel32.dll"")>]
extern uint32 GetCurrentProcessId()
[<DllImport(""c"")>]
extern uint32 getpid()
// Will throw exception if fails
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
printfn ""Current process: %d"" (Imports.GetCurrentProcessId())
else
printfn ""Current process: %d"" (Imports.getpid())
123
"
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let opt = script.Eval(code) |> getValue
let value = opt.Value
Assert.Equal(123, value.ReflectionValue :?> int32)
[<Fact(Skip="This timing test fails in different environments. Skipping so that we don't assume an arbitrary CI environment has enough compute/etc. for what we need here.")>]
member _.``Evaluation can be cancelled``() =
use script = new FSharpScript()
let sleepTime = 10000L
let mutable result = None
let mutable wasCancelled = false
use tokenSource = new CancellationTokenSource()
let eval () =
try
result <- Some(script.Eval(sprintf "System.Threading.Thread.Sleep(%d)\n2" sleepTime, tokenSource.Token))
// if execution gets here (which it shouldn't), the value `2` will be returned
with
| :? OperationCanceledException -> wasCancelled <- true
let sw = Stopwatch.StartNew()
let evalTask = Task.Run(eval)
// cancel and wait for finish
tokenSource.Cancel()
evalTask.GetAwaiter().GetResult()
// ensure we cancelled and didn't complete the sleep or evaluation
Assert.True(wasCancelled)
Assert.Equal(sw.ElapsedMilliseconds, sleepTime)
Assert.Equal(None, result)
[<Fact>]
member _.``Values bound at the root trigger an event``() =
let mutable foundX = false
let mutable foundY = false
let mutable foundCount = 0
use script = new FSharpScript()
script.ValueBound
|> Event.add (fun (value, typ, name) ->
foundX <- foundX || (name = "x" && typ = typeof<int> && value :?> int = 1)
foundY <- foundY || (name = "y" && typ = typeof<int> && value :?> int = 2)
foundCount <- foundCount + 1)
let code = @"
let x = 1
let y = 2
"
script.Eval(code) |> ignoreValue
Assert.True(foundX)
Assert.True(foundY)
Assert.Equal(2, foundCount)
[<Fact>]
member _.``Values re-bound trigger an event``() =
let mutable foundXCount = 0
use script = new FSharpScript()
script.ValueBound
|> Event.add (fun (_value, typ, name) ->
if name = "x" && typ = typeof<int> then foundXCount <- foundXCount + 1)
script.Eval("let x = 1") |> ignoreValue
script.Eval("let x = 2") |> ignoreValue
Assert.Equal(2, foundXCount)
[<Fact>]
member _.``Nested let bindings don't trigger event``() =
let mutable foundInner = false
use script = new FSharpScript()
script.ValueBound
|> Event.add (fun (_value, _typ, name) ->
foundInner <- foundInner || name = "inner")
let code = @"
let x =
let inner = 1
()
"
script.Eval(code) |> ignoreValue
Assert.False(foundInner)
[<Fact>]
member _.``Script with nuget package that yields out of order dependencies works correctly``() =
// regression test for: https://github.com/dotnet/fsharp/issues/9217
let code = """
#r "nuget: FParsec,1.1.1"
open FParsec
let test p str =
match run p str with
| Success(result, _, _) ->
printfn "Success: %A" result
true
| Failure(errorMsg, _, _) ->
printfn "Failure: %s" errorMsg
false
test pfloat "1.234"
"""
use script = new FSharpScript(additionalArgs=[|"/langversion:preview"|])
let opt = script.Eval(code) |> getValue
let value = opt.Value
Assert.True(true = downcast value.ReflectionValue)