Skip to content

Fix error when merging a single archive #1218

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -36,7 +36,10 @@ extension MergeAction {

func readRootNodeRenderReferencesIn(dataDirectory: URL) throws -> RootRenderReferences {
func inner(url: URL) throws -> [RootRenderReferences.Information] {
try fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
// Path might not exist (e.g. tutorials for a reference-only archive)
guard let contents = try? fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
else { return [] }
return try contents
.compactMap {
guard $0.pathExtension == "json" else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ struct MergeAction: AsyncAction {
let fromDirectory = archive.appendingPathComponent(directoryToCopy, isDirectory: true)
let toDirectory = targetURL.appendingPathComponent(directoryToCopy, isDirectory: true)

guard fileManager.directoryExists(atPath: fromDirectory.path) else { continue }

// Ensure that the destination directory exist in case the first archive didn't have that kind of pages.
// This is necessary when merging a reference-only archive with a tutorial-only archive.
try? fileManager.createDirectory(at: toDirectory, withIntermediateDirectories: false, attributes: nil)
Expand Down
60 changes: 51 additions & 9 deletions Tests/SwiftDocCUtilitiesTests/MergeActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,50 @@ class MergeActionTests: XCTestCase {
"doc://org.swift.test/documentation/second.json",
])
}


func testSingleReferenceOnlyArchiveMerging() async throws {
let fileSystem = try TestFileSystem(
folders: [
Folder(name: "Output.doccarchive", content: []),
Self.makeArchive(
name: "First",
documentationPages: [
"First",
"First/SomeClass",
"First/SomeClass/someProperty",
"First/SomeClass/someFunction(:_)",
],
tutorialPages: []
),
]
)

let logStorage = LogHandle.LogStorage()
let action = MergeAction(
archives: [
URL(fileURLWithPath: "/First.doccarchive"),
],
landingPageInfo: testLandingPageInfo,
outputURL: URL(fileURLWithPath: "/Output.doccarchive"),
fileManager: fileSystem
)

_ = try await action.perform(logHandle: .memory(logStorage))
XCTAssertEqual(logStorage.text, "", "The action didn't log anything")

let synthesizedRootNode = try fileSystem.renderNode(atPath: "/Output.doccarchive/data/documentation.json")
XCTAssertEqual(synthesizedRootNode.metadata.title, "Test Landing Page Name")
XCTAssertEqual(synthesizedRootNode.metadata.roleHeading, "Test Landing Page Kind")
XCTAssertEqual(synthesizedRootNode.topicSectionsStyle, .detailedGrid)
XCTAssertEqual(synthesizedRootNode.topicSections.flatMap { [$0.title].compactMap({ $0 }) + $0.identifiers }, [
// No title
"doc://org.swift.test/documentation/first.json",
])
XCTAssertEqual(synthesizedRootNode.references.keys.sorted(), [
"doc://org.swift.test/documentation/first.json",
])
}

func testErrorWhenArchivesContainOverlappingData() async throws {
let fileSystem = try TestFileSystem(
folders: [
Expand Down Expand Up @@ -953,14 +996,13 @@ class MergeActionTests: XCTestCase {
Output.doccarchive/
├─ data/
│ ├─ documentation.json
│ ├─ documentation/
│ │ ├─ first.json
│ │ ├─ first/
│ │ │ ╰─ article.json
│ │ ├─ second.json
│ │ ╰─ second/
│ │ ╰─ article.json
│ ╰─ tutorials/
│ ╰─ documentation/
│ ├─ first.json
│ ├─ first/
│ │ ╰─ article.json
│ ├─ second.json
│ ╰─ second/
│ ╰─ article.json
├─ downloads/
│ ├─ First/
│ ╰─ Second/
Expand Down