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

Fix warning when curating a module under a technology root (#735) #780

Merged
merged 6 commits into from
Jan 29, 2024
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
23 changes: 21 additions & 2 deletions Sources/SwiftDocC/Infrastructure/DocumentationCurator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,29 @@ struct DocumentationCurator {
(childDocumentationNode.kind == .article || childDocumentationNode.kind.isSymbol || childDocumentationNode.kind == .tutorial || childDocumentationNode.kind == .tutorialArticle) else {
continue
}

guard childDocumentationNode.kind != .module else {


// If a module has a path with a manual technology root there shouldn't
// be an error messsage. This is an if statement instead of a guard because
// when there's no warning we still curate the node
if childDocumentationNode.kind == .module {

func isTechnologyRoot(_ reference: ResolvedTopicReference) -> Bool {
guard let node = context.topicGraph.nodeWithReference(reference) else {return false}
return node.kind == .module && documentationNode.kind.isSymbol == false
}


let hasTechnologyRoot = isTechnologyRoot(nodeReference) || context.pathsTo(nodeReference).contains { path in
guard let root = path.first else {return false}
return isTechnologyRoot(root)
}

if !hasTechnologyRoot {
problems.append(Problem(diagnostic: Diagnostic(source: source(), severity: .warning, range: range(), identifier: "org.swift.docc.ModuleCuration", summary: "Linking to \((link.destination ?? "").singleQuoted) from a Topics group in \(nodeReference.absoluteString.singleQuoted) isn't allowed", explanation: "The former is a module, and modules only exist at the root"), possibleSolutions: []))
continue
}

}

// Verify we are not creating a graph cyclic relationship.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,83 @@ class DocumentationCuratorTests: XCTestCase {
)
}

func testModuleUnderTechnologyRoot() throws {
let (_, bundle, context) = try testBundleAndContext(copying: "SourceLocations") { url in
try """
# Root curating a module

@Metadata {
@TechnologyRoot
}

Curating a module from a technology root should not generated any warnings.

## Topics

- ``SourceLocations``

""".write(to: url.appendingPathComponent("Root.md"), atomically: true, encoding: .utf8)
}

let crawler = DocumentationCurator.init(in: context, bundle: bundle)
XCTAssert(context.problems.isEmpty, "Expected no problems. Found: \(context.problems.map(\.diagnostic.summary))")

guard let moduleNode = context.nodeWithSymbolIdentifier("SourceLocations"),
let pathToRoot = context.pathsTo(moduleNode.reference).first,
let root = pathToRoot.first else {

XCTFail("Module doesn't have technology root as a predecessor in its path")
return
}

XCTAssertEqual(root.path, "/documentation/Root")
XCTAssertEqual(crawler.problems.count, 0)

}

func testModuleUnderAncestorOfTechnologyRoot() throws {
let (_, bundle, context) = try testBundleAndContext(copying: "SourceLocations") { url in
try """
# Root with ancestor curatings a module

This is a root article that enables testing the behavior of it's ancestors.

@Metadata {
@TechnologyRoot
}

## Topics
- <doc:Ancestor>


""".write(to: url.appendingPathComponent("Root.md"), atomically: true, encoding: .utf8)

try """
# Ancestor of root

Linking to a module shouldn't raise errors due to this article being an ancestor of a technology root.

## Topics
- ``SourceLocations``

""".write(to: url.appendingPathComponent("Ancestor.md"), atomically: true, encoding: .utf8)
}

let _ = DocumentationCurator.init(in: context, bundle: bundle)
XCTAssert(context.problems.isEmpty, "Expected no problems. Found: \(context.problems.map(\.diagnostic.summary))")

guard let moduleNode = context.nodeWithSymbolIdentifier("SourceLocations"),
let pathToRoot = context.pathsTo(moduleNode.reference).first,
let root = pathToRoot.first else {

XCTFail("Module doesn't have technology root as a predecessor in its path")
return
}

XCTAssertEqual(root.path, "/documentation/Root")
}


func testSymbolLinkResolving() throws {
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
Expand Down