@@ -30,13 +30,54 @@ module rec Compiler =
3030 let shouldUpdateBaselines =
3131 Environment.GetEnvironmentVariable( " TEST_UPDATE_BSL" ) <> null
3232
33+ let private baselineFailureMessage ( expectedFile : string ) ( outFile : string ) ( diff : string ) =
34+ $""" Baseline mismatch for {expectedFile}
35+ to update the baseline:
36+ $ cp {outFile} {expectedFile}
37+ to compare:
38+ $ code --diff {outFile} {expectedFile}
39+ (or set TEST_UPDATE_BSL=1 and re-run to update the baseline automatically)
40+ {diff}"""
41+
42+ let private baselineOutputFile ( expectedFile : string ) =
43+ if Path.GetExtension( expectedFile) = " .bsl" then
44+ Path.ChangeExtension( expectedFile, " .out" )
45+ else
46+ expectedFile + " .out"
47+
48+ let checkBaselineWith ( compare : string -> string -> string option ) ( expected : string ) ( expectedFile : string ) =
49+ let outFile = baselineOutputFile expectedFile
50+ let baselineContent =
51+ if FileSystem.FileExistsShim expectedFile then File.ReadAllText expectedFile else " "
52+ let diff = compare baselineContent expected
53+
54+ match diff with
55+ | None ->
56+ if FileSystem.FileExistsShim outFile then
57+ FileSystem.FileDeleteShim outFile
58+ | Some diff ->
59+ if shouldUpdateBaselines then
60+ if FileSystem.FileExistsShim outFile then
61+ FileSystem.FileDeleteShim outFile
62+ File.WriteAllText( expectedFile, expected)
63+ else
64+ File.WriteAllText( outFile, expected)
65+
66+ Assert.True( false , baselineFailureMessage expectedFile outFile diff)
67+
68+ let checkBaseline ( expected : string ) ( expectedFile : string ) =
69+ let compare fileContent produced =
70+ let e = normalizeNewlines fileContent
71+ let a = normalizeNewlines produced
72+ if e = a then None else Some $" Expected:\n {e}\n Actual:\n {a}"
73+ checkBaselineWith compare expected expectedFile
74+
3375 [<AutoOpen>]
3476 type SourceUtilities () =
3577 static member getCurrentMethodName ( [<CallerMemberName; Optional; DefaultParameterValue( " " ) >] memberName : string ) = memberName
3678
3779 type BaselineFile =
3880 {
39- FilePath: string
4081 BslSource: string
4182 Content: string option
4283 }
@@ -251,8 +292,6 @@ module rec Compiler =
251292 | Some s -> s
252293 | None -> sourceFilePath + sourceBaselineSuffix + " .il.bsl"
253294
254- let fsOutFilePath = normalizePathSeparator ( Path.ChangeExtension( outputDirectoryPath ++ filename, " .err" ))
255- let ilOutFilePath = normalizePathSeparator ( Path.ChangeExtension( outputDirectoryPath ++ filename, " .il" ))
256295 let fsBslSource = readFileOrDefault fsBslFilePath
257296 let ilBslSource = readFileOrDefault ilBslFilePath
258297
@@ -262,8 +301,8 @@ module rec Compiler =
262301 Some
263302 {
264303 SourceFilename = Some sourceFilePath
265- FSBaseline = { FilePath = fsOutFilePath ; BslSource = fsBslFilePath; Content = fsBslSource }
266- ILBaseline = { FilePath = ilOutFilePath ; BslSource = ilBslFilePath; Content = ilBslSource }
304+ FSBaseline = { BslSource = fsBslFilePath; Content = fsBslSource }
305+ ILBaseline = { BslSource = ilBslFilePath; Content = ilBslSource }
267306 }
268307 Options = Compiler.defaultOptions
269308 OutputType = Library
@@ -1220,36 +1259,6 @@ module rec Compiler =
12201259 | _ -> failwith " FSI running only supports F#."
12211260
12221261
1223- let convenienceBaselineInstructions baseline expected actual =
1224- $""" to update baseline:
1225- $ cp {baseline.FilePath} {baseline.BslSource}
1226- to compare baseline:
1227- $ code --diff {baseline.FilePath} {baseline.BslSource}
1228- Expected:
1229- {expected}
1230- Actual:
1231- {actual}"""
1232- let updateBaseLineIfEnvironmentSaysSo baseline =
1233- if shouldUpdateBaselines then
1234- if FileSystem.FileExistsShim baseline.FilePath then
1235- FileSystem.CopyShim( baseline.FilePath, baseline.BslSource, true )
1236-
1237- let assertBaseline expected actual baseline fOnFail =
1238- if expected <> actual then
1239- fOnFail()
1240- updateBaseLineIfEnvironmentSaysSo baseline
1241- createBaselineErrors baseline actual
1242- Assert.True(( expected = actual), convenienceBaselineInstructions baseline expected actual)
1243- elif FileSystem.FileExistsShim baseline.FilePath then
1244- FileSystem.FileDeleteShim baseline.FilePath
1245-
1246-
1247- let private createBaselineErrors ( baselineFile : BaselineFile ) ( actualErrors : string ) : unit =
1248- printfn $" creating baseline error file for convenience: {baselineFile.FilePath}, expected: {baselineFile.BslSource}"
1249- let file = FileSystem.OpenFileForWriteShim( baselineFile.FilePath)
1250- file.SetLength( 0 )
1251- file.WriteAllText( actualErrors)
1252-
12531262 /// Turn our ErrorInfo back into a genuine FSharpDiagnostic
12541263 let private toFSharpDiagnostic ( ei : ErrorInfo ) : FSharpDiagnostic =
12551264
@@ -1309,19 +1318,8 @@ Actual:
13091318 match o.Compilation with
13101319 | FS fs -> fs
13111320 | _ -> failwith " verifyBaseline only supports F#"
1312- let expected =
1313- fsSource.Baseline.Value.FSBaseline.Content
1314- |> Option.defaultValue " "
1315- |> normalizeNewlines
1316-
13171321 // 4) Compare or update
1318- if expected <> formattedActual then
1319- // same update mechanism you already have:
1320- fsSource.CreateOutputDirectory()
1321- createBaselineErrors fsSource.Baseline.Value.FSBaseline formattedActual
1322- updateBaseLineIfEnvironmentSaysSo fsSource.Baseline.Value.FSBaseline
1323- let msg = convenienceBaselineInstructions fsSource.Baseline.Value.FSBaseline expected formattedActual
1324- Assert.True( false , msg)
1322+ checkBaseline formattedActual fsSource.Baseline.Value.FSBaseline.BslSource
13251323
13261324 // 5) Return the original result for fluent chaining
13271325 cResult
@@ -1387,14 +1385,8 @@ Actual:
13871385 | None -> String.Empty
13881386 let success , errorMsg , actualIL = ILChecker.verifyILAndReturnActual [] p [ expectedIL]
13891387
1390- if not success then
1391- // Failed try update baselines if required
1392- // If we are here then the il file has been produced we can write it back to the baseline location
1393- // if the environment variable TEST_UPDATE_BSL has been set
1394- updateBaseLineIfEnvironmentSaysSo baseline.ILBaseline
1395- createBaselineErrors baseline.ILBaseline actualIL
1396- let errorMsg = ( convenienceBaselineInstructions baseline.ILBaseline expectedIL actualIL) + errorMsg
1397- Assert.Fail( errorMsg)
1388+ let compare _ _ = if success then None else Some errorMsg
1389+ checkBaselineWith compare actualIL baseline.ILBaseline.BslSource
13981390
13991391 let verifyILBaseline ( compilationResult : CompilationResult ) : CompilationResult =
14001392 match compilationResult with
@@ -2011,22 +2003,9 @@ Actual:
20112003 |> String.Concat
20122004
20132005 let withResultsMatchingFile ( path : string ) ( result : CompilationResult ) =
2014- let expectedContent = File.ReadAllText( path) |> normalizeNewLines
2015- let actualErrors = renderToString result
2016-
2017- match Assert.shouldBeSameMultilineStringSets expectedContent actualErrors with
2018- | None -> ()
2019- | Some diff ->
2020- if shouldUpdateBaselines then
2021- File.WriteAllText( path, actualErrors)
2022-
2023- printfn $" {Path.GetFullPath path} \n {diff}"
2024- printfn " ==========================EXPECTED==========================="
2025- printfn " %s " expectedContent
2026- printfn " ===========================ACTUAL============================"
2027- printfn " %s " actualErrors
2028- Assert.True( String.IsNullOrEmpty( diff), path)
2029-
2006+ let compare fileContent produced =
2007+ Assert.shouldBeSameMultilineStringSets ( normalizeNewLines fileContent) produced
2008+ checkBaselineWith compare ( renderToString result) path
20302009 result
20312010
20322011 let checkCodes ( expected : int list ) ( selector : CompilationOutput -> ErrorInfo list ) ( result : CompilationResult ) : CompilationResult =
0 commit comments