Skip to content

Commit 065536e

Browse files
[PubgrubTests] Handle duplicate product keys in DependencyGraphBuilder.serve (#3564)
* [PubgrubTests] Replace `OrderedDictionary` with `KeyValuePairs` in `DependencyGraphBuilder.serve` [One of the callers](https://github.com/apple/swift-package-manager/blob/20eba126ffa32088abb46d642bd0481dbae212ef/Tests/PackageGraphTests/PubgrubTests.swift#L472-L475) of the function passes in a dictionary literal with duplicate keys. Although the logic in this function suggests that duplicate keys are allowed, `TSCBasic.OrderedDictionary` preserves only [the final](https://github.com/apple/swift-tools-support-core/blob/21a79185b2ea8f89b9253ed8cdf2338bf564c499/Sources/TSCBasic/OrderedDictionary.swift#L97-L99) of all duplicate keys' values in a dictionary literal. In addition, with #3533, Swift Collections' `OrderedDictionary` [traps](https://github.com/apple/swift-collections/blob/c0549b6284aadd5fd13156316f43fcb43c7fca77/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary%2BInitializers.swift#L88-L91) on duplicate keys. `KeyValuePairs` seems like the right replacement that allows duplicate keys. * [PubgrubTests] append, not assign, `packageDependencies` to value in dictionary keyed by `product` `dependencies` passed to `DependencyGraphBuilder.serve` can have duplicate product keys, so when `dependencies` are iterated, the same `product` can appear more than once, each time with possibly different `filteredDependencies`. Assigning `packageDependencies` mapped from a `product`'s `filteredDependencies` to the value in a different dictionary keyed by the same `product` overrides any existing value from a previous assignment. Appending `packageDependencies` instead preserves all previous values.
1 parent 26275b6 commit 065536e

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Tests/PackageGraphTests/PubgrubTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ class DependencyGraphBuilder {
21662166
_ package: String,
21672167
at version: Version,
21682168
toolsVersion: ToolsVersion? = nil,
2169-
with dependencies: OrderedDictionary<String, OrderedDictionary<String, (PackageRequirement, ProductFilter)>> = [:]
2169+
with dependencies: KeyValuePairs<String, OrderedDictionary<String, (PackageRequirement, ProductFilter)>> = [:]
21702170
) {
21712171
serve(package, at: .version(version), toolsVersion: toolsVersion, with: dependencies)
21722172
}
@@ -2175,7 +2175,7 @@ class DependencyGraphBuilder {
21752175
_ package: String,
21762176
at version: BoundVersion,
21772177
toolsVersion: ToolsVersion? = nil,
2178-
with dependencies: OrderedDictionary<String, OrderedDictionary<String, (PackageRequirement, ProductFilter)>> = [:]
2178+
with dependencies: KeyValuePairs<String, OrderedDictionary<String, (PackageRequirement, ProductFilter)>> = [:]
21792179
) {
21802180
let packageReference = reference(for: package)
21812181
let container = self.containers[package] ?? MockContainer(package: packageReference)
@@ -2193,7 +2193,7 @@ class DependencyGraphBuilder {
21932193
let packageDependencies: [MockContainer.Dependency] = filteredDependencies.map {
21942194
(container: reference(for: $0), requirement: $1.0, products: $1.1)
21952195
}
2196-
container.dependencies[version.description, default: [:]][product] = packageDependencies
2196+
container.dependencies[version.description, default: [:]][product, default: []] += packageDependencies
21972197
}
21982198
self.containers[package] = container
21992199
}

0 commit comments

Comments
 (0)