Skip to content

Commit 2cb180b

Browse files
committed
Tests: Update helpers to support both test libraries
The `_InternalTestSupport` and `_InternalBuildTestSupport` modules have XCTest assert helper functions, that, when called from a Swift Testing test, is essentially a no-op. During migration to Swift Testing, this could lead to false positives if the helpers function are not converted to use Swift Testing APIs. During the transition, update all helper functions to detect whether the caller is executing in the XCTest or Swift Testing context, and call the appropriate API given the library.
1 parent 06fcc03 commit 2cb180b

File tree

14 files changed

+189
-43
lines changed

14 files changed

+189
-43
lines changed

Sources/_InternalBuildTestSupport/MockBuildTestHelper.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import PackageModel
2323
import SPMBuildCore
2424
import TSCUtility
2525
import XCTest
26+
import Testing
2627

2728
public func mockBuildPlan(
2829
buildPath: AbsolutePath? = nil,
@@ -174,12 +175,30 @@ public struct BuildPlanResult {
174175
self.plan = plan
175176
}
176177

177-
public func checkTargetsCount(_ count: Int, file: StaticString = #file, line: UInt = #line) {
178-
XCTAssertEqual(self.targetMap.count, count, file: file, line: line)
178+
public func checkTargetsCount(
179+
_ count: Int,
180+
file: StaticString = #file,
181+
line: UInt = #line,
182+
sourceLocation: SourceLocation = #_sourceLocation,
183+
) {
184+
if Test.current != nil {
185+
#expect(self.targetMap.count == count, sourceLocation: sourceLocation)
186+
} else {
187+
XCTAssertEqual(self.targetMap.count, count, file: file, line: line)
188+
}
179189
}
180190

181-
public func checkProductsCount(_ count: Int, file: StaticString = #file, line: UInt = #line) {
182-
XCTAssertEqual(self.productMap.count, count, file: file, line: line)
191+
public func checkProductsCount(
192+
_ count: Int,
193+
file: StaticString = #file,
194+
line: UInt = #line,
195+
sourceLocation: SourceLocation = #_sourceLocation,
196+
) {
197+
if Test.current != nil {
198+
#expect(self.productMap.count == count, sourceLocation: sourceLocation)
199+
} else {
200+
XCTAssertEqual(self.productMap.count, count, file: file, line: line)
201+
}
183202
}
184203

185204
public func moduleBuildDescription(for name: String) throws -> Build.ModuleBuildDescription {

Sources/_InternalBuildTestSupport/PIFTester.swift

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import Basics
1414
import XCBuildSupport
1515
import XCTest
16+
import Testing
1617

1718
public func PIFTester(_ pif: PIF.TopLevelObject, _ body: (PIFWorkspaceTester) throws -> Void) throws {
1819
try body(PIFWorkspaceTester(workspace: pif.workspace))
@@ -36,10 +37,16 @@ public final class PIFWorkspaceTester {
3637
_ guid: PIF.GUID,
3738
file: StaticString = #file,
3839
line: UInt = #line,
40+
sourceLocation: SourceLocation = #_sourceLocation,
3941
body: (PIFProjectTester) -> Void
4042
) throws {
4143
guard let project = projectMap[guid] else {
42-
return XCTFail("project \(guid) not found", file: file, line: line)
44+
if Test.current != nil {
45+
Issue.record("project \(guid) not found", sourceLocation: sourceLocation)
46+
return
47+
} else {
48+
return XCTFail("project \(guid) not found", file: file, line: line)
49+
}
4350
}
4451

4552
body(try PIFProjectTester(project: project, targetMap: targetMap))
@@ -72,15 +79,26 @@ public final class PIFProjectTester {
7279
_ guid: PIF.GUID,
7380
file: StaticString = #file,
7481
line: UInt = #line,
82+
sourceLocation: SourceLocation = #_sourceLocation,
7583
body: ((PIFTargetTester) -> Void)? = nil
7684
) {
7785
guard let baseTarget = baseTarget(withGUID: guid) else {
7886
let guids = project.targets.map { $0.guid }.joined(separator: ", ")
79-
return XCTFail("target \(guid) not found among \(guids)", file: file, line: line)
87+
if Test.current != nil {
88+
Issue.record("target \(guid) not found among \(guids)", sourceLocation: sourceLocation)
89+
return
90+
} else {
91+
return XCTFail("target \(guid) not found among \(guids)", file: file, line: line)
92+
}
8093
}
8194

8295
guard let target = baseTarget as? PIF.Target else {
83-
return XCTFail("target \(guid) is not a standard target", file: file, line: line)
96+
if Test.current != nil {
97+
Issue.record("target \(guid) is not a standard target", sourceLocation: sourceLocation)
98+
return
99+
} else {
100+
return XCTFail("target \(guid) is not a standard target", file: file, line: line)
101+
}
84102
}
85103

86104
body?(PIFTargetTester(target: target, targetMap: targetMap, fileMap: fileMap))
@@ -90,26 +108,42 @@ public final class PIFProjectTester {
90108
_ guid: PIF.GUID,
91109
file: StaticString = #file,
92110
line: UInt = #line,
111+
sourceLocation: SourceLocation = #_sourceLocation,
93112
body: ((PIFTargetTester) -> Void)? = nil
94113
) {
95114
if baseTarget(withGUID: guid) != nil {
96-
XCTFail("target \(guid) found", file: file, line: line)
115+
if Test.current != nil {
116+
Issue.record("target \(guid) found", sourceLocation: sourceLocation)
117+
} else {
118+
XCTFail("target \(guid) found", file: file, line: line)
119+
}
97120
}
98121
}
99122

100123
public func checkAggregateTarget(
101124
_ guid: PIF.GUID,
102125
file: StaticString = #file,
103126
line: UInt = #line,
127+
sourceLocation: SourceLocation = #_sourceLocation,
104128
body: ((PIFAggregateTargetTester) -> Void)? = nil
105129
) {
106130
guard let baseTarget = baseTarget(withGUID: guid) else {
107131
let guids = project.targets.map { $0.guid }.joined(separator: ", ")
108-
return XCTFail("target \(guid) not found among \(guids)", file: file, line: line)
132+
if Test.current != nil {
133+
Issue.record("target \(guid) not found among \(guids)", sourceLocation: sourceLocation)
134+
return
135+
} else {
136+
return XCTFail("target \(guid) not found among \(guids)", file: file, line: line)
137+
}
109138
}
110139

111140
guard let target = baseTarget as? PIF.AggregateTarget else {
112-
return XCTFail("target \(guid) is not an aggregate target", file: file, line: line)
141+
if Test.current != nil {
142+
Issue.record("target \(guid) is not an aggregate target", sourceLocation: sourceLocation)
143+
return
144+
} else {
145+
return XCTFail("target \(guid) is not an aggregate target", file: file, line: line)
146+
}
113147
}
114148

115149
body?(PIFAggregateTargetTester(target: target, targetMap: targetMap, fileMap: fileMap))
@@ -119,11 +153,17 @@ public final class PIFProjectTester {
119153
_ name: String,
120154
file: StaticString = #file,
121155
line: UInt = #line,
156+
sourceLocation: SourceLocation = #_sourceLocation,
122157
body: (PIFBuildConfigurationTester) -> Void
123158
) {
124159
guard let configuration = buildConfiguration(withName: name) else {
125160
let names = project.buildConfigurations.map { $0.name }.joined(separator: ", ")
126-
return XCTFail("build configuration \(name) not found among \(names)", file: file, line: line)
161+
if Test.current != nil {
162+
Issue.record("build configuration \(name) not found among \(names)", sourceLocation: sourceLocation)
163+
return
164+
} else {
165+
return XCTFail("build configuration \(name) not found among \(names)", file: file, line: line)
166+
}
127167
}
128168

129169
body(PIFBuildConfigurationTester(buildConfiguration: configuration))
@@ -185,10 +225,16 @@ public class PIFBaseTargetTester {
185225
_ name: String,
186226
file: StaticString = #file,
187227
line: UInt = #line,
228+
sourceLocation: SourceLocation = #_sourceLocation,
188229
body: (PIFBuildConfigurationTester) -> Void
189230
) {
190231
guard let configuration = buildConfiguration(withName: name) else {
191-
return XCTFail("build configuration \(name) not found", file: file, line: line)
232+
if Test.current != nil {
233+
Issue.record("build configuration \(name) not found", sourceLocation: #_sourceLocation)
234+
return
235+
} else {
236+
return XCTFail("build configuration \(name) not found", file: file, line: line)
237+
}
192238
}
193239

194240
body(PIFBuildConfigurationTester(buildConfiguration: configuration))
@@ -319,20 +365,42 @@ public final class PIFBuildSettingsTester {
319365
}
320366
}
321367

322-
public func checkUncheckedSettings(file: StaticString = #file, line: UInt = #line) {
368+
public func checkUncheckedSettings(file: StaticString = #file, line: UInt = #line, sourceLocation: SourceLocation = #_sourceLocation) {
323369
let uncheckedKeys =
324370
Array(buildSettings.singleValueSettings.keys.map { $0.rawValue }) +
325371
Array(buildSettings.multipleValueSettings.keys.map { $0.rawValue })
326-
XCTAssert(uncheckedKeys.isEmpty, "settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
372+
if Test.current != nil {
373+
#expect(
374+
uncheckedKeys.isEmpty,
375+
"settings are left unchecked: \(uncheckedKeys)",
376+
sourceLocation: sourceLocation,
377+
)
378+
} else {
379+
XCTAssert(uncheckedKeys.isEmpty, "settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
380+
}
327381

328382
for (platform, settings) in buildSettings.platformSpecificSingleValueSettings {
329383
let uncheckedKeys = Array(settings.keys.map { $0.rawValue })
330-
XCTAssert(uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
384+
if Test.current != nil {
385+
#expect(
386+
uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)",
387+
sourceLocation: sourceLocation,
388+
)
389+
} else {
390+
XCTAssert(uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
391+
}
331392
}
332393

333394
for (platform, settings) in buildSettings.platformSpecificMultipleValueSettings {
334395
let uncheckedKeys = Array(settings.keys.map { $0.rawValue })
335-
XCTAssert(uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
396+
if Test.current != nil {
397+
#expect(
398+
uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)",
399+
sourceLocation: sourceLocation,
400+
)
401+
} else {
402+
XCTAssert(uncheckedKeys.isEmpty, "\(platform) settings are left unchecked: \(uncheckedKeys)", file: file, line: line)
403+
}
336404
}
337405
}
338406
}

Sources/_InternalTestSupport/MockBuildTestHelper.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import struct PackageGraph.ResolvedProduct
1818
import PackageModel
1919
import SPMBuildCore
2020
import TSCUtility
21-
import XCTest
2221

2322
public struct MockToolchain: PackageModel.Toolchain {
2423
#if os(Windows)

Sources/_InternalTestSupport/MockDependencyGraph.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
import XCTest
13+
import Testing
1314

1415
import PackageGraph
1516
import PackageModel
@@ -32,15 +33,30 @@ public struct MockDependencyGraph {
3233
public func checkResult(
3334
_ output: [(container: PackageReference, version: Version)],
3435
file: StaticString = #file,
35-
line: UInt = #line
36+
line: UInt = #line,
37+
sourceLocation: SourceLocation = #_sourceLocation
3638
) {
3739
var result = self.result
3840
for item in output {
39-
XCTAssertEqual(result[item.container], item.version, file: file, line: line)
41+
if Test.current != nil {
42+
#expect(
43+
result[item.container] == item.version,
44+
sourceLocation: sourceLocation,
45+
)
46+
} else {
47+
XCTAssertEqual(result[item.container], item.version, file: file, line: line)
48+
}
4049
result[item.container] = nil
4150
}
4251
if !result.isEmpty {
43-
XCTFail("Unchecked containers: \(result)", file: file, line: line)
52+
if Test.current != nil {
53+
Issue.record(
54+
"Unchecked containers: \(result)",
55+
sourceLocation: sourceLocation,
56+
)
57+
} else {
58+
XCTFail("Unchecked containers: \(result)", file: file, line: line)
59+
}
4460
}
4561
}
4662
}

Sources/_InternalTestSupport/MockPackageContainer.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Dispatch
1515
import PackageGraph
1616
import PackageModel
1717
import SourceControl
18-
import XCTest
1918

2019
import struct TSCUtility.Version
2120

0 commit comments

Comments
 (0)