Skip to content

Commit d0f8471

Browse files
committed
[Tests] NFC: Add tests for new ModulesGraph.{product, target}(for:destination:) behavior
1 parent 3d6473c commit d0f8471

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
516516
}
517517

518518
/// Compute the llbuild target name using the given subset.
519-
private func computeLLBuildTargetName(for subset: BuildSubset) throws -> String {
519+
func computeLLBuildTargetName(for subset: BuildSubset) throws -> String {
520520
switch subset {
521521
case .allExcludingTests:
522522
return LLBuildManifestBuilder.TargetKind.main.targetName

Sources/SPMTestSupport/MockPackageGraphs.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ public func macrosPackageGraph() throws -> MockPackageGraph {
135135
@_spi(SwiftPMInternal)
136136
public func macrosTestsPackageGraph() throws -> MockPackageGraph {
137137
let fs = InMemoryFileSystem(emptyFiles:
138+
"/swift-mmio/Plugins/MMIOPlugin/source.swift",
138139
"/swift-mmio/Sources/MMIO/source.swift",
139140
"/swift-mmio/Sources/MMIOMacros/source.swift",
140141
"/swift-mmio/Sources/MMIOMacrosTests/source.swift",
142+
"/swift-mmio/Sources/MMIOMacro+PluginTests/source.swift",
141143
"/swift-syntax/Sources/SwiftSyntax/source.swift",
142144
"/swift-syntax/Sources/SwiftSyntaxMacrosTestSupport/source.swift",
143145
"/swift-syntax/Sources/SwiftSyntaxMacros/source.swift",
@@ -164,6 +166,11 @@ public func macrosTestsPackageGraph() throws -> MockPackageGraph {
164166
name: "MMIO",
165167
type: .library(.automatic),
166168
targets: ["MMIO"]
169+
),
170+
ProductDescription(
171+
name: "MMIOPlugin",
172+
type: .plugin,
173+
targets: ["MMIOPlugin"]
167174
)
168175
],
169176
targets: [
@@ -179,13 +186,26 @@ public func macrosTestsPackageGraph() throws -> MockPackageGraph {
179186
],
180187
type: .macro
181188
),
189+
TargetDescription(
190+
name: "MMIOPlugin",
191+
type: .plugin,
192+
pluginCapability: .buildTool
193+
),
182194
TargetDescription(
183195
name: "MMIOMacrosTests",
184196
dependencies: [
185197
.target(name: "MMIOMacros"),
186198
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax")
187199
],
188200
type: .test
201+
),
202+
TargetDescription(
203+
name: "MMIOMacro+PluginTests",
204+
dependencies: [
205+
.target(name: "MMIOPlugin"),
206+
.target(name: "MMIOMacros")
207+
],
208+
type: .test
189209
)
190210
]
191211
),

Tests/BuildTests/BuildOperationTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import LLBuildManifest
1818
@_spi(DontAdoptOutsideOfSwiftPMExposedForBenchmarksAndTestsOnly)
1919
import PackageGraph
2020
import SPMBuildCore
21+
@_spi(SwiftPMInternal)
2122
import SPMTestSupport
2223
import XCTest
2324

@@ -179,4 +180,41 @@ final class BuildOperationTests: XCTestCase {
179180
}
180181
}
181182
}
183+
184+
func testHostProductsAndTargetsWithoutExplicitDestination() throws {
185+
let mock = try macrosTestsPackageGraph()
186+
187+
let op = mockBuildOperation(
188+
productsBuildParameters: mockBuildParameters(destination: .target),
189+
toolsBuildParameters: mockBuildParameters(destination: .host),
190+
packageGraphLoader: { mock.graph },
191+
scratchDirectory: AbsolutePath("/.build/\(hostTriple)"),
192+
fs: mock.fileSystem,
193+
observabilityScope: mock.observabilityScope
194+
)
195+
196+
XCTAssertEqual(
197+
"MMIOMacros-\(hostTriple)-debug-tool.exe",
198+
try op.computeLLBuildTargetName(for: .product("MMIOMacros"))
199+
)
200+
201+
for target in ["MMIOMacros", "MMIOPlugin", "MMIOMacrosTests", "MMIOMacro+PluginTests"] {
202+
XCTAssertEqual(
203+
"\(target)-\(hostTriple)-debug-tool.module",
204+
try op.computeLLBuildTargetName(for: .target(target))
205+
)
206+
}
207+
208+
let dependencies = try BuildSubset.target("MMIOMacro+PluginTests").recursiveDependencies(
209+
for: mock.graph,
210+
observabilityScope: mock.observabilityScope
211+
)
212+
213+
XCTAssertNotNil(dependencies)
214+
XCTAssertTrue(dependencies!.count > 0)
215+
216+
for dependency in dependencies! {
217+
XCTAssertEqual(dependency.buildTriple, .tools)
218+
}
219+
}
182220
}

Tests/BuildTests/CrossCompilationBuildPlanTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
302302
)
303303
let result = try BuildPlanResult(plan: plan)
304304
result.checkProductsCount(2)
305-
result.checkTargetsCount(15)
305+
result.checkTargetsCount(16)
306306

307307
XCTAssertTrue(try result.allTargets(named: "SwiftSyntax")
308308
.map { try $0.swiftTarget() }

Tests/PackageGraphTests/CrossCompilationPackageGraphTests.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
8383
}
8484
}
8585

86+
result.checkProduct("MMIOMacros", destination: .destination) { result in
87+
result.check(buildTriple: .tools)
88+
}
89+
8690
result.checkTargets("SwiftSyntax") { results in
8791
XCTAssertEqual(results.count, 2)
8892

@@ -100,6 +104,7 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
100104
result.check(
101105
targets: "MMIO",
102106
"MMIOMacros",
107+
"MMIOPlugin",
103108
"SwiftCompilerPlugin",
104109
"SwiftCompilerPlugin",
105110
"SwiftCompilerPluginMessageHandling",
@@ -111,14 +116,15 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
111116
"SwiftSyntaxMacrosTestSupport",
112117
"SwiftSyntaxMacrosTestSupport"
113118
)
114-
result.check(testModules: "MMIOMacrosTests")
119+
result.check(testModules: "MMIOMacrosTests", "MMIOMacro+PluginTests")
115120
result.checkTarget("MMIO") { result in
116121
result.check(buildTriple: .destination)
117122
result.check(dependencies: "MMIOMacros")
118123
}
119124
result.checkTargets("MMIOMacros") { results in
120125
XCTAssertEqual(results.count, 1)
121126
}
127+
122128
result.checkTarget("MMIOMacrosTests", destination: .tools) { result in
123129
result.check(buildTriple: .tools)
124130
result.checkDependency("MMIOMacros") { result in
@@ -146,6 +152,14 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
146152
}
147153
}
148154

155+
result.checkTarget("MMIOMacros", destination: .destination) { result in
156+
result.check(buildTriple: .tools)
157+
}
158+
159+
result.checkTarget("MMIOMacrosTests", destination: .destination) { result in
160+
result.check(buildTriple: .tools)
161+
}
162+
149163
result.checkTargets("SwiftSyntax") { results in
150164
XCTAssertEqual(results.count, 2)
151165

@@ -168,4 +182,35 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
168182
}
169183
}
170184
}
185+
186+
func testPlugins() throws {
187+
let graph = try macrosTestsPackageGraph().graph
188+
PackageGraphTester(graph) { result in
189+
result.checkProduct("MMIOPlugin", destination: .tools) { result in
190+
result.check(buildTriple: .tools)
191+
}
192+
193+
result.checkProduct("MMIOPlugin", destination: .destination) { result in
194+
result.check(buildTriple: .tools)
195+
}
196+
197+
result.checkTarget("MMIOPlugin", destination: .tools) { result in
198+
result.check(buildTriple: .tools)
199+
}
200+
201+
result.checkTarget("MMIOPlugin", destination: .destination) { result in
202+
result.check(buildTriple: .tools)
203+
}
204+
205+
result.checkTarget("MMIOMacro+PluginTests", destination: .tools) { result in
206+
result.check(buildTriple: .tools)
207+
result.check(dependencies: "MMIOPlugin", "MMIOMacros")
208+
}
209+
210+
result.checkTarget("MMIOMacro+PluginTests", destination: .destination) { result in
211+
result.check(buildTriple: .tools)
212+
result.check(dependencies: "MMIOPlugin", "MMIOMacros")
213+
}
214+
}
215+
}
171216
}

0 commit comments

Comments
 (0)