Skip to content

Commit 032879d

Browse files
abelbraaksmabaronfel
authored andcommitted
Cleanup tests that use global state, ensure deletion of temp files
1 parent 7967b92 commit 032879d

1 file changed

Lines changed: 20 additions & 46 deletions

File tree

tests/service/ExprTests.fs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ module internal Utils =
3535
if Directory.Exists tempPath then ()
3636
else Directory.CreateDirectory tempPath |> ignore
3737

38-
/// Returns the filename part of a temp file name created with Path.GetTempFileName()
39-
/// and an added process id and thread id to ensure uniqueness between threads.
38+
/// Returns the filename part of a temp file name created with Path.GetTempFileName().
4039
let getTempFileName() =
4140
let tempFileName = Path.GetTempFileName()
4241
try
43-
let tempFile, tempExt = Path.GetFileNameWithoutExtension tempFileName, Path.GetExtension tempFileName
44-
let procId, threadId = Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId
45-
String.concat "" [tempFile; "_"; string procId; "_"; string threadId; tempExt] // ext includes dot
42+
let tempFileName = Path.GetFileName(tempFileName)
43+
tempFileName
4644
finally
4745
try
48-
// Since Path.GetTempFileName() creates a *.tmp file in the %TEMP% folder, we want to clean it up.
49-
// This also prevents a system to run out of available randomized temp files (the pool is only 64k large).
46+
// since Path.GetTempFileName() creates a *.tmp file in the %TEMP% folder, we want to clean it up
5047
File.Delete tempFileName
5148
with _ -> ()
5249

53-
/// Clean up after a test is run. If you need to inspect the create *.fs files, change this function to do nothing, or just break here.
50+
/// Clean up after a test is run. If you need to inspect the create *.fs files, change this function to do nothing.
5451
let cleanupTempFiles files =
5552
for fileName in files do
5653
try
@@ -70,18 +67,6 @@ module internal Utils =
7067
let getTempFilePathChangeExt tmp ext =
7168
Path.Combine(getTempPath(), Path.ChangeExtension(tmp, ext))
7269

73-
// This behaves slightly differently on Mono versions, 'null' is printed somethimes, 'None' other times
74-
// Presumably this is very small differences in Mono reflection causing F# printing to change behaviour
75-
// For now just disabling this test. See https://github.com/fsharp/FSharp.Compiler.Service/pull/766
76-
let filterHack l =
77-
l |> List.map (fun (s:string) ->
78-
// potential difference on Mono
79-
s.Replace("ILArrayShape [(Some 0, None)]", "ILArrayShape [(Some 0, null)]")
80-
// spacing difference when run locally in VS
81-
.Replace("I_ldelema (NormalAddress,false,ILArrayShape [(Some 0, null)],!0)]", "I_ldelema (NormalAddress, false, ILArrayShape [(Some 0, null)], !0)]")
82-
// local VS IDE vs CI env difference
83-
.Replace("Operators.Hash<Microsoft.FSharp.Core.string> (x)", "x.GetHashCode()"))
84-
8570
let rec printExpr low (e:FSharpExpr) =
8671
match e with
8772
| BasicPatterns.AddressOf(e1) -> "&"+printExpr 0 e1
@@ -708,7 +693,7 @@ let test{0}ToStringOperator (e1:{1}) = string e1
708693
709694
"""
710695

711-
[<Test>]
696+
/// This test is run in unison with its optimized counterpart below
712697
let ``Test Unoptimized Declarations Project1`` () =
713698
let wholeProjectResults = exprChecker.ParseAndCheckProject(snd Project1.options.Value) |> Async.RunSynchronously
714699

@@ -830,17 +815,17 @@ let ``Test Unoptimized Declarations Project1`` () =
830815

831816
printDeclarations None (List.ofSeq file1.Declarations)
832817
|> Seq.toList
833-
|> Utils.filterHack
834-
|> shouldPairwiseEqual (Utils.filterHack expected)
818+
|> filterHack
819+
|> shouldPairwiseEqual (filterHack expected)
835820

836821
printDeclarations None (List.ofSeq file2.Declarations)
837822
|> Seq.toList
838-
|> Utils.filterHack
839-
|> shouldPairwiseEqual (Utils.filterHack expected2)
823+
|> filterHack
824+
|> shouldPairwiseEqual (filterHack expected2)
840825

841826
()
842827

843-
[<Test>]
828+
/// This test is run in unison with its unoptimized counterpart below
844829
let ``Test Optimized Declarations Project1`` () =
845830
let wholeProjectResults = exprChecker.ParseAndCheckProject(snd Project1.options.Value) |> Async.RunSynchronously
846831

@@ -993,30 +978,24 @@ let ``Test Optimized and Unoptimized Declarations for Project1`` () =
993978

994979
let testOperators dnName fsName excludedTests expectedUnoptimized expectedOptimized =
995980

996-
/// File is placed in local user's %TEMP%\ExprTests folder
997-
let tempPath = Path.Combine(Path.GetTempPath(), "ExprTests")
998-
do
999-
if Directory.Exists tempPath then ()
1000-
else Directory.CreateDirectory tempPath |> ignore
1001-
1002-
let tempFileName = Path.GetFileName(Path.GetTempFileName())
1003-
let basePath = Path.Combine(tempPath, tempFileName)
1004-
let fileName = Path.ChangeExtension(basePath, ".fs")
1005-
let dllName = Path.ChangeExtension(basePath, ".dll")
1006-
let projFileName = Path.ChangeExtension(basePath, ".fsproj")
981+
let tempFileName = Utils.getTempFileName()
982+
let filePath = Utils.getTempFilePathChangeExt tempFileName ".fs"
983+
let dllPath =Utils.getTempFilePathChangeExt tempFileName ".dll"
984+
let projFilePath = Utils.getTempFilePathChangeExt tempFileName ".fsproj"
1007985

1008986
try
987+
createTempDir()
1009988
let source = System.String.Format(Project1.operatorTests, dnName, fsName)
1010989
let replace (s:string) r = s.Replace("let " + r, "// let " + r)
1011990
let fileSource = excludedTests |> List.fold replace source
1012-
File.WriteAllText(fileName, fileSource)
991+
File.WriteAllText(filePath, fileSource)
1013992

1014993
let args = [|
1015-
yield! mkProjectCommandLineArgsSilent (dllName, [fileName])
994+
yield! mkProjectCommandLineArgsSilent (dllPath, [filePath])
1016995
yield @"-r:System.Numerics.dll" // needed for some tests
1017996
|]
1018997

1019-
let options = checker.GetProjectOptionsFromCommandLineArgs (projFileName, args)
998+
let options = checker.GetProjectOptionsFromCommandLineArgs (projFilePath, args)
1020999

10211000
let wholeProjectResults = exprChecker.ParseAndCheckProject(options) |> Async.RunSynchronously
10221001

@@ -1043,13 +1022,8 @@ let testOperators dnName fsName excludedTests expectedUnoptimized expectedOptimi
10431022
|> shouldPairwiseEqual expectedOptimized
10441023

10451024
finally
1046-
try
1047-
// cleanup: only the source file is written to the temp dir.
1048-
File.Delete fileName
1049-
if Directory.GetFiles(tempPath) |> Array.isEmpty then
1050-
Directory.Delete tempPath
1025+
Utils.cleanupTempFiles [filePath; dllPath; projFilePath]
10511026

1052-
with _ -> ()
10531027
()
10541028

10551029
[<Test>]

0 commit comments

Comments
 (0)