Skip to content

Commit a4b2da7

Browse files
authored
Tests: Migrate more suites to Swift Testing (#8993)
Migrate additional suites to Swift Testing. Namely `MermaidPackageSerializerTests`, `PackageRegistryCommandTests` and `WorkspaceStateTests`. Where applicable, augment the test suites to buid against the Native and Swift Build build engines, and to build using the `debug` and `release` configurations. Depends on #8988 Relates to #8997
1 parent 2669cb7 commit a4b2da7

File tree

6 files changed

+513
-317
lines changed

6 files changed

+513
-317
lines changed

Sources/_InternalTestSupport/SwiftTesting+Helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public func expectThrowsCommandExecutionError<T>(
4444
/// An `async`-friendly replacement for `XCTAssertThrowsError`.
4545
public func expectAsyncThrowsError<T>(
4646
_ expression: @autoclosure () async throws -> T,
47-
_ message: @autoclosure () -> Comment = "",
47+
_ message: @autoclosure () -> Comment? = nil,
4848
sourceLocation: SourceLocation = #_sourceLocation,
4949
_ errorHandler: (_ error: any Error) -> Void = { _ in }
5050
) async {
5151
do {
5252
_ = try await expression()
53-
Issue.record(message(), sourceLocation: sourceLocation)
53+
Issue.record(message() ?? "Expected an error, which did not not.", sourceLocation: sourceLocation)
5454
} catch {
5555
errorHandler(error)
5656
}

Sources/_InternalTestSupport/SwiftTesting+Tags.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extension Tag.Feature {
3636

3737
extension Tag.Feature.Command {
3838
public enum Package {}
39+
public enum PackageRegistry {}
3940
@Tag public static var Build: Tag
4041
@Tag public static var Test: Tag
4142
@Tag public static var Run: Tag
@@ -53,6 +54,15 @@ extension Tag.Feature.Command.Package {
5354
@Tag public static var Update: Tag
5455
}
5556

57+
extension Tag.Feature.Command.PackageRegistry {
58+
@Tag public static var General: Tag
59+
@Tag public static var Login: Tag
60+
@Tag public static var Logout: Tag
61+
@Tag public static var Publish: Tag
62+
@Tag public static var Set: Tag
63+
@Tag public static var Unset: Tag
64+
}
65+
5666
extension Tag.Feature.PackageType {
5767
@Tag public static var Library: Tag
5868
@Tag public static var Executable: Tag

Sources/_InternalTestSupport/SwiftTesting+TraitConditional.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import class PackageModel.UserToolchain
1515
import DriverSupport
1616
import Basics
1717
import Testing
18+
import TSCclibc // for SPM_posix_spawn_file_actions_addchdir_np_supported
1819

1920
extension Trait where Self == Testing.ConditionTrait {
2021
/// Skip test if the host operating system does not match the running OS.
@@ -67,6 +68,22 @@ extension Trait where Self == Testing.ConditionTrait {
6768
requiresHostLibrary(lib: "libSwiftSyntaxMacros.dylib")
6869
}
6970

71+
/// Ensure platform support working directory
72+
public static var requiresWorkingDirectorySupport: Self {
73+
enabled("working directory not supported on this platform") {
74+
#if !os(Windows)
75+
// needed for archiving
76+
if SPM_posix_spawn_file_actions_addchdir_np_supported() {
77+
return true
78+
} else {
79+
return false
80+
}
81+
#else
82+
return true
83+
#endif
84+
}
85+
}
86+
7087
/// Skip test unconditionally
7188
public static func skip(_ comment: Comment? = nil) -> Self {
7289
disabled(comment ?? "Unconditional skip, a comment should be added for the reason") { true }

Tests/CommandsTests/MermaidPackageSerializerTests.swift

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213

1314
import class Basics.InMemoryFileSystem
1415
import class Basics.ObservabilitySystem
@@ -24,14 +25,20 @@ import func _InternalTestSupport.XCTAssertNoDiagnostics
2425
@testable
2526
import Commands
2627

27-
import XCTest
28+
import Testing
29+
import _InternalTestSupport
2830

29-
final class MermaidPackageSerializerTests: XCTestCase {
30-
func testSimplePackage() throws {
31+
struct MermaidPackageSerializerTests {
32+
@Test(
33+
.tags(
34+
.TestSize.medium, //?
35+
),
36+
)
37+
func simplePackage() throws {
3138
let observability = ObservabilitySystem.makeForTesting()
3239
let fileSystem = InMemoryFileSystem(
3340
emptyFiles:
34-
"/A/Sources/ATarget/main.swift",
41+
"/A/Sources/ATarget/main.swift",
3542
"/A/Tests/ATargetTests/TestCases.swift"
3643
)
3744
let graph = try loadModulesGraph(
@@ -48,14 +55,12 @@ final class MermaidPackageSerializerTests: XCTestCase {
4855
],
4956
observabilityScope: observability.topScope
5057
)
51-
XCTAssertNoDiagnostics(observability.diagnostics)
58+
expectNoDiagnostics(observability.diagnostics)
5259

53-
XCTAssertEqual(graph.packages.count, 1)
54-
let package = try XCTUnwrap(graph.packages.first)
60+
#expect(graph.packages.count == 1)
61+
let package = try #require(graph.packages.first)
5562
let serializer = MermaidPackageSerializer(package: package.underlying)
56-
XCTAssertEqual(
57-
serializer.renderedMarkdown,
58-
"""
63+
#expect(serializer.renderedMarkdown == """
5964
```mermaid
6065
flowchart TB
6166
subgraph a
@@ -65,14 +70,18 @@ final class MermaidPackageSerializerTests: XCTestCase {
6570
end
6671
```
6772
68-
"""
69-
)
73+
""")
7074
}
7175

72-
func testDependenciesOnProducts() throws {
76+
@Test(
77+
.tags(
78+
.TestSize.medium, //?
79+
),
80+
)
81+
func dependenciesOnProducts() throws {
7382
let fileSystem = InMemoryFileSystem(
7483
emptyFiles:
75-
"/A/Sources/ATarget/foo.swift",
84+
"/A/Sources/ATarget/foo.swift",
7685
"/A/Tests/ATargetTests/foo.swift",
7786
"/B/Sources/BTarget/foo.swift",
7887
"/B/Tests/BTargetTests/foo.swift"
@@ -107,14 +116,12 @@ final class MermaidPackageSerializerTests: XCTestCase {
107116
],
108117
observabilityScope: observability.topScope
109118
)
110-
XCTAssertNoDiagnostics(observability.diagnostics)
119+
expectNoDiagnostics(observability.diagnostics)
111120

112-
XCTAssertEqual(graph.packages.count, 2)
113-
let package = try XCTUnwrap(graph.package(for: .plain("A")))
121+
#expect(graph.packages.count == 2)
122+
let package = try #require(graph.package(for: .plain("A")))
114123
let serializer = MermaidPackageSerializer(package: package.underlying)
115-
XCTAssertEqual(
116-
serializer.renderedMarkdown,
117-
"""
124+
#expect(serializer.renderedMarkdown == """
118125
```mermaid
119126
flowchart TB
120127
subgraph a
@@ -124,14 +131,18 @@ final class MermaidPackageSerializerTests: XCTestCase {
124131
end
125132
```
126133
127-
"""
128-
)
134+
""")
129135
}
130136

131-
func testDependenciesOnPackages() throws {
137+
@Test(
138+
.tags(
139+
.TestSize.medium, //?
140+
),
141+
)
142+
func dependenciesOnPackages() throws {
132143
let fileSystem = InMemoryFileSystem(
133144
emptyFiles:
134-
"/A/Sources/ATarget/foo.swift",
145+
"/A/Sources/ATarget/foo.swift",
135146
"/A/Tests/ATargetTests/foo.swift",
136147
"/B/Sources/BTarget/foo.swift",
137148
"/B/Tests/BTargetTests/foo.swift"
@@ -166,14 +177,12 @@ final class MermaidPackageSerializerTests: XCTestCase {
166177
],
167178
observabilityScope: observability.topScope
168179
)
169-
XCTAssertNoDiagnostics(observability.diagnostics)
180+
expectNoDiagnostics(observability.diagnostics)
170181

171-
XCTAssertEqual(graph.packages.count, 2)
172-
let package = try XCTUnwrap(graph.package(for: .plain("A")))
182+
#expect(graph.packages.count == 2)
183+
let package = try #require(graph.package(for: .plain("A")))
173184
let serializer = MermaidPackageSerializer(package: package.underlying)
174-
XCTAssertEqual(
175-
serializer.renderedMarkdown,
176-
"""
185+
#expect(serializer.renderedMarkdown == """
177186
```mermaid
178187
flowchart TB
179188
subgraph a
@@ -186,7 +195,6 @@ final class MermaidPackageSerializerTests: XCTestCase {
186195
end
187196
```
188197
189-
"""
190-
)
198+
""")
191199
}
192200
}

0 commit comments

Comments
 (0)