Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize the diagnostic engine if an error is thrown #637

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ public final class DiagnosticEngine {
}

public func finalize() {
workQueue.async { [weak self] in
// If the engine isn't around then return early
guard let self = self else { return }
workQueue.sync {
for consumer in self.consumers.sync({ $0.values }) {
try? consumer.finalize()
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftDocC/Infrastructure/DocumentationConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
mutating public func convert<OutputConsumer: ConvertOutputConsumer>(
outputConsumer: OutputConsumer
) throws -> (analysisProblems: [Problem], conversionProblems: [Problem]) {
defer {
diagnosticEngine.finalize()
}

// Unregister the current file data provider and all its bundles
// when running repeated conversions.
if let dataProvider = self.currentDataProvider {
Expand Down Expand Up @@ -397,8 +401,6 @@ public struct DocumentationConverter: DocumentationConverterProtocol {

context.linkResolutionMismatches.reportGatheredMismatchesIfEnabled()

diagnosticEngine.finalize()

return (analysisProblems: context.problems, conversionProblems: conversionProblems)
}

Expand Down
41 changes: 41 additions & 0 deletions Tests/SwiftDocCUtilitiesTests/ConvertActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,47 @@ class ConvertActionTests: XCTestCase {
XCTAssert(error is ErrorsEncountered, "Unexpected error type thrown by \(ConvertAction.self)")
}
}

func testWritesDiagnosticFileWhenThrowingError() throws {
let bundle = Folder(name: "unit-test.docc", content: [
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
CopyOfFile(original: symbolGraphFile, newName: "MyKit.symbols.json"),
TextFile(name: "Article.md", utf8Content: """
Bad title

This article has a malformed title and can't be analyzed, so it
produces one warning.
"""),
incompleteSymbolGraphFile,
])

let testDataProvider = try TestFileSystem(folders: [bundle, Folder.emptyHTMLTemplateDirectory])
let targetDirectory = URL(fileURLWithPath: testDataProvider.currentDirectoryPath)
.appendingPathComponent("target", isDirectory: true)

let diagnosticFile = try createTemporaryDirectory().appendingPathComponent("test-diagnostics.json")

var action = try ConvertAction(
documentationBundleURL: bundle.absoluteURL,
outOfProcessResolver: nil,
analyze: true,
targetDirectory: targetDirectory,
htmlTemplateDirectory: Folder.emptyHTMLTemplateDirectory.absoluteURL,
emitDigest: false,
currentPlatforms: nil,
dataProvider: testDataProvider,
fileManager: testDataProvider,
temporaryDirectory: createTemporaryDirectory(),
diagnosticLevel: "error",
diagnosticFilePath: diagnosticFile
)

XCTAssertFalse(FileManager.default.fileExists(atPath: diagnosticFile.path), "Diagnostic file doesn't exist before")
XCTAssertThrowsError(try action.performAndHandleResult()) { error in
XCTAssert(error is ErrorsEncountered, "Unexpected error type thrown by \(ConvertAction.self)")
}
XCTAssertTrue(FileManager.default.fileExists(atPath: diagnosticFile.path), "Diagnostic file exist after")
}

// Verifies setting convert inherit docs flag
func testConvertInheritDocsOption() throws {
Expand Down