@@ -32,6 +32,76 @@ module CompilerAssert =
3232
3333 let private config = TestFramework.initializeSuite ()
3434
35+
36+ // Do a one time dotnet sdk build to compute the proper set of reference assemblies to pass to the compiler
37+ #if ! NETCOREAPP
38+ #else
39+ let projectFile = """
40+ <Project Sdk="Microsoft.NET.Sdk">
41+
42+ <PropertyGroup>
43+ <OutputType>Exe</OutputType>
44+ <TargetFramework>netcoreapp2.1</TargetFramework>
45+ </PropertyGroup>
46+
47+ <ItemGroup><Compile Include="Program.fs" /></ItemGroup>
48+
49+ <Target Name="WriteFrameworkReferences" AfterTargets="AfterBuild">
50+ <WriteLinesToFile File="FrameworkReferences.txt" Lines="@(ReferencePath)" Overwrite="true" WriteOnlyWhenDifferent="true" />
51+ </Target>
52+
53+ </Project>"""
54+
55+ let programFs = """
56+ open System
57+
58+ [<EntryPoint>]
59+ let main argv = 0"""
60+
61+ let getNetCoreAppReferences =
62+ let mutable output = " "
63+ let mutable errors = " "
64+ let mutable cleanUp = true
65+ let projectDirectory = Path.Combine( Path.GetTempPath(), " netcoreapp2.1" , Path.GetRandomFileName())
66+ try
67+ try
68+ Directory.CreateDirectory( projectDirectory) |> ignore
69+ let projectFileName = Path.Combine( projectDirectory, " ProjectFile.fsproj" )
70+ let programFsFileName = Path.Combine( projectDirectory, " Program.fs" )
71+ let frameworkReferencesFileName = Path.Combine( projectDirectory, " FrameworkReferences.txt" )
72+
73+ File.WriteAllText( projectFileName, projectFile)
74+ File.WriteAllText( programFsFileName, programFs)
75+
76+ let pInfo = ProcessStartInfo ()
77+
78+ pInfo.FileName <- config.DotNetExe
79+ pInfo.Arguments <- " build"
80+ pInfo.WorkingDirectory <- projectDirectory
81+ pInfo.RedirectStandardOutput <- true
82+ pInfo.RedirectStandardError <- true
83+ pInfo.UseShellExecute <- false
84+
85+ let p = Process.Start( pInfo)
86+ p.WaitForExit()
87+
88+ output <- p.StandardOutput.ReadToEnd ()
89+ errors <- p.StandardError.ReadToEnd ()
90+ if not ( String.IsNullOrWhiteSpace errors) then Assert.Fail errors
91+
92+ if p.ExitCode <> 0 then Assert.Fail( sprintf " Program exited with exit code %d " p.ExitCode)
93+
94+ File.ReadLines( frameworkReferencesFileName) |> Seq.toArray
95+ with | e ->
96+ cleanUp <- false
97+ printfn " %s " output
98+ printfn " %s " errors
99+ raise ( new Exception ( sprintf " An error occured getting netcoreapp references: %A " e))
100+ finally
101+ if cleanUp then
102+ try Directory.Delete( projectDirectory) with | _ -> ()
103+ #endif
104+
35105 let private defaultProjectOptions =
36106 {
37107 ProjectFileName = " Z:\\ test.fsproj"
@@ -41,14 +111,7 @@ module CompilerAssert =
41111 OtherOptions = [| " --preferreduilang:en-US" ;|]
42112#else
43113 OtherOptions =
44- // Hack: Currently a hack to get the runtime assemblies for netcore in order to compile.
45- let assemblies =
46- typeof< obj>. Assembly.Location
47- |> Path.GetDirectoryName
48- |> Directory.EnumerateFiles
49- |> Seq.toArray
50- |> Array.filter ( fun x -> x.ToLowerInvariant() .Contains( " system." ))
51- |> Array.map ( fun x -> sprintf " -r:%s " x)
114+ let assemblies = getNetCoreAppReferences |> Array.map ( fun x -> sprintf " -r:%s " x)
52115 Array.append [| " --preferreduilang:en-US" ; " --targetprofile:netcore" ; " --noframework" |] assemblies
53116#endif
54117 ReferencedProjects = [||]
@@ -60,7 +123,7 @@ module CompilerAssert =
60123 ExtraProjectInfo = None
61124 Stamp = None
62125 }
63-
126+
64127 let private gate = obj ()
65128
66129 let private compile isExe source f =
@@ -110,8 +173,6 @@ module CompilerAssert =
110173
111174 Assert.IsEmpty( typeCheckResults.Errors, sprintf " Type Check errors: %A " typeCheckResults.Errors)
112175
113-
114-
115176 let TypeCheckWithErrors ( source : string ) expectedTypeErrors =
116177 lock gate <| fun () ->
117178 let parseResults , fileAnswer = checker.ParseAndCheckFileInProject( " test.fs" , 0 , SourceText.ofString source, defaultProjectOptions) |> Async.RunSynchronously
@@ -141,7 +202,7 @@ module CompilerAssert =
141202 TypeCheckWithErrors ( source: string) [| expectedServerity, expectedErrorNumber, expectedErrorRange, expectedErrorMsg |]
142203
143204 let CompileExe ( source : string ) =
144- compile true source ( fun ( errors , _ ) ->
205+ compile true source ( fun ( errors , _ ) ->
145206 if errors.Length > 0 then
146207 Assert.Fail ( sprintf " Compile had warnings and/or errors: %A " errors))
147208
@@ -216,7 +277,7 @@ module CompilerAssert =
216277 ||> Seq.iter2 ( fun expectedErrorMessage errorMessage ->
217278 Assert.AreEqual( expectedErrorMessage, errorMessage)
218279 )
219-
280+
220281 let ParseWithErrors ( source : string ) expectedParseErrors =
221282 let parseResults = checker.ParseFile( " test.fs" , SourceText.ofString source, FSharpParsingOptions.Default) |> Async.RunSynchronously
222283
0 commit comments