Skip to content

Commit 72bb75f

Browse files
Merge pull request #3689 from SwiftPackageIndex/issue-3655-swift-testing-part-12
Issue 3655 swift testing part 12
2 parents a693b59 + 644b0a2 commit 72bb75f

File tree

4 files changed

+102
-88
lines changed

4 files changed

+102
-88
lines changed

Tests/AppTests/ErrorMiddlewareTests.swift

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

15-
import XCTest
16-
1715
@testable import App
1816

1917
import Dependencies
18+
import Testing
2019
import Vapor
2120

2221

23-
class ErrorMiddlewareTests: AppTestCase {
24-
25-
override func setUpWithError() throws {
26-
try super.setUpWithError()
22+
@Suite struct ErrorMiddlewareTests {
2723

24+
func setup(_ app: Application) async throws {
2825
// set up some test routes
2926
app.get("ok") { _ in return "ok" }
3027
app.get("404") { req async throws -> Response in throw Abort(.notFound) }
3128
app.get("500") { req async throws -> Response in throw Abort(.internalServerError) }
3229
}
3330

34-
func test_custom_routes() throws {
35-
// Test to ensure the test routes we've set up in setUpWithError are in effect
36-
try app.test(.GET, "ok", afterResponse: { response in
37-
XCTAssertEqual(response.status, .ok)
38-
XCTAssertEqual(response.body.asString(), "ok")
39-
})
31+
@Test func custom_routes() async throws {
32+
try await withApp(setup) { app in
33+
// Test to ensure the test routes we've set up in setUpWithError are in effect
34+
try await app.test(.GET, "ok", afterResponse: { response async in
35+
#expect(response.status == .ok)
36+
#expect(response.body.asString() == "ok")
37+
})
38+
}
4039
}
4140

42-
func test_html_error() throws {
41+
@Test func html_error() async throws {
4342
// Test to ensure errors are converted to html error pages via the ErrorMiddleware
44-
try withDependencies {
43+
try await withDependencies {
4544
$0.environment.dbId = { nil }
4645
} operation: {
47-
try app.test(.GET, "404", afterResponse: { response in
48-
XCTAssertEqual(response.content.contentType, .html)
49-
XCTAssert(response.body.asString().contains("404 - Not Found"))
50-
})
46+
try await withApp(setup) { app in
47+
try await app.test(.GET, "404", afterResponse: { response async in
48+
#expect(response.content.contentType == .html)
49+
#expect(response.body.asString().contains("404 - Not Found"))
50+
})
51+
}
5152
}
5253
}
5354

54-
func test_status_code() throws {
55+
@Test func status_code() async throws {
5556
// Ensure we're still reporting the actual status code even when serving html pages
5657
// (Status is important for Google ranking, see
5758
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/323)
58-
try withDependencies {
59+
try await withDependencies {
5960
$0.environment.dbId = { nil }
6061
} operation: {
61-
try app.test(.GET, "404", afterResponse: { response in
62-
XCTAssertEqual(response.status, .notFound)
63-
XCTAssertEqual(response.content.contentType, .html)
64-
})
65-
try app.test(.GET, "500", afterResponse: { response in
66-
XCTAssertEqual(response.status, .internalServerError)
67-
XCTAssertEqual(response.content.contentType, .html)
68-
})
62+
try await withApp(setup) { app in
63+
try await app.test(.GET, "404", afterResponse: { response async in
64+
#expect(response.status == .notFound)
65+
#expect(response.content.contentType == .html)
66+
})
67+
try await app.test(.GET, "500", afterResponse: { response async in
68+
#expect(response.status == .internalServerError)
69+
#expect(response.content.contentType == .html)
70+
})
71+
}
6972
}
7073
}
7174

Tests/AppTests/ErrorPageModelTests.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,32 @@
1414

1515
@testable import App
1616

17-
import XCTVapor
17+
import Testing
18+
import Vapor
1819

19-
class ErrorPageModelTests: AppTestCase {
2020

21-
func test_500() throws {
21+
@Suite struct ErrorPageModelTests {
22+
23+
@Test func error_500() throws {
2224
// setup
2325
let error = Abort(.internalServerError)
2426

2527
// MUT
2628
let model = ErrorPage.Model(error)
2729

2830
// validate
29-
XCTAssertEqual(model.errorMessage, "500 - Internal Server Error")
31+
#expect(model.errorMessage == "500 - Internal Server Error")
3032
}
3133

32-
func test_500_with_reason() throws {
34+
@Test func error_500_with_reason() throws {
3335
// setup
3436
let error = Abort(.internalServerError, reason: "Reason")
3537

3638
// MUT
3739
let model = ErrorPage.Model(error)
3840

3941
// validate
40-
XCTAssertEqual(model.errorMessage, "500 - Internal Server Error - Reason")
42+
#expect(model.errorMessage == "500 - Internal Server Error - Reason")
4143
}
4244

4345
}

Tests/AppTests/ErrorReportingTests.swift

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,53 @@
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

1719
import Dependencies
18-
import XCTVapor
20+
import Testing
21+
1922

23+
@Suite struct ErrorReportingTests {
2024

21-
class ErrorReportingTests: AppTestCase {
25+
let capturingLogger = CapturingLogger()
2226

23-
func test_Analyze_recordError() async throws {
24-
let pkg = try await savePackage(on: app.db, "1")
25-
try await Analyze.recordError(database: app.db,
26-
error: AppError.cacheDirectoryDoesNotExist(pkg.id, "path"))
27-
do {
28-
let pkg = try await XCTUnwrapAsync(try await Package.find(pkg.id, on: app.db))
29-
XCTAssertEqual(pkg.status, .cacheDirectoryDoesNotExist)
30-
XCTAssertEqual(pkg.processingStage, .analysis)
27+
@Test func Analyze_recordError() async throws {
28+
try await withApp { app in
29+
let pkg = try await savePackage(on: app.db, "1")
30+
try await Analyze.recordError(database: app.db,
31+
error: AppError.cacheDirectoryDoesNotExist(pkg.id, "path"))
32+
do {
33+
let pkg = try await XCTUnwrapAsync(try await Package.find(pkg.id, on: app.db))
34+
#expect(pkg.status == .cacheDirectoryDoesNotExist)
35+
#expect(pkg.processingStage == .analysis)
36+
}
3137
}
3238
}
3339

34-
func test_Ingestion_error_reporting() async throws {
35-
// setup
36-
try await Package(id: .id0, url: "1", processingStage: .reconciliation).save(on: app.db)
40+
@Test func Ingestion_error_reporting() async throws {
41+
try await withApp(logHandler: capturingLogger) { app in
42+
// setup
43+
try await Package(id: .id0, url: "1", processingStage: .reconciliation).save(on: app.db)
3744

38-
try await withDependencies {
39-
$0.date.now = .now
40-
$0.github.fetchMetadata = { @Sendable _, _ throws(Github.Error) in throw Github.Error.invalidURL("1") }
41-
} operation: {
42-
// MUT
43-
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
44-
}
45+
try await withDependencies {
46+
$0.date.now = .now
47+
$0.github.fetchMetadata = { @Sendable _, _ throws(Github.Error) in throw Github.Error.invalidURL("1") }
48+
} operation: {
49+
// MUT
50+
try await Ingestion.ingest(client: app.client, database: app.db, mode: .limit(10))
51+
}
4552

46-
// validation
47-
logger.logs.withValue {
48-
XCTAssertEqual($0, [.init(level: .warning,
49-
message: #"Ingestion.Error(\#(UUID.id0), invalidURL(1))"#)])
53+
// validation
54+
capturingLogger.logs.withValue {
55+
#expect($0 == [.init(level: .warning,
56+
message: #"Ingestion.Error(\#(UUID.id0), invalidURL(1))"#)])
57+
}
5058
}
5159
}
5260

53-
func test_Analyzer_error_reporting() async throws {
61+
@Test func Analyzer_error_reporting() async throws {
5462
try await withDependencies {
5563
$0.fileManager.fileExists = { @Sendable _ in true }
5664
$0.shell.run = { @Sendable cmd, _ in
@@ -60,37 +68,39 @@ class ErrorReportingTests: AppTestCase {
6068
return "invalid"
6169
}
6270
} operation: {
63-
// setup
64-
try await Package(id: .id1, url: "1".asGithubUrl.url, processingStage: .ingestion).save(on: app.db)
71+
try await withApp(logHandler: capturingLogger) { app in
72+
// setup
73+
try await Package(id: .id1, url: "1".asGithubUrl.url, processingStage: .ingestion).save(on: app.db)
6574

66-
// MUT
67-
try await Analyze.analyze(client: app.client,
68-
database: app.db,
69-
mode: .limit(10))
75+
// MUT
76+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
7077

71-
// validation
72-
logger.logs.withValue {
73-
XCTAssertEqual($0, [
74-
.init(level: .critical, message: "updatePackages: unusually high error rate: 1/1 = 100.0%"),
75-
.init(level: .warning, message: #"App.AppError.genericError(Optional(\#(UUID.id1)), "updateRepository: no repository")"#)
76-
])
78+
// validation
79+
capturingLogger.logs.withValue {
80+
#expect($0 == [
81+
.init(level: .critical, message: "updatePackages: unusually high error rate: 1/1 = 100.0%"),
82+
.init(level: .warning, message: #"App.AppError.genericError(Optional(\#(UUID.id1)), "updateRepository: no repository")"#)
83+
])
84+
}
7785
}
7886
}
7987
}
8088

81-
func test_invalidPackageCachePath() async throws {
89+
@Test func invalidPackageCachePath() async throws {
8290
try await withDependencies {
8391
$0.fileManager.fileExists = { @Sendable _ in true }
8492
} operation: {
85-
// setup
86-
try await savePackages(on: app.db, ["1", "2"], processingStage: .ingestion)
93+
try await withApp { app in
94+
// setup
95+
try await savePackages(on: app.db, ["1", "2"], processingStage: .ingestion)
8796

88-
// MUT
89-
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
97+
// MUT
98+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
9099

91-
// validation
92-
let packages = try await Package.query(on: app.db).sort(\.$url).all()
93-
XCTAssertEqual(packages.map(\.status), [.invalidCachePath, .invalidCachePath])
100+
// validation
101+
let packages = try await Package.query(on: app.db).sort(\.$url).all()
102+
#expect(packages.map(\.status) == [.invalidCachePath, .invalidCachePath])
103+
}
94104
}
95105
}
96106

Tests/AppTests/FundingLinkTests.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,26 @@
1414

1515
@testable import App
1616

17-
import Vapor
18-
import XCTest
17+
import Testing
1918

2019

21-
class FundingLinkTests: XCTestCase {
20+
@Suite struct FundingLinkTests {
2221

23-
func test_fundingLink_missingSchemeFix() async throws {
22+
@Test func fundingLink_missingSchemeFix() async throws {
2423
// URL with both a scheme and a host.
2524
let ghFundingLink1 = Github.Metadata.FundingLinkNode(platform: .customUrl, url: "https://example.com")
26-
let dbFundingLink1 = try XCTUnwrap(FundingLink(from: ghFundingLink1))
27-
XCTAssertEqual(dbFundingLink1.url, "https://example.com")
25+
let dbFundingLink1 = try #require(FundingLink(from: ghFundingLink1))
26+
#expect(dbFundingLink1.url == "https://example.com")
2827

2928
// URL with a host but no scheme.
3029
let ghFundingLink2 = Github.Metadata.FundingLinkNode(platform: .customUrl, url: "example.com")
31-
let dbFundingLink2 = try XCTUnwrap(FundingLink(from: ghFundingLink2))
32-
XCTAssertEqual(dbFundingLink2.url, "https://example.com")
30+
let dbFundingLink2 = try #require(FundingLink(from: ghFundingLink2))
31+
#expect(dbFundingLink2.url == "https://example.com")
3332

3433
// URL with neither.
3534
let ghFundingLink3 = Github.Metadata.FundingLinkNode(platform: .customUrl, url: "!@£$%")
3635
let dbFundingLink3 = FundingLink(from: ghFundingLink3)
37-
XCTAssertNil(dbFundingLink3)
36+
#expect(dbFundingLink3 == nil)
3837
}
3938

4039
}

0 commit comments

Comments
 (0)