Skip to content

Commit 3226d68

Browse files
committed
Cleanup testing assertions and canImport(TestingDisabled)
1 parent 68fa0ab commit 3226d68

File tree

4 files changed

+37
-68
lines changed

4 files changed

+37
-68
lines changed

Sources/_InternalTestSupport/BuildSystemProvider+Supported.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Testing
1314
import struct SPMBuildCore.BuildSystemProvider
1415
import enum PackageModel.BuildConfiguration
1516

@@ -28,6 +29,12 @@ public struct BuildData {
2829
public let config: BuildConfiguration
2930
}
3031

32+
extension BuildData: CustomTestStringConvertible {
33+
public var testDescription: String {
34+
return "\(buildSystem):\(config)"
35+
}
36+
}
37+
3138
public func getBuildData(for buildSystems: [BuildSystemProvider.Kind]) -> [BuildData] {
3239
buildSystems.flatMap { buildSystem in
3340
BuildConfiguration.allCases.compactMap { config in

Sources/_InternalTestSupport/SwiftTesting+Helpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public func requireFileExists(
6161
/// - path: The absolute path to check for file non-existence.
6262
/// - comment: An optional comment to include in the failure message.
6363
/// - sourceLocation: The source location where the expectation is made.
64-
public func expectFileDoesNotExists(
64+
public func expectFileDoesNotExist(
6565
at path: AbsolutePath,
6666
_ comment: Comment? = nil,
6767
sourceLocation: SourceLocation = #_sourceLocation,

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,7 @@ struct PackageCommandTests {
28662866
configuration: data.config,
28672867
buildSystem: data.buildSystem,
28682868
)
2869-
expectFileDoesNotExists(at: binFile)
2869+
expectFileDoesNotExist(at: binFile)
28702870
// Clean again to ensure we get no error.
28712871
_ = try await execute(
28722872
["clean"],
@@ -2911,7 +2911,7 @@ struct PackageCommandTests {
29112911
configuration: data.config,
29122912
buildSystem: data.buildSystem,
29132913
)
2914-
expectFileDoesNotExists(at: binFile)
2914+
expectFileDoesNotExist(at: binFile)
29152915
try #expect(
29162916
!localFileSystem.getDirectoryContents(buildPath.appending("repositories")).isEmpty
29172917
)
@@ -5341,7 +5341,7 @@ struct PackageCommandTests {
53415341
buildSystem: buildData.buildSystem,
53425342
)
53435343
expectFileIsExecutable(at: fixturePath.appending(components: debugTarget), "build-target")
5344-
expectFileDoesNotExists(
5344+
expectFileDoesNotExist(
53455345
at: fixturePath.appending(components: releaseTarget),
53465346
"build-target build-inherit"
53475347
)
@@ -5375,7 +5375,7 @@ struct PackageCommandTests {
53755375
at: fixturePath.appending(components: debugTarget),
53765376
"build-target build-debug"
53775377
)
5378-
expectFileDoesNotExists(
5378+
expectFileDoesNotExist(
53795379
at: fixturePath.appending(components: releaseTarget),
53805380
"build-target build-inherit"
53815381
)
@@ -5406,7 +5406,7 @@ struct PackageCommandTests {
54065406
configuration: buildData.config,
54075407
buildSystem: buildData.buildSystem,
54085408
)
5409-
expectFileDoesNotExists(
5409+
expectFileDoesNotExist(
54105410
at: fixturePath.appending(components: debugTarget),
54115411
"build-target build-inherit"
54125412
)
@@ -5450,7 +5450,7 @@ struct PackageCommandTests {
54505450
fileShouldNotExist = fixturePath.appending(components: debugTarget)
54515451
fileShouldExist = fixturePath.appending(components: releaseTarget)
54525452
}
5453-
expectFileDoesNotExists(at: fileShouldNotExist, "build-target build-inherit")
5453+
expectFileDoesNotExist(at: fileShouldNotExist, "build-target build-inherit")
54545454
expectFileIsExecutable(at: fileShouldExist, "build-target build-inherit")
54555455
}
54565456
} when: {

Tests/WorkspaceTests/InitTests.swift

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ struct InitTests {
104104
#expect(manifestContents.contains(".testTarget("))
105105
}
106106

107-
try customVerification?(path, packageName)
108-
109107
if let buildSystem = buildSystem {
110108
await expectBuilds(path, buildSystem: buildSystem, configurations: Set([buildConfiguration]))
111109

112110
try verifyBuildProducts(for: packageType, at: path, name: packageName, buildSystem: buildSystem, buildConfiguration: buildConfiguration)
113111
}
112+
113+
try customVerification?(path, packageName)
114114
}
115115
}
116116

@@ -152,28 +152,11 @@ struct InitTests {
152152
if hasSwiftTesting {
153153
#expect(testFileContents.contains(#"import Testing"#))
154154
#expect(testFileContents.contains(#"@Test func"#))
155-
} else {
156-
#expect(!testFileContents.contains(#"import Testing"#))
157-
#expect(!testFileContents.contains(#"@Test func"#))
158155
}
159156

160157
if hasXCTest {
161158
#expect(testFileContents.contains(#"import XCTest"#))
162159
#expect(testFileContents.contains("func test"))
163-
164-
if hasSwiftTesting {
165-
// When both are present, ensure XCTest content is properly formatted
166-
#expect(testFileContents.contains("import XCTest"), "XCTest import should be present")
167-
} else {
168-
// When only XCTest is present, ensure it's at the beginning of the file
169-
#expect(testFileContents.hasPrefix("import XCTest"), """
170-
Validates formatting of XCTest source file, in particular that it does not contain leading whitespace:
171-
\(testFileContents)
172-
""")
173-
}
174-
} else {
175-
#expect(!testFileContents.contains(#"import XCTest"#))
176-
#expect(!testFileContents.contains("func test"))
177160
}
178161
}
179162

@@ -274,7 +257,7 @@ struct InitTests {
274257
try await createAndVerifyPackage(
275258
packageType: packageType,
276259
supportedTestingLibraries: [.xctest],
277-
buildSystem: buildSystemRespectingTestingDisabled(data.buildSystem),
260+
buildSystem: data.buildSystem,
278261
buildConfiguration: data.config,
279262
customVerification: { path, name in
280263
#expect(
@@ -299,21 +282,17 @@ struct InitTests {
299282
try await createAndVerifyPackage(
300283
packageType: packageType,
301284
supportedTestingLibraries: [.swiftTesting],
302-
buildSystem: buildSystemRespectingTestingDisabled(data.buildSystem),
285+
buildSystem: data.buildSystem,
303286
buildConfiguration: data.config,
304287
customVerification: { path, name in
305288
try verifyTestFileContents(at: path, name: name, hasSwiftTesting: true, hasXCTest: false)
289+
let binPath = try data.buildSystem.binPath(for: data.config, triple: Self.targetTriple.platformBuildPathComponent)
290+
let swiftModule = "\(name).swiftmodule"
291+
let expectedPath = path
292+
.appending(components: binPath)
293+
.appending(components: data.buildSystem == .native ? ["Modules", swiftModule] : [swiftModule])
306294

307-
#if canImport(TestingDisabled)
308-
let expectedPath = path.appending(components:
309-
".build",
310-
Self.targetTriple.platformBuildPathComponent,
311-
"debug",
312-
"Modules",
313-
"\(name).swiftmodule"
314-
)
315-
try requireFileExists(at: expectedPath)
316-
#endif
295+
expectFileExists(at: expectedPath)
317296
}
318297
)
319298
}
@@ -324,21 +303,17 @@ struct InitTests {
324303
try await createAndVerifyPackage(
325304
packageType: packageType,
326305
supportedTestingLibraries: [.swiftTesting, .xctest],
327-
buildSystem: buildSystemRespectingTestingDisabled(data.buildSystem),
306+
buildSystem: data.buildSystem,
328307
buildConfiguration: data.config,
329308
customVerification: { path, name in
330309
try verifyTestFileContents(at: path, name: name, hasSwiftTesting: true, hasXCTest: true)
310+
let binPath = try data.buildSystem.binPath(for: data.config, triple: Self.targetTriple.platformBuildPathComponent)
311+
let swiftModule = "\(name).swiftmodule"
312+
let expectedPath = path
313+
.appending(components: binPath)
314+
.appending(components: data.buildSystem == .native ? ["Modules", swiftModule] : [swiftModule])
331315

332-
#if canImport(TestingDisabled)
333-
let expectedPath = path.appending(components:
334-
".build",
335-
Self.targetTriple.platformBuildPathComponent,
336-
"debug",
337-
"Modules",
338-
"\(name).swiftmodule"
339-
)
340316
expectFileExists(at: expectedPath)
341-
#endif
342317
}
343318
)
344319
}
@@ -349,24 +324,21 @@ struct InitTests {
349324
try await createAndVerifyPackage(
350325
packageType: packageType,
351326
supportedTestingLibraries: [],
352-
buildSystem: buildSystemRespectingTestingDisabled(data.buildSystem),
327+
buildSystem: data.buildSystem,
353328
buildConfiguration: data.config,
354329
customVerification: { path, name in
355330
let manifestContents: String = try localFileSystem.readFileContents(path.appending("Package.swift"))
356331
#expect(!manifestContents.contains(#".testTarget"#))
357332

358333
expectDirectoryDoesNotExist(at: path.appending("Tests"))
359334

360-
#if canImport(TestingDisabled)
361-
let expectedPath = path.appending(components:
362-
".build",
363-
Self.targetTriple.platformBuildPathComponent,
364-
"debug",
365-
"Modules",
366-
"\(name).swiftmodule"
367-
)
335+
let binPath = try data.buildSystem.binPath(for: data.config, triple: Self.targetTriple.platformBuildPathComponent)
336+
let swiftModule = "\(name).swiftmodule"
337+
let expectedPath = path
338+
.appending(components: binPath)
339+
.appending(components: data.buildSystem == .native ? ["Modules", swiftModule] : [swiftModule])
340+
368341
expectFileExists(at: expectedPath)
369-
#endif
370342
}
371343
)
372344
}
@@ -513,14 +485,4 @@ let package = Package(
513485
)
514486
"""
515487
}
516-
517-
/// Returns the build system to use, respecting whether TestingDisabled is imported.
518-
func buildSystemRespectingTestingDisabled(_ buildSystem: BuildSystemProvider.Kind?) -> BuildSystemProvider.Kind? {
519-
#if canImport(TestingDisabled)
520-
return buildSystem
521-
#else
522-
return nil
523-
#endif
524-
}
525-
526488
}

0 commit comments

Comments
 (0)