Skip to content

Commit a02ec4d

Browse files
authored
Merge pull request #11895 from dotnet/merges/main-to-release/dev17.0
2 parents 57e4e3c + 32d8989 commit a02ec4d

21 files changed

+200
-163
lines changed

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.Tests.fsproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net472;net5.0</TargetFrameworks>
5+
<TargetFrameworks Condition="'$(OS)' != 'Unix'">net472;net5.0</TargetFrameworks>
6+
<TargetFrameworks Condition="'$(OS)' == 'Unix'">net5.0</TargetFrameworks>
67
<NoWarn>$(NoWarn);44;75;</NoWarn>
78
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
89
<GenerateProgramFile>false</GenerateProgramFile>
@@ -79,6 +80,7 @@
7980
<ItemGroup>
8081
<PackageReference Include="Dotnet.ProjInfo" Version="0.37.0" />
8182
<ProjectReference Include="..\..\src\fsharp\FSharp.Compiler.Service\FSharp.Compiler.Service.fsproj" />
83+
<ProjectReference Include="..\..\tests\FSharp.Test.Utilities\FSharp.Test.Utilities.fsproj" />
8284
</ItemGroup>
8385

8486
</Project>

tests/FSharp.Test.Utilities/CompilerAssert.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ type CompilerAssert private () =
138138
errors, outputFilePath
139139

140140
static let compileAux isExe options source f : unit =
141-
let inputFilePath = Path.ChangeExtension(Path.GetTempFileName(), ".fs")
142-
let outputFilePath = Path.ChangeExtension (Path.GetTempFileName(), if isExe then ".exe" else ".dll")
141+
let inputFilePath = Path.ChangeExtension(tryCreateTemporaryFileName (), ".fs")
142+
let outputFilePath = Path.ChangeExtension (tryCreateTemporaryFileName (), if isExe then ".exe" else ".dll")
143143
try
144144
f (rawCompile inputFilePath outputFilePath isExe options source)
145145
finally
@@ -394,7 +394,7 @@ type CompilerAssert private () =
394394

395395
/// Assert that the given source code compiles with the `defaultProjectOptions`, with no errors or warnings
396396
static member CompileOfAst isExe source =
397-
let outputFilePath = Path.ChangeExtension (Path.GetTempFileName(), if isExe then "exe" else ".dll")
397+
let outputFilePath = Path.ChangeExtension (tryCreateTemporaryFileName (), if isExe then "exe" else ".dll")
398398
let parseOptions = { FSharpParsingOptions.Default with SourceFiles = [|"test.fs"|] }
399399

400400
let parseResults =

tests/FSharp.Test.Utilities/FSharp.Test.Utilities.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>net472;net5.0</TargetFrameworks>
4+
<TargetFrameworks Condition="'$(OS)' != 'Unix'">net472;net5.0</TargetFrameworks>
55
<TargetFrameworks Condition="'$(OS)' == 'Unix'">net5.0</TargetFrameworks>
66
<RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
77
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81</AssetTargetFallback>

tests/FSharp.Test.Utilities/TestFramework.fs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ open Scripting
1010
open NUnit.Framework
1111
open FSharp.Compiler.IO
1212

13+
let inline getTestsDirectory src dir = src ++ dir
14+
15+
// Temporary directory is TempPath + "/FSharp.Test.Utilities/" date ("yyy-MM-dd")
16+
// Throws exception if it Fails
17+
let tryCreateTemporaryDirectory () =
18+
let path = Path.GetTempPath()
19+
let now = DateTime.Now.ToString("yyyy-MM-dd")
20+
let directory = Path.Combine(path, "FSharp.Test.Utilities", now)
21+
Directory.CreateDirectory(directory).FullName
22+
23+
// Create a temporaryFileName -- newGuid is random --- there is no point validating the file alread exists because: threading and Path.ChangeExtension() is commonly used after this API
24+
let tryCreateTemporaryFileName () =
25+
let directory = tryCreateTemporaryDirectory ()
26+
let fileName = ("Temp-" + Guid.NewGuid().ToString() + ".tmp").Replace('-', '_')
27+
let filePath = Path.Combine(directory, fileName)
28+
filePath
29+
1330
[<RequireQualifiedAccess>]
1431
module Commands =
1532

@@ -178,7 +195,7 @@ module Commands =
178195
exec peverifyExe (sprintf "%s %s" (quotepath path) flags)
179196

180197
let createTempDir () =
181-
let path = Path.GetTempFileName ()
198+
let path = tryCreateTemporaryFileName ()
182199
File.Delete path
183200
Directory.CreateDirectory path |> ignore
184201
path

tests/FSharp.Test.Utilities/Utilities.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ let main argv = 0"""
285285
static member CreateILCompilation (source: string) =
286286
let compute =
287287
lazy
288-
let ilFilePath = Path.GetTempFileName ()
289-
let tmp = Path.GetTempFileName()
288+
let ilFilePath = tryCreateTemporaryFileName ()
289+
let tmp = tryCreateTemporaryFileName ()
290290
let dllFilePath = Path.ChangeExtension (tmp, ".dll")
291291
try
292292
File.WriteAllText (ilFilePath, source)

tests/fsharp/Compiler/Language/WitnessTests.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open NUnit.Framework
66
open FSharp.Test
77
open FSharp.Test.Utilities
88
open FSharp.Test.Compiler
9+
open TestFramework
910

1011
#if !NETCOREAPP
1112

tests/fsharp/Compiler/Service/MultiProjectTests.fs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ open FSharp.Compiler.CodeAnalysis
1313
open Microsoft.CodeAnalysis
1414
open Microsoft.CodeAnalysis.CSharp
1515
open FSharp.Compiler.Text
16+
open TestFramework
1617

1718
[<TestFixture>]
1819
module MultiProjectTests =
@@ -85,14 +86,14 @@ let test() =
8586
reraise()
8687

8788
let createOnDisk src =
88-
let tmpFilePath = Path.GetTempFileName()
89+
let tmpFilePath = tryCreateTemporaryFileName ()
8990
let tmpRealFilePath = Path.ChangeExtension(tmpFilePath, ".fs")
9091
try File.Delete(tmpFilePath) with | _ -> ()
9192
File.WriteAllText(tmpRealFilePath, src)
9293
tmpRealFilePath
9394

9495
let createOnDiskCompiledAsDll checker src =
95-
let tmpFilePath = Path.GetTempFileName()
96+
let tmpFilePath = tryCreateTemporaryFileName ()
9697
let tmpRealFilePath = Path.ChangeExtension(tmpFilePath, ".fs")
9798
try File.Delete(tmpFilePath) with | _ -> ()
9899
File.WriteAllText(tmpRealFilePath, src)
@@ -134,6 +135,7 @@ let test() =
134135
let ``Using compiler service, file referencing a DLL will correctly update when the referenced DLL file changes``() =
135136
let checker = CompilerAssert.Checker
136137

138+
// Create an assembly with the module Script1 and function x.
137139
let dllPath1 =
138140
createOnDiskCompiledAsDll checker
139141
"""
@@ -142,6 +144,7 @@ module Script1
142144
let x = 1
143145
"""
144146

147+
// Create script with that uses Script1 and function x
145148
let filePath1 =
146149
createOnDisk
147150
"""
@@ -159,12 +162,14 @@ let x = Script1.x
159162
ReferencedProjects = [||]
160163
SourceFiles = [|filePath1|] }
161164

162-
let checkProjectResults =
165+
// Verify that a script using Script1.x works
166+
let checkProjectResults1 =
163167
checker.ParseAndCheckProject(fsOptions1)
164168
|> Async.RunImmediate
165169

166-
Assert.IsEmpty(checkProjectResults.Diagnostics)
170+
Assert.IsEmpty(checkProjectResults1.Diagnostics)
167171

172+
// Create script with that uses Script1 and function x and function y
168173
updateFileOnDisk filePath1
169174
"""
170175
module Script2
@@ -173,12 +178,14 @@ let x = Script1.x
173178
let y = Script1.y
174179
"""
175180

176-
let checkProjectResults =
181+
// Verify that a script using Script1.x and Script1.y fails
182+
let checkProjectResults2 =
177183
checker.ParseAndCheckProject(fsOptions1)
178184
|> Async.RunImmediate
179185

180-
Assert.IsNotEmpty(checkProjectResults.Diagnostics)
186+
Assert.IsNotEmpty(checkProjectResults2.Diagnostics)
181187

188+
// Create an assembly with the module Script1 and function x and function y
182189
updateCompiledDllOnDisk checker dllPath1
183190
"""
184191
module Script1
@@ -187,11 +194,12 @@ let x = 1
187194
let y = 1
188195
"""
189196

190-
let checkProjectResults =
197+
// Verify that a script using Script1.x and Script1.y fails
198+
let checkProjectResults3 =
191199
checker.ParseAndCheckProject(fsOptions1)
192200
|> Async.RunImmediate
193201

194-
Assert.IsEmpty(checkProjectResults.Diagnostics)
202+
Assert.IsEmpty(checkProjectResults3.Diagnostics)
195203

196204
finally
197205
try File.Delete(dllPath1) with | _ -> ()

tests/fsharp/TypeProviderTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let FSC_BASIC = FSC_OPT_PLUS_DEBUG
3333
let FSI_BASIC = FSI_FILE
3434
#endif
3535

36-
let inline getTestsDirectory dir = FSharp.Test.Utilities.getTestsDirectory __SOURCE_DIRECTORY__ dir
36+
let inline getTestsDirectory dir = getTestsDirectory __SOURCE_DIRECTORY__ dir
3737
let testConfig = getTestsDirectory >> testConfig
3838

3939
[<Test>]

tests/fsharp/single-test.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ let singleTestBuildAndRunCore cfg copyFiles p languageVersion =
368368
let singleTestBuildAndRunAux cfg p =
369369
singleTestBuildAndRunCore cfg "" p "latest"
370370

371+
371372
let singleTestBuildAndRunWithCopyDlls cfg copyFiles p =
372373
singleTestBuildAndRunCore cfg copyFiles p "latest"
373374

@@ -377,6 +378,7 @@ let singleTestBuildAndRun dir p =
377378

378379
let singleTestBuildAndRunVersion dir p version =
379380
let cfg = testConfig dir
381+
380382
singleTestBuildAndRunCore cfg "" p version
381383

382384
let singleVersionedNegTest (cfg: TestConfig) version testname =
@@ -450,4 +452,6 @@ let singleVersionedNegTest (cfg: TestConfig) version testname =
450452
log "***** %s.vserr %s differed: a bug or baseline may need updating" testname VSBSLFILE
451453
failwithf "%s.err %s.bsl differ; %A; %s.vserr %s differ; %A" testname testname l1 testname VSBSLFILE l2
452454

453-
let singleNegTest (cfg: TestConfig) testname = singleVersionedNegTest (cfg: TestConfig) "" testname
455+
456+
let singleNegTest (cfg: TestConfig) testname =
457+
singleVersionedNegTest (cfg: TestConfig) "" testname

tests/fsharp/tests.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let FSI_BASIC = FSI_FILE
3232
#endif
3333
// ^^^^^^^^^^^^ To run these tests in F# Interactive , 'build net40', then send this chunk, then evaluate body of a test ^^^^^^^^^^^^
3434

35-
let inline getTestsDirectory dir = FSharp.Test.Utilities.getTestsDirectory __SOURCE_DIRECTORY__ dir
35+
let inline getTestsDirectory dir = getTestsDirectory __SOURCE_DIRECTORY__ dir
3636
let singleTestBuildAndRun = getTestsDirectory >> singleTestBuildAndRun
3737
let singleTestBuildAndRunVersion = getTestsDirectory >> singleTestBuildAndRunVersion
3838
let testConfig = getTestsDirectory >> testConfig
@@ -981,8 +981,8 @@ module CoreTests =
981981

982982
let fsc_flags_errors_ok = ""
983983

984-
let rawFileOut = Path.GetTempFileName()
985-
let rawFileErr = Path.GetTempFileName()
984+
let rawFileOut = tryCreateTemporaryFileName ()
985+
let rawFileErr = tryCreateTemporaryFileName ()
986986
``fsi <a >b 2>c`` "%s --nologo %s" fsc_flags_errors_ok flag ("test.fsx", rawFileOut, rawFileErr)
987987

988988
// REM REVIEW: want to normalise CWD paths, not suppress them.

0 commit comments

Comments
 (0)