Skip to content

Commit aa7da0c

Browse files
authored
Ensure that preview-specific errors are written to console (#1022)
1 parent 4f43500 commit aa7da0c

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

Sources/SwiftDocC/Infrastructure/Diagnostics/DiagnosticConsoleWriter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public final class DiagnosticConsoleWriter: DiagnosticFormattingConsumer {
6969
} else {
7070
let text = self.diagnosticFormatter.formattedDescription(for: problems)
7171
outputStream.write(text)
72+
outputStream.write("\n")
7273
}
7374
problems = [] // `flush()` is called more than once. Don't emit the same problems again.
7475
self.diagnosticFormatter.finalize()

Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public final class PreviewAction: Action, RecreatingContext {
5959

6060
/// A unique ID to access the action's preview server.
6161
let serverIdentifier = ProcessInfo.processInfo.globallyUniqueString
62-
63-
private let diagnosticEngine: DiagnosticEngine
6462

6563
/// Creates a new preview action from the given parameters.
6664
///
@@ -89,9 +87,7 @@ public final class PreviewAction: Action, RecreatingContext {
8987
self.createConvertAction = createConvertAction
9088
self.convertAction = try createConvertAction()
9189
self.workspace = workspace
92-
let engine = self.convertAction.diagnosticEngine
93-
self.diagnosticEngine = engine
94-
self.context = try context ?? DocumentationContext(dataProvider: workspace, diagnosticEngine: engine)
90+
self.context = try context ?? DocumentationContext(dataProvider: workspace, diagnosticEngine: self.convertAction.diagnosticEngine)
9591
self.printHTMLTemplatePath = printTemplatePath
9692
}
9793

@@ -155,7 +151,10 @@ public final class PreviewAction: Action, RecreatingContext {
155151
try servers[serverIdentifier]!.start()
156152
previewResult = ActionResult(didEncounterError: false)
157153
} catch {
154+
let diagnosticEngine = convertAction.diagnosticEngine
158155
diagnosticEngine.emit(.init(description: error.localizedDescription, source: nil))
156+
diagnosticEngine.flush()
157+
159158
// Stale server entry, remove it from the list
160159
servers.removeValue(forKey: serverIdentifier)
161160
previewResult = ActionResult(didEncounterError: true)

Tests/SwiftDocCTests/Diagnostics/DiagnosticConsoleWriterDefaultFormattingTest.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
4242
\u{001B}[1;31merror: \(summary)\u{001B}[0;0m
4343
\(explanation)
4444
\(expectedPath)
45+
4546
""")
4647
}
4748

@@ -56,6 +57,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
5657
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
5758
\(explanation)
5859
\(expectedPath)
60+
5961
""")
6062
}
6163

@@ -70,6 +72,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
7072
\u{001B}[1;39mnotice: \(summary)\u{001B}[0;0m
7173
\(explanation)
7274
\(expectedPath)
75+
7376
""")
7477
}
7578

@@ -84,6 +87,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
8487
\u{001B}[1;39mnote: \(summary)\u{001B}[0;0m
8588
\(explanation)
8689
\(expectedPath)
90+
8791
""")
8892
}
8993
}
@@ -106,6 +110,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
106110
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
107111
\(explanation)
108112
--> file.md:1:8-10:21
113+
109114
""")
110115
}
111116

@@ -140,6 +145,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
140145
\(explanation)
141146
/path/to/other/file.md:1:1: This is a note
142147
--> /path/to/file.md:1:8-10:21
148+
143149
""")
144150
}
145151

@@ -214,6 +220,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
214220
\u{001B}[1;33mwarning: Third diagnostic summary\u{001B}[0;0m
215221
Third diagnostic explanation
216222
--> /path/to/other/file.md
223+
217224
""")
218225
}
219226

@@ -243,6 +250,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
243250
44 + This section link refers to this section itself: \u{001B}[1;32m<doc:/tutorials/Test-Bundle/TestTutorial#Create-a-New-AR-Project-%F0%9F%92%BB>.\u{001B}[0;0m
244251
45 | This is an external link to Swift documentation: [Swift Documentation](https://swift.org/documentation/).
245252
46 | This section link refers to the next section in this file: <doc:/tutorials/Test-Bundle/TestTutorial#Initiate-ARKit-Plane-Detection>.
253+
246254
""")
247255
}
248256

@@ -288,6 +296,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
288296
3 + A short abstract \u{001B}[1;32mwith emoji 💻 in\u{001B}[0;0m it.
289297
4 |
290298
5 | @Metadata {
299+
291300
""")
292301
}
293302

@@ -332,6 +341,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
332341
| ╰─\u{001B}[1;39msuggestion: Solution summary\u{001B}[0;0m
333342
45 | This is an external link to Swift documentation: [Swift Documentation](https://swift.org/documentation/).
334343
46 | This section link refers to the next section in this file: <doc:/tutorials/Test-Bundle/TestTutorial#Initiate-ARKit-Plane-Detection>.
344+
335345
""")
336346
}
337347

@@ -355,6 +365,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
355365
| ╰─\u{001B}[1;39msuggestion: Solution summary\u{001B}[0;0m
356366
45 | This is an external link to Swift documentation: [Swift Documentation](https://swift.org/documentation/).
357367
46 | This section link refers to the next section in this file: <doc:/tutorials/Test-Bundle/TestTutorial#Initiate-ARKit-Plane-Detection>.
368+
358369
""")
359370
}
360371

@@ -386,6 +397,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
386397
| ╰─\u{001B}[1;39msuggestion: Solution summary\u{001B}[0;0m
387398
45 | This is an external link to Swift documentation: [Swift Documentation](https://swift.org/documentation/).
388399
46 | This section link refers to the next section in this file: <doc:/tutorials/Test-Bundle/TestTutorial#Initiate-ARKit-Plane-Detection>.
400+
389401
""")
390402
}
391403
}
@@ -431,6 +443,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
431443
1 + # \u{001B}[1;32mTitle\u{001B}[0;0m
432444
2 |
433445
3 | A very short article with only an abstract.
446+
434447
""")
435448

436449
// Highlight the "short" word on line 3
@@ -441,6 +454,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
441454
1 | # Title
442455
2 |
443456
3 + A very \u{001B}[1;32mshort\u{001B}[0;0m article with only an abstract.
457+
444458
""")
445459

446460
// Extend the highlight beyond the end of that line
@@ -451,6 +465,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
451465
1 | # Title
452466
2 |
453467
3 + A very \u{001B}[1;32mshort article with only an abstract.\u{001B}[0;0m
468+
454469
""")
455470

456471
// Extend the highlight beyond the start of that line
@@ -461,34 +476,39 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
461476
1 | # Title
462477
2 |
463478
3 + \u{001B}[1;32mA very short\u{001B}[0;0m article with only an abstract.
479+
464480
""")
465481

466482
// Highlight a line before the start of the file
467483
XCTAssertEqual(try logMessageFor(start: (line: -4, column: 1), end: (line: -4, column: 5)), """
468484
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
469485
\(explanation)
470486
--> Something.docc/Article.md:1:1-1:5
487+
471488
""")
472489

473490
// Highlight a line after the end of the file
474491
XCTAssertEqual(try logMessageFor(start: (line: 100, column: 1), end: (line: 100, column: 5)), """
475492
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
476493
\(explanation)
477494
--> Something.docc/Article.md:100:1-100:5
495+
478496
""")
479497

480498
// Extended the highlighted lines before the start of the file
481499
XCTAssertEqual(try logMessageFor(start: (line: -4, column: 1), end: (line: 1, column: 5)), """
482500
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
483501
\(explanation)
484502
--> Something.docc/Article.md:1:1-1:5
503+
485504
""")
486505

487506
// Extended the highlighted lines after the end of the file
488507
XCTAssertEqual(try logMessageFor(start: (line: 1, column: 1), end: (line: 100, column: 5)), """
489508
\u{001B}[1;33mwarning: \(summary)\u{001B}[0;0m
490509
\(explanation)
491510
--> Something.docc/Article.md:1:1-100:5
511+
492512
""")
493513
}
494514

@@ -522,6 +542,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
522542
suggestion:
523543
0 + var slothName = \"slothy\"
524544
1 + var slothDiet = .vegetarian
545+
525546
"""
526547
)
527548

@@ -554,6 +575,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
554575
suggestion:
555576
0 + var slothName = SlothGenerator().generateName()
556577
1 + var slothDiet = SlothGenerator().generateDiet()
578+
557579
"""
558580
)
559581

@@ -589,6 +611,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
589611
suggestion:
590612
0 + var beeName = "Bee"
591613
1 + var beeDiet = .vegetarian
614+
592615
"""
593616
)
594617
}

Tests/SwiftDocCTests/Model/ParametersAndReturnValidatorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ class ParametersAndReturnValidatorTests: XCTestCase {
685685
})
686686

687687
context.diagnosticEngine.flush()
688-
return logStorage.text
688+
return logStorage.text.trimmingCharacters(in: .newlines)
689689
}
690690

691691
private let start = SymbolGraph.LineList.SourceRange.Position(line: 7, character: 6) // an arbitrary non-zero start position

Tests/SwiftDocCTests/Utility/ListItemExtractorTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class ListItemExtractorTests: XCTestCase {
325325
19 | ```
326326
20 |
327327
21 + > Warning: Inner aside, with ``\u{001B}[1;32mThirdNotFoundSymbol\u{001B}[0;0m`` link
328+
328329
""", file: file, line: line)
329330
}
330331

@@ -391,6 +392,7 @@ class ListItemExtractorTests: XCTestCase {
391392
20 | ```
392393
21 |
393394
22 + > Warning: Inner aside, with ``\u{001B}[1;32mThirdNotFoundSymbol\u{001B}[0;0m`` link
395+
394396
""", file: file, line: line)
395397
}
396398

Tests/SwiftDocCUtilitiesTests/PreviewActionIntegrationTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ class PreviewActionIntegrationTests: XCTestCase {
315315
}
316316
// Start watching the source and get the initial (successful) state.
317317
do {
318-
let engine = preview.convertAction.diagnosticEngine
319-
320318
// Wait for watch to produce output.
321319
let logOutputExpectation = expectation(description: "Did produce log output")
322320
let logChecker = OutputChecker(fileURL: pipeURL, expectation: logOutputExpectation) { output in
@@ -334,7 +332,7 @@ class PreviewActionIntegrationTests: XCTestCase {
334332
}
335333

336334
XCTAssertTrue(result.didEncounterError, "Did not find an error when running preview", file: file, line: line)
337-
XCTAssertNotNil(engine.problems.first(where: { problem -> Bool in
335+
XCTAssertNotNil(preview.convertAction.diagnosticEngine.problems.first(where: { problem -> Bool in
338336
DiagnosticConsoleWriter.formattedDescription(for: problem.diagnostic).contains(expectedErrorMessage)
339337
}), "Didn't find expected error message '\(expectedErrorMessage)'", file: file, line: line)
340338

0 commit comments

Comments
 (0)