Compare commits

...

4 Commits

Author SHA1 Message Date
Wolfgang Lutz
74d5f8aa31
Merge 14e4455e2f into f8842228c5 2024-07-03 21:43:06 +10:00
Ernesto Cambuston
f8842228c5
Test action macroExpansion allows unavailable buildable reference. (#1471)
* fix buildable ref

* fix test

* add test
2024-07-03 21:41:03 +10:00
Wolfgang Lutz
14e4455e2f add return for older xcode/swift versions 2024-05-26 11:47:01 +02:00
Wolfgang Lutz
985562484a Add Glob support for dictionary includes
This change enables the use of Glob expressions (e.g. Sources/*/Package.yml) when including a yml file:

```
include:
  - path: Sources/*/Package.yml
  - path: Sources/*/Tests.yml
  - path: Sources/*/UITests.yml
```
2024-05-26 11:39:42 +02:00
3 changed files with 82 additions and 11 deletions

View File

@ -1,6 +1,7 @@
import Foundation
import JSONUtilities
import PathKit
import XcodeGenCore
import Yams
public struct SpecFile {
@ -26,22 +27,50 @@ public struct SpecFile {
static let defaultEnable = true
init?(any: Any) {
if let string = any as? String {
path = Path(string)
relativePaths = Include.defaultRelativePaths
enable = Include.defaultEnable
if let path = any as? String {
self.init(
path: Path(path),
relativePaths: Include.defaultRelativePaths,
enable: Include.defaultEnable
)
} else if let dictionary = any as? JSONDictionary, let path = dictionary["path"] as? String {
self.path = Path(path)
relativePaths = Self.resolveBoolean(dictionary, key: "relativePaths") ?? Include.defaultRelativePaths
enable = Self.resolveBoolean(dictionary, key: "enable") ?? Include.defaultEnable
self.init(
path: Path(path),
dictionary: dictionary
)
} else {
return nil
}
}
private init(path: Path, relativePaths: Bool, enable: Bool) {
self.path = path
self.relativePaths = relativePaths
self.enable = enable
}
private init?(path: Path, dictionary: JSONDictionary) {
self.path = path
relativePaths = Self.resolveBoolean(dictionary, key: "relativePaths") ?? Include.defaultRelativePaths
enable = Self.resolveBoolean(dictionary, key: "enable") ?? Include.defaultEnable
}
private static func includes(from array: [Any], basePath: Path) -> [Include] {
array.flatMap { entry -> [Include] in
if let string = entry as? String, let include = Include(any: string) {
return [include]
} else if let dictionary = entry as? JSONDictionary, let path = dictionary["path"] as? String {
return Glob(pattern: (basePath + Path(path)).normalize().string)
.compactMap { Include(path: Path($0), dictionary: dictionary) }
} else {
return []
}
}
}
static func parse(json: Any?) -> [Include] {
static func parse(json: Any?, basePath: Path) -> [Include] {
if let array = json as? [Any] {
return array.compactMap(Include.init)
return includes(from: array, basePath: basePath)
} else if let object = json, let include = Include(any: object) {
return [include]
} else {
@ -92,7 +121,7 @@ public struct SpecFile {
let jsonDictionary = try SpecFile.loadDictionary(path: path).expand(variables: variables)
let includes = Include.parse(json: jsonDictionary["include"])
let includes = Include.parse(json: jsonDictionary["include"], basePath: basePath)
let subSpecs: [SpecFile] = try includes
.filter(\.enable)
.map { include in

View File

@ -286,10 +286,12 @@ public class SchemeGenerator {
let testPlans = scheme.test?.testPlans.enumerated().map { index, testPlan in
XCScheme.TestPlanReference(reference: "container:\(testPlan.path)", default: defaultTestPlanIndex == index)
} ?? []
let testBuildableEntries = buildActionEntries.filter({ $0.buildFor.contains(.testing) }) + testBuildTargetEntries
let testMacroExpansionBuildableRef = testBuildableEntries.map(\.buildableReference).contains(buildableReference) ? buildableReference : testBuildableEntries.first?.buildableReference
let testAction = XCScheme.TestAction(
buildConfiguration: scheme.test?.config ?? defaultDebugConfig.name,
macroExpansion: buildableReference,
macroExpansion: testMacroExpansionBuildableRef,
testables: testables,
testPlans: testPlans.isEmpty ? nil : testPlans,
preActions: scheme.test?.preActions.map(getExecutionAction) ?? [],

View File

@ -479,6 +479,46 @@ class SchemeGeneratorTests: XCTestCase {
try expect(xcscheme.launchAction?.macroExpansion?.buildableName) == "MyApp.app"
}
$0.it("generates scheme with macroExpansion from tests when the main target is not part of the scheme") {
let app = Target(
name: "MyApp",
type: .application,
platform: .iOS,
dependencies: []
)
let mockApp = Target(
name: "MockApp",
type: .application,
platform: .iOS,
dependencies: []
)
let testBundle = Target(
name: "TestBundle",
type: .unitTestBundle,
platform: .iOS
)
let appTarget = Scheme.BuildTarget(target: .local(app.name), buildTypes: [.running])
let mockAppTarget = Scheme.BuildTarget(target: .local(mockApp.name), buildTypes: [.testing])
let testBundleTarget = Scheme.BuildTarget(target: .local(testBundle.name), buildTypes: [.testing])
let scheme = Scheme(
name: "TestScheme",
build: Scheme.Build(targets: [appTarget, mockAppTarget, testBundleTarget]),
run: Scheme.Run(config: "Debug", macroExpansion: "MyApp")
)
let project = Project(
name: "test",
targets: [app, mockApp, testBundle],
schemes: [scheme]
)
let xcodeProject = try project.generateXcodeProject()
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
try expect(xcscheme.testAction?.macroExpansion?.buildableName) == "MockApp.app"
}
$0.it("generates scheme with test target of local swift package") {
let targetScheme = TargetScheme(
testTargets: [Scheme.Test.TestTarget(targetReference: TestableTargetReference(name: "XcodeGenKitTests", location: .package("XcodeGen")))])