Skip to content

Commit cc8e384

Browse files
authored
Deterministic behaviour when a catalog has colliding filename (#286)
Whenever there are two or more files with same name for a type in a catalog only one is picked up randomly and rest gets Diagnostics information that its content is being dropped. The change addresses the issue by adding a sort to the url of the document, so that the behavior is deterministic, i.e the order in which a file is picked, as well as the rest who's content is ignored when there is colliding filenames within a catalog. rdar: //81691423
1 parent 2f88d53 commit cc8e384

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed

Sources/SwiftDocC/Infrastructure/DocumentationContext.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,10 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
823823
throw error
824824
}
825825

826-
for analyzedDocument in analyzedDocuments {
826+
// to preserve the order of documents by url
827+
let analyzedDocumentsSorted = analyzedDocuments.sorted(by: \.0.absoluteString)
828+
829+
for analyzedDocument in analyzedDocumentsSorted {
827830
// Store the references we encounter to ensure they're unique. The file name is currently the only part of the URL considered for the topic reference, so collisions may occur.
828831
let (url, analyzed) = analyzedDocument
829832

Tests/SwiftDocCTests/Infrastructure/DocumentationContext/DocumentationContextTests.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,15 +644,30 @@ class DocumentationContextTests: XCTestCase {
644644

645645
func testDetectsReferenceCollision() throws {
646646
let (_, context) = try testBundleAndContext(named: "TestBundleWithDupe")
647-
648-
XCTAssert(
649-
context.problems.contains { problem in
650-
problem.diagnostic.identifier == "org.swift.docc.DuplicateReference"
651-
&& problem.diagnostic.localizedSummary == "Redeclaration of 'TestTutorial.tutorial'; this file will be skipped"
652-
}
653-
)
647+
648+
let problemWithDuplicate = context.problems.filter { $0.diagnostic.identifier == "org.swift.docc.DuplicateReference" }
649+
650+
XCTAssertEqual(problemWithDuplicate.count, 1)
651+
652+
let localizedSummary = try XCTUnwrap(problemWithDuplicate.first?.diagnostic.localizedSummary)
653+
XCTAssertEqual(localizedSummary, "Redeclaration of 'TestTutorial.tutorial'; this file will be skipped")
654+
654655
}
655656

657+
func testDetectsMultipleMDfilesWithSameName() throws {
658+
let (_, context) = try testBundleAndContext(named: "TestBundleWithDupMD")
659+
660+
let problemWithDuplicateReference = context.problems.filter { $0.diagnostic.identifier == "org.swift.docc.DuplicateReference" }
661+
662+
XCTAssertEqual(problemWithDuplicateReference.count, 2)
663+
664+
let localizedSummary = try XCTUnwrap(problemWithDuplicateReference.first?.diagnostic.localizedSummary)
665+
XCTAssertEqual(localizedSummary, "Redeclaration of \'overview.md\'; this file will be skipped")
666+
667+
let localizedSummarySecond = try XCTUnwrap(problemWithDuplicateReference[1].diagnostic.localizedSummary)
668+
XCTAssertEqual(localizedSummarySecond, "Redeclaration of \'overview.md\'; this file will be skipped")
669+
}
670+
656671
func testGraphChecks() throws {
657672
let workspace = DocumentationWorkspace()
658673
let context = try DocumentationContext(dataProvider: workspace)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleName</key>
6+
<string>TestBundleWithDupMD</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>Test Bundle</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>org.swift.docc.example</string>
11+
<key>CFBundleIconFile</key>
12+
<string>DocumentationIcon</string>
13+
<key>CFBundleIconName</key>
14+
<string>DocumentationIcon</string>
15+
<key>CFBundlePackageType</key>
16+
<string>DOCS</string>
17+
<key>CFBundleSignature</key>
18+
<string>????</string>
19+
<key>CFBundleVersion</key>
20+
<string>0.1.0</string>
21+
<key>CFBundleShortVersionString</key>
22+
<string>0.1.0</string>
23+
</dict>
24+
</plist>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Getting Started with Capybara
2+
3+
Create a capybara and assign personality traits and abilities.
4+
5+
## Overview at documentation level
6+
7+
Capybara are complex creatures that require careful creation and a suitable habitat. After creating a capybara, you're responsible for feeding them, providing fulfilling activities, and giving them opportunities to exercise and rest.
8+
9+
<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Getting Started with Monkeys
2+
3+
Create a monkey and assign personality traits and abilities.
4+
5+
## Overview at documentation1
6+
7+
Monekys are complex creatures that require careful creation and a suitable habitat. After creating a sloth, you're responsible for feeding them, providing fulfilling activities, and giving them opportunities to exercise and rest.
8+
9+
<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Getting Started with Monkeys
2+
3+
Create a monkey and assign personality traits and abilities.
4+
5+
## Overview at parent level
6+
7+
Monkeys are complex creatures that require careful creation and a suitable habitat. After creating a sloth, you're responsible for feeding them, providing fulfilling activities, and giving them opportunities to exercise and rest.
8+
9+
<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

0 commit comments

Comments
 (0)