Skip to content

Commit bb2cdc7

Browse files
authored
Fix error when merging a single archive (#1218)
* Fix error when merging a single archive * Prevent creation of empty directories in merge
1 parent cf6dde1 commit bb2cdc7

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

Sources/SwiftDocCUtilities/Action/Actions/Merge/MergeAction+SynthesizedLandingPage.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ extension MergeAction {
3636

3737
func readRootNodeRenderReferencesIn(dataDirectory: URL) throws -> RootRenderReferences {
3838
func inner(url: URL) throws -> [RootRenderReferences.Information] {
39-
try fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
39+
// Path might not exist (e.g. tutorials for a reference-only archive)
40+
guard let contents = try? fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
41+
else { return [] }
42+
return try contents
4043
.compactMap {
4144
guard $0.pathExtension == "json" else {
4245
return nil

Sources/SwiftDocCUtilities/Action/Actions/Merge/MergeAction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct MergeAction: AsyncAction {
6565
let fromDirectory = archive.appendingPathComponent(directoryToCopy, isDirectory: true)
6666
let toDirectory = targetURL.appendingPathComponent(directoryToCopy, isDirectory: true)
6767

68+
guard fileManager.directoryExists(atPath: fromDirectory.path) else { continue }
69+
6870
// Ensure that the destination directory exist in case the first archive didn't have that kind of pages.
6971
// This is necessary when merging a reference-only archive with a tutorial-only archive.
7072
try? fileManager.createDirectory(at: toDirectory, withIntermediateDirectories: false, attributes: nil)

Tests/SwiftDocCUtilitiesTests/MergeActionTests.swift

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,50 @@ class MergeActionTests: XCTestCase {
659659
"doc://org.swift.test/documentation/second.json",
660660
])
661661
}
662-
662+
663+
func testSingleReferenceOnlyArchiveMerging() async throws {
664+
let fileSystem = try TestFileSystem(
665+
folders: [
666+
Folder(name: "Output.doccarchive", content: []),
667+
Self.makeArchive(
668+
name: "First",
669+
documentationPages: [
670+
"First",
671+
"First/SomeClass",
672+
"First/SomeClass/someProperty",
673+
"First/SomeClass/someFunction(:_)",
674+
],
675+
tutorialPages: []
676+
),
677+
]
678+
)
679+
680+
let logStorage = LogHandle.LogStorage()
681+
let action = MergeAction(
682+
archives: [
683+
URL(fileURLWithPath: "/First.doccarchive"),
684+
],
685+
landingPageInfo: testLandingPageInfo,
686+
outputURL: URL(fileURLWithPath: "/Output.doccarchive"),
687+
fileManager: fileSystem
688+
)
689+
690+
_ = try await action.perform(logHandle: .memory(logStorage))
691+
XCTAssertEqual(logStorage.text, "", "The action didn't log anything")
692+
693+
let synthesizedRootNode = try fileSystem.renderNode(atPath: "/Output.doccarchive/data/documentation.json")
694+
XCTAssertEqual(synthesizedRootNode.metadata.title, "Test Landing Page Name")
695+
XCTAssertEqual(synthesizedRootNode.metadata.roleHeading, "Test Landing Page Kind")
696+
XCTAssertEqual(synthesizedRootNode.topicSectionsStyle, .detailedGrid)
697+
XCTAssertEqual(synthesizedRootNode.topicSections.flatMap { [$0.title].compactMap({ $0 }) + $0.identifiers }, [
698+
// No title
699+
"doc://org.swift.test/documentation/first.json",
700+
])
701+
XCTAssertEqual(synthesizedRootNode.references.keys.sorted(), [
702+
"doc://org.swift.test/documentation/first.json",
703+
])
704+
}
705+
663706
func testErrorWhenArchivesContainOverlappingData() async throws {
664707
let fileSystem = try TestFileSystem(
665708
folders: [
@@ -953,14 +996,13 @@ class MergeActionTests: XCTestCase {
953996
Output.doccarchive/
954997
├─ data/
955998
│ ├─ documentation.json
956-
│ ├─ documentation/
957-
│ │ ├─ first.json
958-
│ │ ├─ first/
959-
│ │ │ ╰─ article.json
960-
│ │ ├─ second.json
961-
│ │ ╰─ second/
962-
│ │ ╰─ article.json
963-
│ ╰─ tutorials/
999+
│ ╰─ documentation/
1000+
│ ├─ first.json
1001+
│ ├─ first/
1002+
│ │ ╰─ article.json
1003+
│ ├─ second.json
1004+
│ ╰─ second/
1005+
│ ╰─ article.json
9641006
├─ downloads/
9651007
│ ├─ First/
9661008
│ ╰─ Second/

0 commit comments

Comments
 (0)