Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing macro expansions on test actions. #1468

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/ProjectSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ The different actions share some properties:
- [ ] **askForAppToLaunch**: **Bool** - `run` and `profile` actions can define the executable set to ask to launch. This defaults to false.
- [ ] **launchAutomaticallySubstyle**: **String** - `run` action can define the launch automatically substyle ('2' for extensions).
- [ ] **storeKitConfiguration**: **String** - `run` action can specify a storekit configuration. See [Options](#options).
- [ ] **macroExpansion**: **String** - `run` action can define the macro expansion from other target. This defaults to nil.
- [ ] **macroExpansion**: **String** - `run` and `test` action can define the macro expansion from other target. This defaults to nil.

### Execution Action

Expand Down
7 changes: 6 additions & 1 deletion Sources/ProjectSpec/Scheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public struct Scheme: Equatable {
public var captureScreenshotsAutomatically: Bool
public var deleteScreenshotsWhenEachTestSucceeds: Bool
public var testPlans: [TestPlan]
public var macroExpansion: String?

public struct TestTarget: Equatable, ExpressibleByStringLiteral {

Expand Down Expand Up @@ -286,7 +287,8 @@ public struct Scheme: Equatable {
debugEnabled: Bool = debugEnabledDefault,
customLLDBInit: String? = nil,
captureScreenshotsAutomatically: Bool = captureScreenshotsAutomaticallyDefault,
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault,
macroExpansion: String? = nil
) {
self.config = config
self.gatherCoverageData = gatherCoverageData
Expand All @@ -304,6 +306,7 @@ public struct Scheme: Equatable {
self.customLLDBInit = customLLDBInit
self.captureScreenshotsAutomatically = captureScreenshotsAutomatically
self.deleteScreenshotsWhenEachTestSucceeds = deleteScreenshotsWhenEachTestSucceeds
self.macroExpansion = macroExpansion
}

public var shouldUseLaunchSchemeArgsEnv: Bool {
Expand Down Expand Up @@ -620,6 +623,7 @@ extension Scheme.Test: JSONObjectConvertible {
customLLDBInit = jsonDictionary.json(atKeyPath: "customLLDBInit")
captureScreenshotsAutomatically = jsonDictionary.json(atKeyPath: "captureScreenshotsAutomatically") ?? Scheme.Test.captureScreenshotsAutomaticallyDefault
deleteScreenshotsWhenEachTestSucceeds = jsonDictionary.json(atKeyPath: "deleteScreenshotsWhenEachTestSucceeds") ?? Scheme.Test.deleteScreenshotsWhenEachTestSucceedsDefault
macroExpansion = jsonDictionary.json(atKeyPath: "macroExpansion")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just missing the encoding in toJSONValue below as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 0195336

}
}

Expand All @@ -636,6 +640,7 @@ extension Scheme.Test: JSONEncodable {
"language": language,
"region": region,
"coverageTargets": coverageTargets.map { $0.reference },
"macroExpansion": macroExpansion
]

if gatherCoverageData != Scheme.Test.gatherCoverageDataDefault {
Expand Down
11 changes: 10 additions & 1 deletion Sources/XcodeGenKit/SchemeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,18 @@ public class SchemeGenerator {
let testBuildableEntries = buildActionEntries.filter({ $0.buildFor.contains(.testing) }) + testBuildTargetEntries
let testMacroExpansionBuildableRef = testBuildableEntries.map(\.buildableReference).contains(buildableReference) ? buildableReference : testBuildableEntries.first?.buildableReference

let testMacroExpansion: XCScheme.BuildableReference = buildActionEntries.first(
where: { value in
if let macroExpansion = scheme.test?.macroExpansion {
return value.buildableReference.blueprintName == macroExpansion
}
return false
}
)?.buildableReference ?? testMacroExpansionBuildableRef ?? buildableReference

let testAction = XCScheme.TestAction(
buildConfiguration: scheme.test?.config ?? defaultDebugConfig.name,
macroExpansion: testMacroExpansionBuildableRef,
macroExpansion: testMacroExpansion,
testables: testables,
testPlans: testPlans.isEmpty ? nil : testPlans,
preActions: scheme.test?.preActions.map(getExecutionAction) ?? [],
Expand Down
35 changes: 35 additions & 0 deletions Tests/XcodeGenKitTests/SchemeGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,41 @@ class SchemeGeneratorTests: XCTestCase {
let xcodeProject = try project.generateXcodeProject()

let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
try expect(xcscheme.testAction?.macroExpansion?.buildableName) == "MyApp.app"
try expect(xcscheme.launchAction?.macroExpansion?.buildableName) == "MyApp.app"
}

$0.it("allows to override test macroExpansion") {
let app = Target(
name: "MyApp",
type: .application,
platform: .iOS,
dependencies: [Dependency(type: .target, reference: "MyAppExtension", embed: false)]
)

let `extension` = Target(
name: "MyAppExtension",
type: .appExtension,
platform: .iOS
)
let appTarget = Scheme.BuildTarget(target: .local(app.name), buildTypes: [.running])
let extensionTarget = Scheme.BuildTarget(target: .local(`extension`.name), buildTypes: [.running])

let scheme = Scheme(
name: "TestScheme",
build: Scheme.Build(targets: [appTarget, extensionTarget]),
run: Scheme.Run(config: "Debug", macroExpansion: "MyApp"),
test: .init(macroExpansion: "MyAppExtension")
)
let project = Project(
name: "test",
targets: [app, `extension`],
schemes: [scheme]
)
let xcodeProject = try project.generateXcodeProject()

let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
try expect(xcscheme.testAction?.macroExpansion?.buildableName) == "MyAppExtension.appex"
try expect(xcscheme.launchAction?.macroExpansion?.buildableName) == "MyApp.app"
}

Expand Down