Skip to content

Commit fbad654

Browse files
committed
Update index tests to use common build request
Setting up the build request for the index arena description was spread across three separate testing helpers and: 1. Used macOS as the run destination 2. Added all targets from the given workspace Update these to use a common request that is more similar to the workspace build description by default (ie. no run destination + skipping package targets). Resolves rdar://142699583.
1 parent 6cf6a44 commit fbad654

File tree

5 files changed

+98
-53
lines changed

5 files changed

+98
-53
lines changed

Sources/SWBTestSupport/BuildOperationTester.swift

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,29 +1541,78 @@ package final class BuildOperationTester {
15411541
try await checkBuild(name, parameters: parameters, runDestination: runDestination, buildRequest: inputBuildRequest, buildCommand: buildCommand, schemeCommand: schemeCommand, persistent: persistent, serial: serial, buildOutputMap: buildOutputMap, signableTargets: signableTargets, signableTargetInputs: signableTargetInputs, clientDelegate: clientDelegate, sourceLocation: sourceLocation, body: body)
15421542
}
15431543

1544+
package static func buildRequestForIndexOperation(
1545+
workspace: Workspace,
1546+
buildTargets: [any TestTarget]? = nil,
1547+
prepareTargets: [String]? = nil,
1548+
workspaceOperation: Bool? = nil,
1549+
runDestination: RunDestinationInfo? = nil,
1550+
sourceLocation: SourceLocation = #_sourceLocation
1551+
) throws -> BuildRequest {
1552+
let workspaceOperation = workspaceOperation ?? (buildTargets == nil)
1553+
1554+
// If this is an operation on the entire workspace (rather than for a specific target/package), then
1555+
// we will end up configuring for all platforms - do not pass a run destination. Use the provided (or a
1556+
// reasonable default) otherwise.
1557+
let buildDestination: RunDestinationInfo?
1558+
if workspaceOperation {
1559+
buildDestination = nil
1560+
} else {
1561+
buildDestination = runDestination ?? RunDestinationInfo.macOS
1562+
}
1563+
1564+
let arena = ArenaInfo.indexBuildArena(derivedDataRoot: workspace.path.dirname)
1565+
let overrides: [String: String] = ["ONLY_ACTIVE_ARCH": "YES", "ALWAYS_SEARCH_USER_PATHS": "NO"]
1566+
let buildRequestParameters = BuildParameters(action: .indexBuild, configuration: "Debug", activeRunDestination: buildDestination, overrides: overrides, arena: arena)
1567+
1568+
let buildRequestTargets: [BuildRequest.BuildTargetInfo]
1569+
if let buildTargets {
1570+
buildRequestTargets = try buildTargets.map { BuildRequest.BuildTargetInfo(parameters: buildRequestParameters, target: try #require(workspace.target(for: $0.guid), sourceLocation: sourceLocation)) }
1571+
} else {
1572+
buildRequestTargets = workspace.projects.flatMap { project in
1573+
// The workspace description excludes packages so that we don't end up configuring every target for every
1574+
// platform.
1575+
if workspaceOperation && project.isPackage {
1576+
return [BuildRequest.BuildTargetInfo]()
1577+
}
1578+
return project.targets.compactMap { target in
1579+
if target.type == .aggregate {
1580+
return nil
1581+
}
1582+
return BuildRequest.BuildTargetInfo(parameters: buildRequestParameters, target: target)
1583+
}
1584+
}
1585+
}
1586+
1587+
let targetsToPrepare = try prepareTargets?.map { try #require(workspace.target(for: $0)) }
1588+
return BuildRequest(parameters: buildRequestParameters, buildTargets: buildRequestTargets, dependencyScope: .workspace, continueBuildingAfterErrors: true, useParallelTargets: true, useImplicitDependencies: true, useDryRun: false, buildCommand: .prepareForIndexing(buildOnlyTheseTargets: targetsToPrepare, enableIndexBuildArena: true))
1589+
1590+
}
1591+
15441592
/// Construct 'prepare' index build operation, and test the result.
1545-
package func checkIndexBuild(
1593+
package func checkIndexBuild<T>(
15461594
prepareTargets: [String],
1547-
derivedDataRoot: Path? = nil,
1595+
workspaceOperation: Bool = true,
15481596
runDestination: RunDestinationInfo? = nil,
15491597
persistent: Bool = false,
15501598
sourceLocation: SourceLocation = #_sourceLocation,
1551-
body: (BuildResults) throws -> Void
1552-
) async throws {
1553-
let arena = ArenaInfo.indexBuildArena(derivedDataRoot: derivedDataRoot ?? workspace.path.dirname)
1554-
let overrides: [String: String] = ["ONLY_ACTIVE_ARCH": "YES", "ALWAYS_SEARCH_USER_PATHS": "NO"]
1555-
let targetsToPrepare = prepareTargets.map{ workspace.target(for: $0)! }
1556-
// Using 'macOS' as default run-destination; run-destination does not matter for an index build description since it configures targets for all supported platforms.
1557-
let parameters = BuildParameters(action: .indexBuild, configuration: "Debug", activeRunDestination: .macOS, overrides: overrides, arena: arena)
1558-
let buildTargets = workspace.allTargets.compactMap{ $0.type == .aggregate ? nil : BuildRequest.BuildTargetInfo(parameters: parameters, target: $0) }
1559-
let buildRequest = BuildRequest(parameters: parameters, buildTargets: buildTargets, dependencyScope: .workspace, continueBuildingAfterErrors: true, useParallelTargets: true, useImplicitDependencies: true, useDryRun: false, buildCommand: .prepareForIndexing(buildOnlyTheseTargets: targetsToPrepare, enableIndexBuildArena: true))
1560-
1561-
// Use the provided run-destination for the build operation.
1599+
body: (BuildResults) throws -> T
1600+
) async throws -> T {
1601+
let buildRequest = try Self.buildRequestForIndexOperation(
1602+
workspace: workspace,
1603+
prepareTargets: prepareTargets,
1604+
workspaceOperation: workspaceOperation,
1605+
runDestination: runDestination,
1606+
sourceLocation: sourceLocation
1607+
)
1608+
1609+
// The build request may have no run destination (for a workspace description), but we always build for a
1610+
// specific target.
15621611
let runDestination = runDestination ?? RunDestinationInfo.macOS
1563-
let operationParameters = parameters.replacing(activeRunDestination: runDestination, activeArchitecture: nil)
1612+
let operationParameters = buildRequest.parameters.replacing(activeRunDestination: runDestination, activeArchitecture: nil)
15641613
let operationBuildRequest = buildRequest.with(parameters: operationParameters, buildTargets: [])
15651614

1566-
try await checkBuild(buildRequest: buildRequest, operationBuildRequest: operationBuildRequest, persistent: persistent, sourceLocation: sourceLocation, body: body, performBuild: { await $0.buildWithTimeout() })
1615+
return try await checkBuild(buildRequest: buildRequest, operationBuildRequest: operationBuildRequest, persistent: persistent, sourceLocation: sourceLocation, body: body, performBuild: { await $0.buildWithTimeout() })
15671616
}
15681617

15691618
package struct BuildGraphResult: Sendable {
@@ -1631,15 +1680,23 @@ package final class BuildOperationTester {
16311680
}
16321681

16331682
package func checkIndexBuildGraph(
1634-
targets: [any TestTarget], activeRunDestination: RunDestinationInfo = .macOS,
1683+
targets: [any TestTarget]? = nil,
1684+
workspaceOperation: Bool? = nil,
1685+
activeRunDestination: RunDestinationInfo? = nil,
16351686
graphTypes: [TargetGraphFactory.GraphType] = [.dependency],
16361687
sourceLocation: SourceLocation = #_sourceLocation,
16371688
body: (BuildGraphResult) throws -> Void
16381689
) async throws {
1639-
// `activeRunDestination` only matters for packages, so a default of `.macOS` is fine everywhere else
1640-
let buildParameters = BuildParameters(action: .indexBuild, configuration: "Debug", activeRunDestination: activeRunDestination)
1641-
let buildTargets = try targets.map { BuildRequest.BuildTargetInfo(parameters: buildParameters, target: try #require(self.workspace.target(for: $0.guid), sourceLocation: sourceLocation)) }
1642-
let buildRequest = BuildRequest(parameters: buildParameters, buildTargets: buildTargets, dependencyScope: .workspace, continueBuildingAfterErrors: true, useParallelTargets: true, useImplicitDependencies: true, useDryRun: false, buildCommand: .prepareForIndexing(buildOnlyTheseTargets: nil, enableIndexBuildArena: true))
1690+
let workspaceOperation = workspaceOperation ?? (targets == nil)
1691+
1692+
let buildRequest = try Self.buildRequestForIndexOperation(
1693+
workspace: workspace,
1694+
buildTargets: targets,
1695+
workspaceOperation: workspaceOperation,
1696+
runDestination: activeRunDestination,
1697+
sourceLocation: sourceLocation
1698+
)
1699+
16431700
let buildRequestContext = BuildRequestContext(workspaceContext: workspaceContext)
16441701
let delegate = EmptyTargetDependencyResolverDelegate(workspace: workspaceContext.workspace)
16451702

Sources/SWBTestSupport/TaskConstructionTester.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -989,39 +989,23 @@ extension TaskConstructionTester: Sendable { }
989989
extension TaskConstructionTester.PlanningResults: Sendable { }
990990

991991
extension TaskConstructionTester {
992-
package func buildParametersForIndexOperation(
993-
runDestination: RunDestinationInfo? = nil,
994-
arena: ArenaInfo? = nil
995-
) -> BuildParameters {
996-
let arena = arena ?? ArenaInfo.indexBuildArena(derivedDataRoot: workspace.path.dirname.join("DerivedData"))
997-
let overrides: [String: String] = ["ONLY_ACTIVE_ARCH": "YES", "ALWAYS_SEARCH_USER_PATHS": "NO"]
998-
let runDestination = runDestination ?? RunDestinationInfo.macOS
999-
let buildParameters = BuildParameters(action: .indexBuild, configuration: "Debug", activeRunDestination: runDestination, overrides: overrides, arena: arena)
1000-
return buildParameters
1001-
}
1002-
1003992
/// Construct the tasks for an index build operation, and test the result.
1004993
package func checkIndexBuild(
1005994
targets: [any TestTarget]? = nil,
995+
workspaceOperation: Bool? = nil,
1006996
runDestination: RunDestinationInfo? = nil,
1007-
arena: ArenaInfo? = nil,
1008997
systemInfo: SystemInfo? = nil,
1009998
sourceLocation: SourceLocation = #_sourceLocation,
1010999
body: (PlanningResults) throws -> Void
10111000
) async throws {
1012-
let buildParameters = buildParametersForIndexOperation(
1013-
runDestination: runDestination
1001+
let workspaceOperation = workspaceOperation ?? (targets == nil)
1002+
let buildRequest = try BuildOperationTester.buildRequestForIndexOperation(
1003+
workspace: workspace,
1004+
buildTargets: targets,
1005+
workspaceOperation: workspaceOperation,
1006+
runDestination: runDestination,
1007+
sourceLocation: sourceLocation
10141008
)
1015-
1016-
let buildTargets: [BuildRequest.BuildTargetInfo]
1017-
if let targets {
1018-
buildTargets = try targets.map { BuildRequest.BuildTargetInfo(parameters: buildParameters, target: try #require(self.workspace.target(for: $0.guid), sourceLocation: sourceLocation)) }
1019-
} else {
1020-
buildTargets = workspace.allTargets.compactMap{ $0.type == .aggregate ? nil : BuildRequest.BuildTargetInfo(parameters: buildParameters, target: $0) }
1021-
}
1022-
1023-
let buildRequest = BuildRequest(parameters: buildParameters, buildTargets: buildTargets, dependencyScope: .workspace, continueBuildingAfterErrors: true, useParallelTargets: true, useImplicitDependencies: true, useDryRun: false, buildCommand: .prepareForIndexing(buildOnlyTheseTargets: nil, enableIndexBuildArena: true))
1024-
10251009
return try await checkBuild(buildRequest: buildRequest, systemInfo: systemInfo, sourceLocation: sourceLocation, body: body)
10261010
}
10271011
}

Tests/SWBBuildSystemTests/HostBuildToolBuildOperationTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,14 @@ fileprivate struct HostBuildToolBuildOperationTests: CoreBasedTests {
726726
"""
727727
}
728728

729-
try await tester.checkIndexBuild(prepareTargets: testProject.targets.map(\.guid), runDestination: .host, persistent: true) { results in
729+
try await tester.checkIndexBuild(prepareTargets: tester.workspace.targets(named: "Framework").map(\.guid), runDestination: .host, persistent: true) { results in
730730
results.checkNoDiagnostics()
731731

732732
// The tool itself should compile and link.
733733
results.checkTaskExists(.matchTargetName("Tool"), .matchRuleType("SwiftDriver Compilation"))
734-
results.checkTask(.matchTargetName("Tool"), .matchRuleType("Ld")) { _ in }
734+
results.checkTask(.matchTargetName("Tool"), .matchRuleType("Ld")) { task in
735+
task.checkCommandLineMatches(["-target", .contains("-apple-macos")])
736+
}
735737

736738
try results.checkTask(.matchTargetName("Framework"), .matchRuleType(ProductPlan.preparedForIndexPreCompilationRuleName)) { task in
737739
try results.checkTaskFollows(task, .matchTargetName("Tool"), .matchRuleType("Ld"))
@@ -809,12 +811,14 @@ fileprivate struct HostBuildToolBuildOperationTests: CoreBasedTests {
809811
"""
810812
}
811813

812-
try await tester.checkIndexBuild(prepareTargets: testProject.targets.map(\.guid), runDestination: .host, persistent: true) { results in
814+
try await tester.checkIndexBuild(prepareTargets: tester.workspace.targets(named: "Framework").map(\.guid), runDestination: .host, persistent: true) { results in
813815
results.checkNoDiagnostics()
814816

815817
// The tool itself should compile and link.
816818
results.checkTaskExists(.matchTargetName("Tool"), .matchRuleType("SwiftDriver Compilation"))
817-
results.checkTask(.matchTargetName("Tool"), .matchRuleType("Ld")) { _ in }
819+
results.checkTask(.matchTargetName("Tool"), .matchRuleType("Ld")) { task in
820+
task.checkCommandLineMatches(["-target", .contains("-apple-macos")])
821+
}
818822

819823
try results.checkTask(.matchTargetName("Framework"), .matchRuleType(ProductPlan.preparedForIndexPreCompilationRuleName)) { task in
820824
try results.checkTaskFollows(task, .matchTargetName("Tool"), .matchRuleType("Ld"))

Tests/SWBBuildSystemTests/IndexBuildOperationTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,10 +1678,10 @@ fileprivate struct IndexBuildOperationTests: CoreBasedTests {
16781678

16791679
try await tester.fs.writeFileContents(SRCROOT.join("test.swift")) { $0 <<< "// test.swift" }
16801680

1681-
let arena = ArenaInfo.indexBuildArena(derivedDataRoot: tester.workspace.path.dirname)
1682-
1683-
try await tester.checkIndexBuild(prepareTargets: [app.guid], persistent: true) { results in
1681+
let arena = try await tester.checkIndexBuild(prepareTargets: [app.guid], persistent: true) { results in
1682+
let arena = try #require(results.buildRequest.parameters.arena)
16841683
#expect(tester.fs.exists(arena.buildProductsPath))
1684+
return arena
16851685
}
16861686

16871687
let buildRequest = BuildRequest(parameters: BuildParameters(action: .indexBuild, configuration: nil, arena: arena), buildTargets: [], continueBuildingAfterErrors: true, useParallelTargets: true, useImplicitDependencies: true, useDryRun: false, buildCommand: .cleanBuildFolder(style: .regular))

Tests/SWBTaskConstructionTests/IndexBuildTaskConstructionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fileprivate struct IndexBuildTaskConstructionTests: CoreBasedTests {
145145
}
146146

147147
@Test(.requireSDKs(.macOS, .iOS))
148-
func vFSAndHeaderMapContents() async throws {
148+
func vfsAndHeaderMapContents() async throws {
149149

150150
let fwkTarget1 = TestStandardTarget(
151151
"FrameworkTarget1",
@@ -1309,7 +1309,7 @@ fileprivate struct IndexBuildTaskConstructionTests: CoreBasedTests {
13091309
])
13101310
let tester = try await TaskConstructionTester(getCore(), testProject)
13111311

1312-
try await tester.checkIndexBuild { results in
1312+
try await tester.checkIndexBuild(workspaceOperation: false) { results in
13131313
results.checkNoDiagnostics()
13141314
results.checkTarget("tool") { target in
13151315
results.checkWriteAuxiliaryFileTask(.matchTarget(target), .matchRuleType("WriteAuxiliaryFile"), .matchRuleItemBasename("resource_bundle_accessor.swift")) { task, contents in

0 commit comments

Comments
 (0)