Skip to content

Commit a0c00e3

Browse files
authored
Merge pull request #3697 from SwiftPackageIndex/issue-3655-swift-testing-part-18
Issue 3655 swift testing part 18
2 parents 1cd7211 + 0bf83cb commit a0c00e3

File tree

5 files changed

+218
-213
lines changed

5 files changed

+218
-213
lines changed

Tests/AppTests/MaintainerInfoIndexViewTests.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@
1414

1515
@testable import App
1616

17-
import XCTest
17+
import Testing
1818

19-
final class MaintainerInfoIndexViewTests: XCTestCase {
2019

21-
func test_spiManifestCommonUseCasesDocLink() throws {
22-
XCTAssertEqual(
23-
MaintainerInfoIndex.View.spiManifestCommonUseCasesDocLink(.linuxImages),
24-
"/SwiftPackageIndex/SPIManifest/~/documentation/spimanifest/commonusecases#linuxImages"
20+
@Suite struct MaintainerInfoIndexViewTests {
21+
22+
@Test func spiManifestCommonUseCasesDocLink() throws {
23+
#expect(
24+
MaintainerInfoIndex.View.spiManifestCommonUseCasesDocLink(.linuxImages) == "/SwiftPackageIndex/SPIManifest/~/documentation/spimanifest/commonusecases#linuxImages"
2525
)
2626
}
2727

28-
func test_spiManifestDocLink() throws {
29-
XCTAssertEqual(
30-
MaintainerInfoIndex.View.spiManifestDocLink(),
31-
"/SwiftPackageIndex/SPIManifest/~/documentation/spimanifest"
28+
@Test func spiManifestDocLink() throws {
29+
#expect(
30+
MaintainerInfoIndex.View.spiManifestDocLink() == "/SwiftPackageIndex/SPIManifest/~/documentation/spimanifest"
3231
)
3332
}
3433

Tests/AppTests/ManifestTests.swift

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Foundation
16+
1517
@testable import App
1618

17-
import XCTest
19+
import Testing
1820

1921

20-
class ManifestTests: XCTestCase {
22+
@Suite struct ManifestTests {
2123

22-
func test_decode_Product_Type() throws {
24+
@Test func decode_Product_Type() throws {
2325
// Test product type decoding.
2426
// JSON snippets via `swift package dump-package` from the following
2527
// Package.swift `products` definition:
@@ -45,8 +47,7 @@ class ManifestTests: XCTestCase {
4547
}
4648
}
4749
""".utf8)
48-
XCTAssertEqual(try JSONDecoder().decode(Test.self, from: data),
49-
.init(type: .executable))
50+
#expect(try JSONDecoder().decode(Test.self, from: data) == .init(type: .executable))
5051
}
5152
do { // lib - automatic
5253
let data = Data("""
@@ -56,8 +57,7 @@ class ManifestTests: XCTestCase {
5657
}
5758
}
5859
""".utf8)
59-
XCTAssertEqual(try JSONDecoder().decode(Test.self, from: data),
60-
.init(type: .library(.automatic)))
60+
#expect(try JSONDecoder().decode(Test.self, from: data) == .init(type: .library(.automatic)))
6161
}
6262
do { // lib - dynamic
6363
let data = Data("""
@@ -67,8 +67,7 @@ class ManifestTests: XCTestCase {
6767
}
6868
}
6969
""".utf8)
70-
XCTAssertEqual(try JSONDecoder().decode(Test.self, from: data),
71-
.init(type: .library(.dynamic)))
70+
#expect(try JSONDecoder().decode(Test.self, from: data) == .init(type: .library(.dynamic)))
7271
}
7372
do { // lib - static
7473
let data = Data("""
@@ -78,8 +77,7 @@ class ManifestTests: XCTestCase {
7877
}
7978
}
8079
""".utf8)
81-
XCTAssertEqual(try JSONDecoder().decode(Test.self, from: data),
82-
.init(type: .library(.static)))
80+
#expect(try JSONDecoder().decode(Test.self, from: data) == .init(type: .library(.static)))
8381
}
8482
do { // test
8583
let data = Data("""
@@ -89,30 +87,29 @@ class ManifestTests: XCTestCase {
8987
}
9088
}
9189
""".utf8)
92-
XCTAssertEqual(try JSONDecoder().decode(Test.self, from: data),
93-
.init(type: .test))
90+
#expect(try JSONDecoder().decode(Test.self, from: data) == .init(type: .test))
9491
}
9592
}
9693

97-
func test_decode_basic() throws {
94+
@Test func decode_basic() throws {
9895
let data = try fixtureData(for: "manifest-1.json")
9996
let m = try JSONDecoder().decode(Manifest.self, from: data)
100-
XCTAssertEqual(m.name, "SPI-Server")
101-
XCTAssertEqual(m.platforms, [.init(platformName: .macos, version: "10.15")])
102-
XCTAssertEqual(m.products, [.init(name: "Some Product",
97+
#expect(m.name == "SPI-Server")
98+
#expect(m.platforms == [.init(platformName: .macos, version: "10.15")])
99+
#expect(m.products == [.init(name: "Some Product",
103100
targets: ["t1", "t2"],
104101
type: .library(.automatic))])
105-
XCTAssertEqual(m.swiftLanguageVersions, ["4", "4.2", "5"])
106-
XCTAssertEqual(m.targets, [.init(name: "App", type: .regular),
102+
#expect(m.swiftLanguageVersions == ["4", "4.2", "5"])
103+
#expect(m.targets == [.init(name: "App", type: .regular),
107104
.init(name: "Run", type: .regular),
108105
.init(name: "AppTests", type: .test)])
109-
XCTAssertEqual(m.toolsVersion, .init(version: "5.2.0"))
106+
#expect(m.toolsVersion == .init(version: "5.2.0"))
110107
}
111108

112-
func test_decode_products_complex() throws {
109+
@Test func decode_products_complex() throws {
113110
let data = try fixtureData(for: "SwiftNIO.json")
114111
let m = try JSONDecoder().decode(Manifest.self, from: data)
115-
XCTAssertEqual(m.products, [
112+
#expect(m.products == [
116113
.init(name: "NIOEchoServer",
117114
targets: ["NIOEchoServer"],
118115
type: .executable),
@@ -176,28 +173,27 @@ class ManifestTests: XCTestCase {
176173
])
177174
}
178175

179-
func test_platform_list() throws {
176+
@Test func platform_list() throws {
180177
// Test to ensure the platforms listed in the DTO struct Manifest.Platform.Name
181178
// do not accidentally diverge from those in the db entity's Platform.Name
182-
XCTAssertEqual(Manifest.Platform.Name.allCases.map(\.rawValue).sorted(),
183-
Platform.Name.allCases.map(\.rawValue).sorted())
179+
#expect(Manifest.Platform.Name.allCases.map(\.rawValue).sorted() == Platform.Name.allCases.map(\.rawValue).sorted())
184180
}
185181

186-
func test_decode_plugin_products() throws {
182+
@Test func decode_plugin_products() throws {
187183
let data = try fixtureData(for: "manifest-plugin.json")
188184
let m = try JSONDecoder().decode(Manifest.self, from: data)
189-
XCTAssertEqual(m.products, [
185+
#expect(m.products == [
190186
.init(name: "Swift-DocC", targets: ["Swift-DocC"], type: .plugin),
191187
.init(name: "Swift-DocC Preview", targets: ["Swift-DocC Preview"], type: .plugin),
192188
])
193189
}
194190

195-
func test_issue_2875() throws {
191+
@Test func issue_2875() throws {
196192
// Support decoding custom platform with different capitalisation
197193
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2875
198194
let data = try fixtureData(for: "Lottie-ios.json")
199195
let m = try JSONDecoder().decode(Manifest.self, from: data)
200-
XCTAssertEqual(m.platforms, [
196+
#expect(m.platforms == [
201197
.init(platformName: .ios, version: "11.0"),
202198
.init(platformName: .macos, version: "10.11"),
203199
.init(platformName: .tvos, version: "11.0"),

Tests/AppTests/MastodonTests.swift

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
import Dependencies
1818
import SemanticVersion
19-
import XCTVapor
19+
import Testing
2020

2121

22-
final class MastodonTests: AppTestCase {
22+
@Suite struct MastodonTests {
2323

24-
func test_apiURL() throws {
24+
@Test func apiURL() throws {
2525
let url = try Mastodon.apiURL(with: "message")
26-
XCTAssert(url.contains("visibility=unlisted"), "was: \(url)")
26+
#expect(url.contains("visibility=unlisted"), "was: \(url)")
2727
}
2828

29-
func test_endToEnd() async throws {
29+
@Test func endToEnd() async throws {
3030
let message = QueueIsolated<String?>(nil)
3131
try await withDependencies {
3232
$0.environment.allowSocialPosts = { true }
@@ -52,7 +52,7 @@ final class MastodonTests: AppTestCase {
5252
if message.value == nil {
5353
message.setValue(msg)
5454
} else {
55-
XCTFail("message must only be set once")
55+
Issue.record("message must only be set once")
5656
}
5757
}
5858
$0.shell.run = { @Sendable cmd, path in
@@ -62,67 +62,69 @@ final class MastodonTests: AppTestCase {
6262
return ""
6363
}
6464
} operation: {
65-
// setup
66-
let url = "https://github.com/foo/bar"
67-
68-
try await withDependencies {
69-
$0.date.now = .now
70-
$0.packageListRepository.fetchPackageList = { @Sendable _ in [url.url] }
71-
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
72-
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
73-
$0.packageListRepository.fetchCustomCollection = { @Sendable _, _ in [] }
74-
} operation: {
75-
// run first two processing steps
76-
try await reconcile(client: app.client, database: app.db)
77-
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
78-
79-
// MUT - analyze, triggering the post
80-
try await Analyze.analyze(client: app.client,
81-
database: app.db,
82-
mode: .limit(10))
83-
84-
do {
85-
let msg = try XCTUnwrap(message.value)
86-
XCTAssertTrue(msg.hasPrefix("📦 foo just added a new package, Mock"), "was \(msg)")
87-
}
65+
try await withApp { app in
66+
// setup
67+
let url = "https://github.com/foo/bar"
8868

89-
// run stages again to simulate the cycle...
90-
message.setValue(nil)
91-
try await reconcile(client: app.client, database: app.db)
92-
}
69+
try await withDependencies {
70+
$0.date.now = .now
71+
$0.packageListRepository.fetchPackageList = { @Sendable _ in [url.url] }
72+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
73+
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
74+
$0.packageListRepository.fetchCustomCollection = { @Sendable _, _ in [] }
75+
} operation: {
76+
// run first two processing steps
77+
try await reconcile(client: app.client, database: app.db)
78+
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
9379

94-
try await withDependencies {
95-
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime)
96-
} operation: {
97-
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
80+
// MUT - analyze, triggering the post
81+
try await Analyze.analyze(client: app.client,
82+
database: app.db,
83+
mode: .limit(10))
9884

99-
// MUT - analyze, triggering posts if any
100-
try await Analyze.analyze(client: app.client,
101-
database: app.db,
102-
mode: .limit(10))
103-
}
85+
do {
86+
let msg = try #require(message.value)
87+
#expect(msg.hasPrefix("📦 foo just added a new package, Mock"), "was \(msg)")
88+
}
10489

105-
// validate - there are no new posts to send
106-
XCTAssertNil(message.value)
90+
// run stages again to simulate the cycle...
91+
message.setValue(nil)
92+
try await reconcile(client: app.client, database: app.db)
93+
}
10794

108-
// Now simulate receiving a package update: version 2.0.0
109-
try await withDependencies {
110-
$0.git.getTags = { @Sendable _ in [.tag(2, 0, 0)] }
111-
} operation: {
11295
try await withDependencies {
113-
// fast forward our clock by the deadtime interval again (*2) and re-ingest
114-
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime * 2)
96+
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime)
11597
} operation: {
11698
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
117-
// MUT - analyze again
99+
100+
// MUT - analyze, triggering posts if any
118101
try await Analyze.analyze(client: app.client,
119102
database: app.db,
120103
mode: .limit(10))
121104
}
122105

123-
// validate
124-
let msg = try XCTUnwrap(message.value)
125-
XCTAssertTrue(msg.hasPrefix("⬆️ foo just released Mock v2.0.0"), "was: \(msg)")
106+
// validate - there are no new posts to send
107+
#expect(message.value == nil)
108+
109+
// Now simulate receiving a package update: version 2.0.0
110+
try await withDependencies {
111+
$0.git.getTags = { @Sendable _ in [.tag(2, 0, 0)] }
112+
} operation: {
113+
try await withDependencies {
114+
// fast forward our clock by the deadtime interval again (*2) and re-ingest
115+
$0.date.now = .now.addingTimeInterval(Constants.reIngestionDeadtime * 2)
116+
} operation: {
117+
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
118+
// MUT - analyze again
119+
try await Analyze.analyze(client: app.client,
120+
database: app.db,
121+
mode: .limit(10))
122+
}
123+
124+
// validate
125+
let msg = try #require(message.value)
126+
#expect(msg.hasPrefix("⬆️ foo just released Mock v2.0.0"), "was: \(msg)")
127+
}
126128
}
127129
}
128130
}

0 commit comments

Comments
 (0)