Skip to content

Commit a31b72e

Browse files
committed
Tests: set test source for range debug printing
1 parent 0dbd3cd commit a31b72e

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

src/Compiler/Utilities/range.fs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System
77
open System.IO
88
open System.Collections.Concurrent
99
open System.Collections.Generic
10+
open System.Text
1011
open Microsoft.FSharp.Core.Printf
1112
open Internal.Utilities.Library
1213
open Internal.Utilities.Library.Extras.Bits
@@ -261,6 +262,9 @@ module FileIndex =
261262
let startupFileName = "startup"
262263
let commandLineArgsFileName = "commandLineArgs"
263264

265+
let mutable testSources: ConcurrentDictionary<string, string> =
266+
ConcurrentDictionary()
267+
264268
[<RequireQualifiedAccess>]
265269
module internal LineDirectives =
266270

@@ -329,6 +333,9 @@ type Range(code1: int64, code2: int64) =
329333

330334
member m.FileName = fileOfFileIndex m.FileIndex
331335

336+
member internal m.ShortFileName =
337+
Path.GetFileName(fileOfFileIndex m.FileIndex) |> nonNull
338+
332339
member m.ApplyLineDirectives() =
333340
match LineDirectives.store.TryFind m.FileIndex with
334341
| None -> m
@@ -376,32 +383,42 @@ type Range(code1: int64, code2: int64) =
376383
member _.Code2 = code2
377384

378385
member m.DebugCode =
379-
let name = m.FileName
386+
let getRangeSubstring (m: range) (stream: Stream) =
387+
let endCol = m.EndColumn - 1
388+
let startCol = m.StartColumn - 1
389+
390+
stream.ReadLines()
391+
|> Seq.skip (m.StartLine - 1)
392+
|> Seq.take (m.EndLine - m.StartLine + 1)
393+
|> String.concat "\n"
394+
|> fun s -> s.Substring(startCol + 1, s.LastIndexOf("\n", StringComparison.Ordinal) + 1 - startCol + endCol)
395+
396+
match testSources.TryGetValue(m.FileName) with
397+
| true, source ->
398+
use stream = new MemoryStream(Encoding.UTF8.GetBytes(source + "\n"))
399+
getRangeSubstring m stream
400+
| _ ->
380401

381-
if
382-
name = unknownFileName
383-
|| name = startupFileName
384-
|| name = commandLineArgsFileName
385-
then
386-
name
387-
else
402+
let name = m.FileName
388403

389-
try
390-
let endCol = m.EndColumn - 1
391-
let startCol = m.StartColumn - 1
404+
if
405+
name = unknownFileName
406+
|| name = startupFileName
407+
|| name = commandLineArgsFileName
408+
then
409+
name
410+
else
392411

393-
if FileSystem.IsInvalidPathShim m.FileName then
394-
"path invalid: " + m.FileName
395-
elif not (FileSystem.FileExistsShim m.FileName) then
396-
"nonexistent file: " + m.FileName
397-
else
398-
FileSystem.OpenFileForReadShim(m.FileName).ReadLines()
399-
|> Seq.skip (m.StartLine - 1)
400-
|> Seq.take (m.EndLine - m.StartLine + 1)
401-
|> String.concat "\n"
402-
|> fun s -> s.Substring(startCol + 1, s.LastIndexOf("\n", StringComparison.Ordinal) + 1 - startCol + endCol)
403-
with e ->
404-
e.ToString()
412+
try
413+
if FileSystem.IsInvalidPathShim m.FileName then
414+
"path invalid: " + m.FileName
415+
elif not (FileSystem.FileExistsShim m.FileName) then
416+
"nonexistent file: " + m.FileName
417+
else
418+
use stream = FileSystem.OpenFileForReadShim(m.FileName)
419+
getRangeSubstring m stream
420+
with e ->
421+
e.ToString()
405422

406423
member _.Equals(m2: range) =
407424
let code2 = code2 &&& ~~~(debugPointKindMask ||| isSyntheticMask)
@@ -613,3 +630,6 @@ module Range =
613630
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
614631
with _ ->
615632
mkRange file (mkPos 1 0) (mkPos 1 80)
633+
634+
let internal setTestSource path (source: string) =
635+
testSources.GetOrAdd(path, source) |> ignore

src/Compiler/Utilities/range.fsi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ type Range =
100100
/// The file index for the range
101101
member internal FileIndex: int
102102

103+
member internal DebugCode: string
104+
103105
/// The file name for the file of the range
104106
member FileName: string
105-
107+
108+
member internal ShortFileName: string
109+
106110
/// Apply the line directives to the range.
107111
member ApplyLineDirectives: unit -> range
108112

@@ -272,6 +276,8 @@ module Range =
272276
/// Equality comparer for range.
273277
val comparer: IEqualityComparer<range>
274278

279+
val internal setTestSource: path: string -> source: string -> unit
280+
275281
/// Functions related to converting between lines indexed at 0 and 1
276282
module Line =
277283

tests/FSharp.Compiler.Service.Tests/Common.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,14 @@ let mkTestFileAndOptions additionalArgs =
141141
fileName, options
142142

143143
let parseAndCheckFile fileName source options =
144+
Range.setTestSource fileName source
145+
144146
match checker.ParseAndCheckFileInProject(fileName, 0, SourceText.ofString source, options) |> Async.RunImmediate with
145147
| parseResults, FSharpCheckFileAnswer.Succeeded(checkResults) -> parseResults, checkResults
146148
| _ -> failwithf "Parsing aborted unexpectedly..."
147149

148150
let parseAndCheckScriptWithOptions (file:string, input, opts) =
151+
Range.setTestSource file input
149152

150153
#if NETCOREAPP
151154
let projectOptions =
@@ -194,6 +197,7 @@ let parseSourceCode (name: string, code: string) =
194197
let args = mkProjectCommandLineArgs(dllPath, [filePath])
195198
let options, _errors = checker.GetParsingOptionsFromCommandLineArgs(List.ofArray args)
196199
let parseResults = checker.ParseFile(filePath, SourceText.ofString code, options) |> Async.RunImmediate
200+
Range.setTestSource filePath code
197201
parseResults.ParseTree
198202

199203
let matchBraces (name: string, code: string) =

tests/FSharp.Test.Utilities/Compiler.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ module rec Compiler =
368368
let loadSourceFromFile path = getSource(TestType.Path path)
369369

370370
let fsFromString (source: SourceCodeFileKind): FSharpCompilationSource =
371+
source.GetSourceText |> Option.iter (Range.setTestSource source.GetSourceFileName)
372+
371373
{
372374
Source = source
373375
AdditionalSources = []
@@ -1308,7 +1310,7 @@ Actual:
13081310
/// After compile, rebuild and print FSharpDiagnostic[] exactly as before,
13091311
/// then diff against your existing .err.bsl files (few lines will shift).
13101312
let verifyBaseline (cResult: CompilationResult) : CompilationResult =
1311-
// 1) Grab the ErrorInfo list from the compile result…
1313+
// 1) Grab the ErrorInfo list from the compilation result
13121314
let errorInfos =
13131315
match cResult with
13141316
| CompilationResult.Success o

0 commit comments

Comments
 (0)