Skip to content

Commit 45151c2

Browse files
authored
Allow changing macro expansions on test actions. (#1468)
* Allow to override test macroExpansions * update doc * style * Address feedback from PR * fix build * add toJSONValue encoding
1 parent f884222 commit 45151c2

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

Docs/ProjectSpec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ The different actions share some properties:
10391039
- [ ] **askForAppToLaunch**: **Bool** - `run` and `profile` actions can define the executable set to ask to launch. This defaults to false.
10401040
- [ ] **launchAutomaticallySubstyle**: **String** - `run` action can define the launch automatically substyle ('2' for extensions).
10411041
- [ ] **storeKitConfiguration**: **String** - `run` action can specify a storekit configuration. See [Options](#options).
1042-
- [ ] **macroExpansion**: **String** - `run` action can define the macro expansion from other target. This defaults to nil.
1042+
- [ ] **macroExpansion**: **String** - `run` and `test` action can define the macro expansion from other target. This defaults to nil.
10431043

10441044
### Execution Action
10451045

Sources/ProjectSpec/Scheme.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public struct Scheme: Equatable {
220220
public var captureScreenshotsAutomatically: Bool
221221
public var deleteScreenshotsWhenEachTestSucceeds: Bool
222222
public var testPlans: [TestPlan]
223+
public var macroExpansion: String?
223224

224225
public struct TestTarget: Equatable, ExpressibleByStringLiteral {
225226

@@ -286,7 +287,8 @@ public struct Scheme: Equatable {
286287
debugEnabled: Bool = debugEnabledDefault,
287288
customLLDBInit: String? = nil,
288289
captureScreenshotsAutomatically: Bool = captureScreenshotsAutomaticallyDefault,
289-
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault
290+
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault,
291+
macroExpansion: String? = nil
290292
) {
291293
self.config = config
292294
self.gatherCoverageData = gatherCoverageData
@@ -304,6 +306,7 @@ public struct Scheme: Equatable {
304306
self.customLLDBInit = customLLDBInit
305307
self.captureScreenshotsAutomatically = captureScreenshotsAutomatically
306308
self.deleteScreenshotsWhenEachTestSucceeds = deleteScreenshotsWhenEachTestSucceeds
309+
self.macroExpansion = macroExpansion
307310
}
308311

309312
public var shouldUseLaunchSchemeArgsEnv: Bool {
@@ -620,6 +623,7 @@ extension Scheme.Test: JSONObjectConvertible {
620623
customLLDBInit = jsonDictionary.json(atKeyPath: "customLLDBInit")
621624
captureScreenshotsAutomatically = jsonDictionary.json(atKeyPath: "captureScreenshotsAutomatically") ?? Scheme.Test.captureScreenshotsAutomaticallyDefault
622625
deleteScreenshotsWhenEachTestSucceeds = jsonDictionary.json(atKeyPath: "deleteScreenshotsWhenEachTestSucceeds") ?? Scheme.Test.deleteScreenshotsWhenEachTestSucceedsDefault
626+
macroExpansion = jsonDictionary.json(atKeyPath: "macroExpansion")
623627
}
624628
}
625629

@@ -636,6 +640,7 @@ extension Scheme.Test: JSONEncodable {
636640
"language": language,
637641
"region": region,
638642
"coverageTargets": coverageTargets.map { $0.reference },
643+
"macroExpansion": macroExpansion
639644
]
640645

641646
if gatherCoverageData != Scheme.Test.gatherCoverageDataDefault {

Sources/XcodeGenKit/SchemeGenerator.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,18 @@ public class SchemeGenerator {
289289
let testBuildableEntries = buildActionEntries.filter({ $0.buildFor.contains(.testing) }) + testBuildTargetEntries
290290
let testMacroExpansionBuildableRef = testBuildableEntries.map(\.buildableReference).contains(buildableReference) ? buildableReference : testBuildableEntries.first?.buildableReference
291291

292+
let testMacroExpansion: XCScheme.BuildableReference = buildActionEntries.first(
293+
where: { value in
294+
if let macroExpansion = scheme.test?.macroExpansion {
295+
return value.buildableReference.blueprintName == macroExpansion
296+
}
297+
return false
298+
}
299+
)?.buildableReference ?? testMacroExpansionBuildableRef ?? buildableReference
300+
292301
let testAction = XCScheme.TestAction(
293302
buildConfiguration: scheme.test?.config ?? defaultDebugConfig.name,
294-
macroExpansion: testMacroExpansionBuildableRef,
303+
macroExpansion: testMacroExpansion,
295304
testables: testables,
296305
testPlans: testPlans.isEmpty ? nil : testPlans,
297306
preActions: scheme.test?.preActions.map(getExecutionAction) ?? [],

Tests/XcodeGenKitTests/SchemeGeneratorTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,41 @@ class SchemeGeneratorTests: XCTestCase {
476476
let xcodeProject = try project.generateXcodeProject()
477477

478478
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
479+
try expect(xcscheme.testAction?.macroExpansion?.buildableName) == "MyApp.app"
480+
try expect(xcscheme.launchAction?.macroExpansion?.buildableName) == "MyApp.app"
481+
}
482+
483+
$0.it("allows to override test macroExpansion") {
484+
let app = Target(
485+
name: "MyApp",
486+
type: .application,
487+
platform: .iOS,
488+
dependencies: [Dependency(type: .target, reference: "MyAppExtension", embed: false)]
489+
)
490+
491+
let `extension` = Target(
492+
name: "MyAppExtension",
493+
type: .appExtension,
494+
platform: .iOS
495+
)
496+
let appTarget = Scheme.BuildTarget(target: .local(app.name), buildTypes: [.running])
497+
let extensionTarget = Scheme.BuildTarget(target: .local(`extension`.name), buildTypes: [.running])
498+
499+
let scheme = Scheme(
500+
name: "TestScheme",
501+
build: Scheme.Build(targets: [appTarget, extensionTarget]),
502+
run: Scheme.Run(config: "Debug", macroExpansion: "MyApp"),
503+
test: .init(macroExpansion: "MyAppExtension")
504+
)
505+
let project = Project(
506+
name: "test",
507+
targets: [app, `extension`],
508+
schemes: [scheme]
509+
)
510+
let xcodeProject = try project.generateXcodeProject()
511+
512+
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
513+
try expect(xcscheme.testAction?.macroExpansion?.buildableName) == "MyAppExtension.appex"
479514
try expect(xcscheme.launchAction?.macroExpansion?.buildableName) == "MyApp.app"
480515
}
481516

0 commit comments

Comments
 (0)