-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathProvisioningTests.swift
209 lines (181 loc) · 9.43 KB
/
ProvisioningTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import Testing
@_spi(Testing) import SWBCore
import SWBTestSupport
import SWBUtil
import SWBMacro
private func createMockProject() throws -> Project {
let projectPIF: [String: PropertyListItem] = [
"guid": "some-project-guid",
"path": "/tmp/SomeProject/aProject.xcodeproj",
"projectDirectory": "/tmp/SomeOtherPlace",
"targets": [],
"groupTree": [
"guid": "some-fileGroup-guid",
"type": "group",
"name": "SomeFiles",
"sourceTree": "PROJECT_DIR",
"path": "/tmp/SomeProject/SomeFiles",
],
"buildConfigurations": [],
"defaultConfigurationName": "Debug",
"developmentRegion": "English",
]
let pifLoader = PIFLoader(data: [], namespace: BuiltinMacros.namespace)
return try Project(fromDictionary: projectPIF, signature: "mock", withPIFLoader: pifLoader)
}
private func setupMacroEvaluationScope(_ settings: [MacroDeclaration:String] = [MacroDeclaration:String](), tableSetupHandler: ((inout MacroValueAssignmentTable) -> Void)? = nil) -> MacroEvaluationScope{
var table = MacroValueAssignmentTable(namespace: BuiltinMacros.namespace)
for (key, value) in settings {
switch key {
case let key as StringMacroDeclaration:
table.push(key, literal: value)
case let key as PathMacroDeclaration:
table.push(key, literal: value)
default:
fatalError("Unknown MarcoDeclaration \(key)")
}
}
tableSetupHandler?(&table)
return MacroEvaluationScope(table: table)
}
@Suite fileprivate struct ProvisioningTests: CoreBasedTests {
@Test
func computeBundleIdentifier() {
let bundleIdentifierFromInfoPlist = BuiltinMacros.namespace.parseString("com.apple.$(PRODUCT_NAME)")
let scope = setupMacroEvaluationScope([ BuiltinMacros.PRODUCT_BUNDLE_IDENTIFIER: "BundleID" ])
var result = SWBCore.computeBundleIdentifier(from: scope, bundleIdentifierFromInfoPlist: bundleIdentifierFromInfoPlist)
#expect(result == "BundleID")
let scopeWithoutBundleID = setupMacroEvaluationScope([ BuiltinMacros.PRODUCT_NAME: "ProductName" ])
result = SWBCore.computeBundleIdentifier(from: scopeWithoutBundleID, bundleIdentifierFromInfoPlist: bundleIdentifierFromInfoPlist)
#expect(result == "com.apple.ProductName")
}
@Test
func computeBundleIdentifierForTestRunner() {
let bundleIdentifierFromInfoPlist = BuiltinMacros.namespace.parseString("com.apple.$(PRODUCT_NAME)")
var table = MacroValueAssignmentTable(namespace: BuiltinMacros.namespace)
table.push(BuiltinMacros.PRODUCT_BUNDLE_IDENTIFIER, literal: "BundleID")
table.push(BuiltinMacros.USES_XCTRUNNER, literal: true)
let scope = MacroEvaluationScope(table: table)
var result = SWBCore.computeBundleIdentifier(from: scope, bundleIdentifierFromInfoPlist: bundleIdentifierFromInfoPlist)
#expect(result == "BundleID.xctrunner")
let scopeWithoutBundleID = setupMacroEvaluationScope([ BuiltinMacros.PRODUCT_NAME: "ProductName" ])
result = SWBCore.computeBundleIdentifier(from: scopeWithoutBundleID, bundleIdentifierFromInfoPlist: bundleIdentifierFromInfoPlist)
#expect(result == "com.apple.ProductName")
}
@Test(.requireSDKs(.iOS))
func computeSigningCertificateIdentifier() async throws {
let scope = setupMacroEvaluationScope([ BuiltinMacros.CODE_SIGN_IDENTITY: "MyIdentity" ])
do {
if let platform = try await getCore().platformRegistry.lookup(identifier: "com.apple.platform.iphoneos") {
let result = SWBCore.computeSigningCertificateIdentifier(from: scope, platform: platform)
#expect(result == "MyIdentity")
} else {
Issue.record("couldn't lookup platform com.apple.platform.iphoneos")
}
}
do {
if let platform = try await getCore().platformRegistry.lookup(identifier: "com.apple.platform.iphonesimulator") {
let result = SWBCore.computeSigningCertificateIdentifier(from: scope, platform: platform)
#expect(result == "-") // always Ad-hoc sign for simulator even if CODE_SIGN_IDENTITY has been overridden
} else {
Issue.record("couldn't lookup platform com.apple.platform.iphonesimulator")
}
}
}
@Test(.requireSDKs(.iOS))
func lookupEntitlementsFilePath() async throws {
let core = try await getCore()
let project = try createMockProject()
guard let sdk = core.sdkRegistry.lookup("iphoneos") else {
Issue.record("couldn't lookup SDK iphoneos")
return
}
let fs = PseudoFS()
let entitlementsPath = Path("/Entitlements/entitlements.plist")
let sdkEntitlementsPath = sdk.path.join(Path("Entitlements.plist"))
let scope = setupMacroEvaluationScope([ BuiltinMacros.CODE_SIGN_ENTITLEMENTS: entitlementsPath.str ])
var result = SWBCore.lookupEntitlementsFilePath(from: scope, project: project, sdk: sdk, fs: fs)
#expect(result == entitlementsPath) // FIXME: Path isn't checked for existence, is that correct?
try fs.createDirectory(entitlementsPath.dirname, recursive: false)
try fs.write(entitlementsPath, contents: "foo = bar")
result = SWBCore.lookupEntitlementsFilePath(from: scope, project: project, sdk: sdk, fs: fs)
#expect(result == entitlementsPath)
let emptyScopeWithRequiredEntitlements = setupMacroEvaluationScope() {
$0.push(BuiltinMacros.ENTITLEMENTS_REQUIRED, literal: true)
}
result = SWBCore.lookupEntitlementsFilePath(from: emptyScopeWithRequiredEntitlements, project: project, sdk: sdk, fs: fs)
#expect(result == nil)
try fs.createDirectory(sdkEntitlementsPath.dirname, recursive: true)
try fs.write(sdkEntitlementsPath, contents: "foo = bar")
result = SWBCore.lookupEntitlementsFilePath(from: emptyScopeWithRequiredEntitlements, project: project, sdk: sdk, fs: fs)
#expect(result == sdkEntitlementsPath)
let emptyScope = setupMacroEvaluationScope() {
$0.push(BuiltinMacros.ENTITLEMENTS_REQUIRED, literal: false)
}
result = SWBCore.lookupEntitlementsFilePath(from: emptyScope, project: project, sdk: sdk, fs: fs)
#expect(result == nil)
}
/// Tests overriding the provisioning style using the CODE_SIGN_STYLE build setting.
@Test
func provisioningStyle() async throws {
let core = try await getCore()
let testWorkspace = try TestWorkspace(
"aWorkspace",
projects: [
TestProject(
"aProject",
groupTree: TestGroup(
"Sources",
children: [
TestFile("aClass.m"),
]
),
buildConfigurations:[
TestBuildConfiguration("Debug", buildSettings: ["PRODUCT_NAME": "$(TARGET_NAME)",])
],
targets: [
TestStandardTarget(
"AppTarget",
buildConfigurations: [
TestBuildConfiguration("Debug"),
],
buildPhases: [
TestSourcesBuildPhase(["aClass.m"]),
],
provisioningSourceData: [
ProvisioningSourceData(configurationName: "Debug", appIDHasFeaturesEnabled: false, provisioningStyle: .automatic, bundleIdentifierFromInfoPlist: "AppTarget"),
]
)
]
)
]
).load(core)
let workspaceContext = WorkspaceContext(core: core, workspace: testWorkspace, processExecutionCache: .sharedForTesting)
let buildRequestContext = BuildRequestContext(workspaceContext: workspaceContext)
guard let target = testWorkspace.projects.first?.targets.first as? StandardTarget else {
Issue.record("unable to find target in test workspace")
return
}
#expect(target.provisioningSourceData(for: "Debug")?.provisioningStyle == .automatic)
do {
let scope = buildRequestContext.getCachedSettings(BuildParameters(configuration: "Debug", commandLineOverrides: ["CODE_SIGN_STYLE": "Automatic"]), target: target).globalScope
#expect(target.provisioningSourceData(for: "Debug", scope: scope)?.provisioningStyle == .automatic)
}
do {
let scope = buildRequestContext.getCachedSettings(BuildParameters(configuration: "Debug", commandLineOverrides: ["CODE_SIGN_STYLE": "Manual"]), target: target).globalScope
#expect(target.provisioningSourceData(for: "Debug", scope: scope)?.provisioningStyle == .manual)
}
}
}