Skip to content

Commit 06fcc03

Browse files
authored
adds traits to dependency output in show-dependencies --json and --text (#9034)
Adds traits to the output of the show-dependencies command for `--text` and `--json` output. ### Motivation: Exposes the default traits, if any, that are enabled by the dependencies a package. Resolves: #9033 ### Modifications: Extended the logic in show-dependencies to add in the `enabledTraits` for a resolved package. ### Result: The output of show-dependencies includes output akin to `(traits: FirstTrait, SecondTrait)` is the resolved package has traits and includes the traits that it has. If the package doesn't offer traits, no additional/new content is displayed in `--text`. The JSON provided from `show-dependencies --json` includes a `traits` property, empty if there aren't any traits enabled.
1 parent 7a90723 commit 06fcc03

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Sources/Commands/Utilities/DependenciesSerializer.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ final class PlainTextDumper: DependenciesDumper {
3434

3535
let pkgVersion = package.manifest.version?.description ?? "unspecified"
3636

37-
stream.send("\(hanger)\(package.identity.description)<\(package.manifest.packageLocation)@\(pkgVersion)>\n")
37+
let traitsEnabled: String
38+
if let enabled = package.enabledTraits, !enabled.isEmpty {
39+
traitsEnabled = "(traits: \(package.enabledTraits?.joined(separator: ", ") ?? ""))"
40+
} else {
41+
traitsEnabled = ""
42+
}
43+
44+
stream.send("\(hanger)\(package.identity.description)<\(package.manifest.packageLocation)@\(pkgVersion)>\(traitsEnabled)\n")
3845

3946
if !package.dependencies.isEmpty {
4047
let replacement = (index == packages.count - 1) ? " " : ""
@@ -130,6 +137,7 @@ final class JSONDumper: DependenciesDumper {
130137
"url": .string(package.manifest.packageLocation),
131138
"version": .string(package.manifest.version?.description ?? "unspecified"),
132139
"path": .string(package.path.pathString),
140+
"traits": .array(package.enabledTraits?.map { .string($0) } ?? []),
133141
"dependencies": .array(package.dependencies.compactMap { graph.packages[$0] }.map(convert)),
134142
])
135143
}

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,55 @@ struct PackageCommandTests {
13151315
}
13161316
}
13171317

1318+
@Test(
1319+
.tags(
1320+
.Feature.Command.Package.ShowDependencies,
1321+
),
1322+
arguments: getBuildData(for: SupportedBuildSystemOnAllPlatforms),
1323+
)
1324+
func showDependenciesWithTraits(
1325+
data: BuildData,
1326+
) async throws {
1327+
try await fixture(name: "Traits") { fixturePath in
1328+
let packageRoot = fixturePath.appending("Example")
1329+
let (textOutput, _) = try await execute(
1330+
["show-dependencies", "--format=text"],
1331+
packagePath: packageRoot,
1332+
configuration: data.config,
1333+
buildSystem: data.buildSystem,
1334+
)
1335+
#expect(textOutput.contains("(traits: Package3Trait3)"))
1336+
1337+
let (jsonOutput, _) = try await execute(
1338+
["show-dependencies", "--format=json"],
1339+
packagePath: packageRoot,
1340+
configuration: data.config,
1341+
buildSystem: data.buildSystem,
1342+
)
1343+
let json = try JSON(bytes: ByteString(encodingAsUTF8: jsonOutput))
1344+
guard case .dictionary(let contents) = json else {
1345+
Issue.record("unexpected result")
1346+
return
1347+
}
1348+
guard case .string(let name)? = contents["name"] else {
1349+
Issue.record("unexpected result")
1350+
return
1351+
}
1352+
#expect(name == "TraitsExample")
1353+
1354+
// verify the traits JSON entry lists each of the traits in the fixture
1355+
guard case .array(let traitsProperty)? = contents["traits"] else {
1356+
Issue.record("unexpected result")
1357+
return
1358+
}
1359+
#expect(traitsProperty.contains(.string("Package1")))
1360+
#expect(traitsProperty.contains(.string("Package2")))
1361+
#expect(traitsProperty.contains(.string("Package3")))
1362+
#expect(traitsProperty.contains(.string("Package4")))
1363+
#expect(traitsProperty.contains(.string("BuildCondition1")))
1364+
}
1365+
}
1366+
13181367
@Test(
13191368
arguments: getBuildData(for: SupportedBuildSystemOnAllPlatforms),
13201369
)

0 commit comments

Comments
 (0)