1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
import SymbolKit
@testable import SwiftDocC
import SwiftDocCTestUtilities
class DocumentationContext_RootPageTests: XCTestCase {
func testNoSGFBundle() throws {
let tempFolderURL = try createTempFolder(content: [
Folder(name: "no-sgf-test.docc", content: [
// Root page for the collection
TextFile(name: "ReleaseNotes.md", utf8Content: """
# Release Notes
@Metadata {
@TechnologyRoot
}
Learn about recent changes.
## Topics
### Release Notes
- <doc:documentation/TechnologyX/ReleaseNotes-1.2>
"""),
// A curated article
TextFile(name: "ReleaseNotes 1.2.md", utf8Content: """
# Release Notes for version 1.2
Learn about changes in version 1.2
## See Also
- <doc:documentation/TechnologyX/ReleaseNotes>
"""),
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
]),
])
// Parse this test content
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
let dataProvider = try LocalFileSystemDataProvider(rootURL: tempFolderURL.appendingPathComponent("no-sgf-test.docc"))
try workspace.registerProvider(dataProvider)
// Verify all articles were loaded in the context
XCTAssertEqual(context.knownIdentifiers.count, 2)
// Verify /documentation/ReleaseNotes is a root node
XCTAssertEqual(context.rootModules.map({ $0.url.path }), ["/documentation/ReleaseNotes"])
// Verify the root was crawled
XCTAssertEqual(context.topicGraph.edges[ResolvedTopicReference(bundleIdentifier: "com.test.example", path: "/documentation/ReleaseNotes", sourceLanguage: .swift)]?.map({ $0.url.path }),
["/documentation/TestBundle/ReleaseNotes-1.2"])
}
func testWarnForSidecarRootPage() throws {
let tempFolderURL = try createTempFolder(content: [
Folder(name: "no-sgf-test.docc", content: [
// Root page for the collection
TextFile(name: "ReleaseNotes.md", utf8Content: """
# Release Notes
@Metadata {
@TechnologyRoot
}
Learn about recent changes.
## Topics
### Release Notes
- <doc:documentation/TechnologyX/ReleaseNotes-1.2>
"""),
// A sidecar documentation file
TextFile(name: "MyClass.md", utf8Content: """
# ``ReleaseNotes/MyClass``
@Metadata {
@TechnologyRoot
}
"""),
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
]),
])
// Parse this test content
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
let dataProvider = try LocalFileSystemDataProvider(rootURL: tempFolderURL.appendingPathComponent("no-sgf-test.docc"))
try workspace.registerProvider(dataProvider)
// Verify that we emit a warning when trying to make a symbol a root page
let technologyRootProblem = try XCTUnwrap(context.problems.first(where: { $0.diagnostic.identifier == "org.swift.docc.UnexpectedTechnologyRoot" }))
XCTAssertEqual(technologyRootProblem.diagnostic.source, tempFolderURL.appendingPathComponent("no-sgf-test.docc").appendingPathComponent("MyClass.md"))
XCTAssertEqual(technologyRootProblem.diagnostic.range?.lowerBound.line, 3)
let solution = try XCTUnwrap(technologyRootProblem.possibleSolutions.first)
XCTAssertEqual(solution.replacements.first?.range.lowerBound.line, 3)
XCTAssertEqual(solution.replacements.first?.range.upperBound.line, 3)
}
func testSingleArticleWithoutTechnologyRootDirective() throws {
let tempFolderURL = try createTempFolder(content: [
Folder(name: "Something.docc", content: [
TextFile(name: "Article.md", utf8Content: """
# My article
A regular article without an explicit `@TechnologyRoot` directive.
""")
]),
])
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
let dataProvider = try LocalFileSystemDataProvider(rootURL: tempFolderURL)
try workspace.registerProvider(dataProvider)
XCTAssertEqual(context.knownPages.map(\.absoluteString), ["doc://Something/documentation/Article"])
XCTAssertEqual(context.rootModules.map(\.absoluteString), ["doc://Something/documentation/Article"])
XCTAssertEqual(context.problems.count, 0)
}
func testMultipleArticlesWithoutTechnologyRootDirective() throws {
let tempFolderURL = try createTempFolder(content: [
Folder(name: "Something.docc", content: [
TextFile(name: "First.md", utf8Content: """
# My first article
A regular article without an explicit `@TechnologyRoot` directive.
"""),
TextFile(name: "Second.md", utf8Content: """
# My second article
Another regular article without an explicit `@TechnologyRoot` directive.
"""),
TextFile(name: "Third.md", utf8Content: """
# My third article
Yet another regular article without an explicit `@TechnologyRoot` directive.
"""),
]),
])
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
let dataProvider = try LocalFileSystemDataProvider(rootURL: tempFolderURL)
try workspace.registerProvider(dataProvider)
XCTAssertEqual(context.knownPages.map(\.absoluteString).sorted(), [
"doc://Something/documentation/Something", // A synthesized root
"doc://Something/documentation/Something/First",
"doc://Something/documentation/Something/Second",
"doc://Something/documentation/Something/Third",
])
XCTAssertEqual(context.rootModules.map(\.absoluteString), ["doc://Something/documentation/Something"], "If no single article is a clear root, the root page is synthesized")
XCTAssertEqual(context.problems.count, 0)
}
func testMultipleArticlesWithoutTechnologyRootDirectiveWithOneMatchingTheCatalogName() throws {
let tempFolderURL = try createTempFolder(content: [
Folder(name: "Something.docc", content: [
TextFile(name: "Something.md", utf8Content: """
# Some article
A regular article without an explicit `@TechnologyRoot` directive.
The name of this article file matches the name of the catalog.
"""),
TextFile(name: "Second.md", utf8Content: """
# My second article
Another regular article without an explicit `@TechnologyRoot` directive.
"""),
TextFile(name: "Third.md", utf8Content: """
# My third article
Yet another regular article without an explicit `@TechnologyRoot` directive.
"""),
]),
])
let workspace = DocumentationWorkspace()
let context = try DocumentationContext(dataProvider: workspace)
let dataProvider = try LocalFileSystemDataProvider(rootURL: tempFolderURL)
try workspace.registerProvider(dataProvider)
XCTAssertEqual(context.knownPages.map(\.absoluteString).sorted(), [
"doc://Something/documentation/Something", // This article became the root
"doc://Something/documentation/Something/Second",
"doc://Something/documentation/Something/Third",
])
XCTAssertEqual(context.rootModules.map(\.absoluteString), ["doc://Something/documentation/Something"])
XCTAssertEqual(context.problems.count, 0)
}
}
|