Skip to content

Commit ec407ac

Browse files
authored
Don't include test products in the product_memberships key of the describe command. Also fix a problem where not all products were included. (#3139)
1 parent c39839a commit ec407ac

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Sources/Commands/Describe.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@ fileprivate struct DescribedPackage: Encodable {
6262
self.dependencies = package.manifest.dependencies.map { DescribedPackageDependency(from: $0) }
6363
self.defaultLocalization = package.manifest.defaultLocalization
6464
self.platforms = package.manifest.platforms.map { DescribedPlatformRestriction(from: $0) }
65-
self.products = package.products.map {
65+
// SwiftPM considers tests to be products, which is not how things are presented in the manifest.
66+
let nonTestProducts = package.products.filter{ $0.type != .test }
67+
self.products = nonTestProducts.map {
6668
DescribedProduct(from: $0, in: package)
6769
}
6870
// Create a mapping from the targets to the products to which they contribute directly. This excludes any
6971
// contributions that occur through `.product()` dependencies, but since those targets are still part of a
7072
// product of the package, the set of targets that contribute to products still accurately represents the
7173
// set of targets reachable from external clients.
72-
let targetProductPairs = package.products.flatMap{ p in transitiveClosure(p.targets, successors: {
73-
$0.dependencies.compactMap{ $0.target } }).map{ t in (t, p) }
74+
let targetProductPairs = nonTestProducts.flatMap{ p in
75+
transitiveClosure(p.targets, successors: {
76+
$0.dependencies.compactMap{ $0.target }
77+
}).union(p.targets).map{ t in (t, p) }
7478
}
7579
let targetsToProducts = Dictionary(targetProductPairs.map{ ($0.0, [$0.1]) }, uniquingKeysWith: { $0 + $1 })
7680
self.targets = package.targets.map {

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ final class PackageToolTests: XCTestCase {
193193
}
194194

195195
func testDescribe() throws {
196+
197+
fixture(name: "Miscellaneous/ExeTest") { prefix in
198+
// Generate the JSON description.
199+
let jsonResult = try SwiftPMProduct.SwiftPackage.executeProcess(["describe", "--type=json"], packagePath: prefix)
200+
let jsonOutput = try jsonResult.utf8Output()
201+
let json = try JSON(bytes: ByteString(encodingAsUTF8: jsonOutput))
202+
203+
// Check that tests don't appear in the product memberships.
204+
XCTAssertEqual(json["name"]?.string, "ExeTest")
205+
let jsonTarget0 = try XCTUnwrap(json["targets"]?.array?[0])
206+
XCTAssertNil(jsonTarget0["product_memberships"])
207+
let jsonTarget1 = try XCTUnwrap(json["targets"]?.array?[1])
208+
XCTAssertEqual(jsonTarget1["product_memberships"]?.array?[0].stringValue, "Exe")
209+
}
210+
196211
fixture(name: "CFamilyTargets/SwiftCMixed") { prefix in
197212
// Generate the JSON description.
198213
let jsonResult = try SwiftPMProduct.SwiftPackage.executeProcess(["describe", "--type=json"], packagePath: prefix)
@@ -214,11 +229,13 @@ final class PackageToolTests: XCTestCase {
214229
XCTAssertEqual(jsonTarget1["c99name"]?.stringValue, "SeaExec")
215230
XCTAssertEqual(jsonTarget1["type"]?.stringValue, "executable")
216231
XCTAssertEqual(jsonTarget1["module_type"]?.stringValue, "SwiftTarget")
232+
XCTAssertEqual(jsonTarget1["product_memberships"]?.array?[0].stringValue, "SeaExec")
217233
let jsonTarget2 = try XCTUnwrap(json["targets"]?.array?[2])
218234
XCTAssertEqual(jsonTarget2["name"]?.stringValue, "CExec")
219235
XCTAssertEqual(jsonTarget2["c99name"]?.stringValue, "CExec")
220236
XCTAssertEqual(jsonTarget2["type"]?.stringValue, "executable")
221237
XCTAssertEqual(jsonTarget2["module_type"]?.stringValue, "ClangTarget")
238+
XCTAssertEqual(jsonTarget2["product_memberships"]?.array?[0].stringValue, "CExec")
222239

223240
// Generate the text description.
224241
let textResult = try SwiftPMProduct.SwiftPackage.executeProcess(["describe", "--type=text"], packagePath: prefix)

0 commit comments

Comments
 (0)