Skip to content

Commit 6ab2e6c

Browse files
committed
Convert ErrorReportingTests
1 parent da937da commit 6ab2e6c

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

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

0 commit comments

Comments
 (0)