Skip to content

Commit c160a5d

Browse files
committed
Update tests
1 parent 5d2d879 commit c160a5d

File tree

8 files changed

+112
-87
lines changed

8 files changed

+112
-87
lines changed

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ final class AnalyzeErrorTests: AppTestCase {
115115
func test_analyze_refreshCheckout_failed() async throws {
116116
try await withDependencies {
117117
$0.environment.loadSPIManifest = { _ in nil }
118+
$0.fileManager.fileExists = { @Sendable _ in true }
118119
} operation: {
119120
Current.shell.run = { @Sendable cmd, path in
120121
switch cmd {
@@ -130,9 +131,7 @@ final class AnalyzeErrorTests: AppTestCase {
130131
}
131132

132133
// MUT
133-
try await Analyze.analyze(client: app.client,
134-
database: app.db,
135-
mode: .limit(10))
134+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
136135

137136
// validate
138137
try await defaultValidation()
@@ -147,6 +146,7 @@ final class AnalyzeErrorTests: AppTestCase {
147146
func test_analyze_updateRepository_invalidPackageCachePath() async throws {
148147
try await withDependencies {
149148
$0.environment.loadSPIManifest = { _ in nil }
149+
$0.fileManager.fileExists = { @Sendable _ in true }
150150
} operation: {
151151
// setup
152152
let pkg = try await Package.find(badPackageID, on: app.db).unwrap()
@@ -157,9 +157,7 @@ final class AnalyzeErrorTests: AppTestCase {
157157
try await pkg.save(on: app.db)
158158

159159
// MUT
160-
try await Analyze.analyze(client: app.client,
161-
database: app.db,
162-
mode: .limit(10))
160+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
163161

164162
// validate
165163
try await defaultValidation()
@@ -174,6 +172,7 @@ final class AnalyzeErrorTests: AppTestCase {
174172
func test_analyze_getPackageInfo_gitCheckout_error() async throws {
175173
try await withDependencies {
176174
$0.environment.loadSPIManifest = { _ in nil }
175+
$0.fileManager.fileExists = { @Sendable _ in true }
177176
} operation: {
178177
// setup
179178
Current.shell.run = { @Sendable cmd, path in
@@ -187,9 +186,7 @@ final class AnalyzeErrorTests: AppTestCase {
187186
}
188187

189188
// MUT
190-
try await Analyze.analyze(client: app.client,
191-
database: app.db,
192-
mode: .limit(10))
189+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
193190

194191
// validate
195192
try await defaultValidation()

Tests/AppTests/AnalyzerTests.swift

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class AnalyzerTests: AppTestCase {
314314
// Ensure a package that fails analysis goes back to ingesting and isn't stuck in an analysis loop
315315
try await withDependencies {
316316
$0.date.now = .now
317+
$0.fileManager.fileExists = { @Sendable _ in true }
317318
} operation: {
318319
// setup
319320
do {
@@ -333,9 +334,7 @@ class AnalyzerTests: AppTestCase {
333334
try await XCTAssertEqualAsync(try await Package.fetchCandidates(app.db, for: .analysis, limit: 10).count, 1)
334335

335336
// MUT
336-
try await Analyze.analyze(client: app.client,
337-
database: app.db,
338-
mode: .limit(10))
337+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
339338

340339
// Ensure candidate selection is now zero for analysis
341340
// (and also for ingestion, as we're immediately after analysis)
@@ -360,6 +359,7 @@ class AnalyzerTests: AppTestCase {
360359
$0.date.now = .now
361360
$0.environment.allowSocialPosts = { true }
362361
$0.environment.loadSPIManifest = { _ in nil }
362+
$0.fileManager.fileExists = { @Sendable _ in true }
363363
} operation: {
364364
// setup
365365
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
@@ -395,9 +395,7 @@ class AnalyzerTests: AppTestCase {
395395
}
396396

397397
// MUT
398-
try await Analyze.analyze(client: app.client,
399-
database: app.db,
400-
mode: .limit(10))
398+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
401399

402400
// assert packages have been updated
403401
let packages = try await Package.query(on: app.db).sort(\.$createdAt).all()
@@ -521,39 +519,43 @@ class AnalyzerTests: AppTestCase {
521519
}
522520

523521
func test_updateRepository() async throws {
524-
// setup
525-
Current.git.commitCount = { @Sendable _ in 12 }
526-
Current.git.firstCommitDate = { @Sendable _ in .t0 }
527-
Current.git.lastCommitDate = { @Sendable _ in .t1 }
528-
Current.git.shortlog = { @Sendable _ in
522+
try await withDependencies {
523+
$0.fileManager.fileExists = { @Sendable _ in true }
524+
} operation: {
525+
// setup
526+
Current.git.commitCount = { @Sendable _ in 12 }
527+
Current.git.firstCommitDate = { @Sendable _ in .t0 }
528+
Current.git.lastCommitDate = { @Sendable _ in .t1 }
529+
Current.git.shortlog = { @Sendable _ in
529530
"""
530531
10\tPerson 1
531532
2\tPerson 2
532533
"""
533-
}
534-
Current.shell.run = { @Sendable cmd, _ in throw TestError.unknownCommand }
535-
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
536-
try await pkg.save(on: app.db)
537-
try await Repository(id: .id1, package: pkg, defaultBranch: "main").save(on: app.db)
538-
let jpr = try await Package.fetchCandidate(app.db, id: pkg.id!)
534+
}
535+
Current.shell.run = { @Sendable cmd, _ in throw TestError.unknownCommand }
536+
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
537+
try await pkg.save(on: app.db)
538+
try await Repository(id: .id1, package: pkg, defaultBranch: "main").save(on: app.db)
539+
let jpr = try await Package.fetchCandidate(app.db, id: pkg.id!)
539540

540-
// MUT
541-
try await Analyze.updateRepository(on: app.db, package: jpr)
541+
// MUT
542+
try await Analyze.updateRepository(on: app.db, package: jpr)
542543

543-
// validate
544-
do { // ensure JPR relation is updated
545-
XCTAssertEqual(jpr.repository?.commitCount, 12)
546-
XCTAssertEqual(jpr.repository?.firstCommitDate, .t0)
547-
XCTAssertEqual(jpr.repository?.lastCommitDate, .t1)
548-
XCTAssertEqual(jpr.repository?.authors, PackageAuthors(authors: [Author(name: "Person 1")],
549-
numberOfContributors: 1))
550-
}
551-
do { // ensure changes are persisted
552-
let repo = try await Repository.find(.id1, on: app.db)
553-
XCTAssertEqual(repo?.commitCount, 12)
554-
XCTAssertEqual(repo?.firstCommitDate, .t0)
555-
XCTAssertEqual(repo?.lastCommitDate, .t1)
556-
XCTAssertEqual(repo?.authors, PackageAuthors(authors: [Author(name: "Person 1")], numberOfContributors: 1))
544+
// validate
545+
do { // ensure JPR relation is updated
546+
XCTAssertEqual(jpr.repository?.commitCount, 12)
547+
XCTAssertEqual(jpr.repository?.firstCommitDate, .t0)
548+
XCTAssertEqual(jpr.repository?.lastCommitDate, .t1)
549+
XCTAssertEqual(jpr.repository?.authors, PackageAuthors(authors: [Author(name: "Person 1")],
550+
numberOfContributors: 1))
551+
}
552+
do { // ensure changes are persisted
553+
let repo = try await Repository.find(.id1, on: app.db)
554+
XCTAssertEqual(repo?.commitCount, 12)
555+
XCTAssertEqual(repo?.firstCommitDate, .t0)
556+
XCTAssertEqual(repo?.lastCommitDate, .t1)
557+
XCTAssertEqual(repo?.authors, PackageAuthors(authors: [Author(name: "Person 1")], numberOfContributors: 1))
558+
}
557559
}
558560
}
559561

@@ -751,6 +753,7 @@ class AnalyzerTests: AppTestCase {
751753
// Tests getPackageInfo(package:version:)
752754
try await withDependencies {
753755
$0.environment.loadSPIManifest = { _ in nil }
756+
$0.fileManager.fileExists = { @Sendable _ in true }
754757
} operation: {
755758
// setup
756759
let commands = QueueIsolated<[String]>([])
@@ -890,6 +893,7 @@ class AnalyzerTests: AppTestCase {
890893
$0.date.now = .now
891894
$0.environment.allowSocialPosts = { true }
892895
$0.environment.loadSPIManifest = { _ in nil }
896+
$0.fileManager.fileExists = { @Sendable _ in true }
893897
} operation: {
894898
// setup
895899
Current.git.commitCount = { @Sendable _ in 12 }
@@ -1021,16 +1025,20 @@ class AnalyzerTests: AppTestCase {
10211025
// Test parsing a Package.swift that requires a 5.4 toolchain
10221026
// NB: If this test fails on macOS make sure xcode-select -p
10231027
// points to the correct version of Xcode!
1024-
// setup
1025-
Current.fileManager = .live
1026-
Current.shell = .live
1027-
try await withTempDir { tempDir in
1028-
let fixture = fixturesDirectory()
1029-
.appendingPathComponent("5.4-Package-swift").path
1030-
let fname = tempDir.appending("/Package.swift")
1031-
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1032-
let m = try await Analyze.dumpPackage(at: tempDir)
1033-
XCTAssertEqual(m.name, "VisualEffects")
1028+
try await withDependencies {
1029+
$0.fileManager.fileExists = FileManagerClient.liveValue.fileExists(atPath:)
1030+
} operation: {
1031+
// setup
1032+
Current.fileManager = .live
1033+
Current.shell = .live
1034+
try await withTempDir { tempDir in
1035+
let fixture = fixturesDirectory()
1036+
.appendingPathComponent("5.4-Package-swift").path
1037+
let fname = tempDir.appending("/Package.swift")
1038+
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1039+
let m = try await Analyze.dumpPackage(at: tempDir)
1040+
XCTAssertEqual(m.name, "VisualEffects")
1041+
}
10341042
}
10351043
}
10361044

@@ -1039,33 +1047,41 @@ class AnalyzerTests: AppTestCase {
10391047
// NB: If this test fails on macOS make sure xcode-select -p
10401048
// points to the correct version of Xcode!
10411049
// See also https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/1441
1042-
// setup
1043-
Current.fileManager = .live
1044-
Current.shell = .live
1045-
try await withTempDir { tempDir in
1046-
let fixture = fixturesDirectory()
1047-
.appendingPathComponent("5.5-Package-swift").path
1048-
let fname = tempDir.appending("/Package.swift")
1049-
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1050-
let m = try await Analyze.dumpPackage(at: tempDir)
1051-
XCTAssertEqual(m.name, "Firestarter")
1050+
try await withDependencies {
1051+
$0.fileManager.fileExists = FileManagerClient.liveValue.fileExists(atPath:)
1052+
} operation: {
1053+
// setup
1054+
Current.fileManager = .live
1055+
Current.shell = .live
1056+
try await withTempDir { tempDir in
1057+
let fixture = fixturesDirectory()
1058+
.appendingPathComponent("5.5-Package-swift").path
1059+
let fname = tempDir.appending("/Package.swift")
1060+
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1061+
let m = try await Analyze.dumpPackage(at: tempDir)
1062+
XCTAssertEqual(m.name, "Firestarter")
1063+
}
10521064
}
10531065
}
10541066

10551067
func test_dumpPackage_5_9_macro_target() async throws {
10561068
// Test parsing a 5.9 Package.swift with a macro target
10571069
// NB: If this test fails on macOS make sure xcode-select -p
10581070
// points to the correct version of Xcode!
1059-
// setup
1060-
Current.fileManager = .live
1061-
Current.shell = .live
1062-
try await withTempDir { tempDir in
1063-
let fixture = fixturesDirectory()
1064-
.appendingPathComponent("5.9-Package-swift").path
1065-
let fname = tempDir.appending("/Package.swift")
1066-
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1067-
let m = try await Analyze.dumpPackage(at: tempDir)
1068-
XCTAssertEqual(m.name, "StaticMemberIterable")
1071+
try await withDependencies {
1072+
$0.fileManager.fileExists = FileManagerClient.liveValue.fileExists(atPath:)
1073+
} operation: {
1074+
// setup
1075+
Current.fileManager = .live
1076+
Current.shell = .live
1077+
try await withTempDir { tempDir in
1078+
let fixture = fixturesDirectory()
1079+
.appendingPathComponent("5.9-Package-swift").path
1080+
let fname = tempDir.appending("/Package.swift")
1081+
try await ShellOut.shellOut(to: .copyFile(from: fixture, to: fname))
1082+
let m = try await Analyze.dumpPackage(at: tempDir)
1083+
XCTAssertEqual(m.name, "StaticMemberIterable")
1084+
}
10691085
}
10701086
}
10711087

@@ -1506,6 +1522,7 @@ class AnalyzerTests: AppTestCase {
15061522
try await withDependencies {
15071523
$0.date.now = .now
15081524
$0.environment.loadSPIManifest = { _ in nil }
1525+
$0.fileManager.fileExists = { @Sendable _ in true }
15091526
} operation: {
15101527
// setup
15111528
let pkg = try await savePackage(on: app.db, id: .id0, "https://github.com/foo/1".url, processingStage: .ingestion)

Tests/AppTests/ErrorReportingTests.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,19 @@ class ErrorReportingTests: AppTestCase {
7979
}
8080

8181
func test_invalidPackageCachePath() async throws {
82-
// setup
83-
try await savePackages(on: app.db, ["1", "2"], processingStage: .ingestion)
82+
try await withDependencies {
83+
$0.fileManager.fileExists = { @Sendable _ in true }
84+
} operation: {
85+
// setup
86+
try await savePackages(on: app.db, ["1", "2"], processingStage: .ingestion)
8487

85-
// MUT
86-
try await Analyze.analyze(client: app.client,
87-
database: app.db,
88-
mode: .limit(10))
88+
// MUT
89+
try await Analyze.analyze(client: app.client, database: app.db, mode: .limit(10))
8990

90-
// validation
91-
let packages = try await Package.query(on: app.db).sort(\.$url).all()
92-
XCTAssertEqual(packages.map(\.status), [.invalidCachePath, .invalidCachePath])
91+
// validation
92+
let packages = try await Package.query(on: app.db).sort(\.$url).all()
93+
XCTAssertEqual(packages.map(\.status), [.invalidCachePath, .invalidCachePath])
94+
}
9395
}
9496

9597
}

Tests/AppTests/MastodonTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ final class MastodonTests: AppTestCase {
2626
try await withDependencies {
2727
$0.environment.allowSocialPosts = { true }
2828
$0.environment.loadSPIManifest = { _ in nil }
29+
$0.fileManager.fileExists = { @Sendable _ in true }
2930
$0.github.fetchLicense = { @Sendable _, _ in nil }
3031
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
3132
$0.github.fetchReadme = { @Sendable _, _ in nil }

Tests/AppTests/MetricsTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ class MetricsTests: AppTestCase {
129129
}
130130

131131
func test_analyzeDurationSeconds() async throws {
132-
// setup
133-
let pkg = try await savePackage(on: app.db, "1")
132+
try await withDependencies {
133+
$0.fileManager.fileExists = { @Sendable _ in true }
134+
} operation: {
135+
// setup
136+
let pkg = try await savePackage(on: app.db, "1")
134137

135-
// MUT
136-
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!))
138+
// MUT
139+
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!))
137140

138-
// validation
139-
XCTAssert((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0)
141+
// validation
142+
XCTAssert((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0)
143+
}
140144
}
141145

142146
func test_triggerBuildsDurationSeconds() async throws {

Tests/AppTests/PackageTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ final class PackageTests: AppTestCase {
311311
let url = "1".asGithubUrl
312312
try await withDependencies {
313313
$0.date.now = .now
314+
$0.fileManager.fileExists = { @Sendable _ in true }
314315
$0.github.fetchLicense = { @Sendable _, _ in nil }
315316
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
316317
$0.github.fetchReadme = { @Sendable _, _ in nil }

Tests/AppTests/PipelineTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class PipelineTests: AppTestCase {
162162
try await withDependencies {
163163
$0.date.now = .now
164164
$0.environment.loadSPIManifest = { _ in nil }
165+
$0.fileManager.fileExists = { @Sendable _ in true }
165166
$0.github.fetchLicense = { @Sendable _, _ in nil }
166167
$0.github.fetchMetadata = { @Sendable owner, repository in .mock(owner: owner, repository: repository) }
167168
$0.github.fetchReadme = { @Sendable _, _ in nil }

Tests/AppTests/ReAnalyzeVersionsTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ReAnalyzeVersionsTests: AppTestCase {
3030
$0.date.now = .t0
3131
$0.environment.allowSocialPosts = { true }
3232
$0.environment.loadSPIManifest = { _ in nil }
33+
$0.fileManager.fileExists = { @Sendable _ in true }
3334
$0.httpClient.mastodonPost = { @Sendable _ in }
3435
} operation: {
3536
// setup
@@ -182,6 +183,7 @@ class ReAnalyzeVersionsTests: AppTestCase {
182183
try await withDependencies {
183184
$0.date.now = .t2
184185
$0.environment.loadSPIManifest = { _ in nil }
186+
$0.fileManager.fileExists = { @Sendable _ in true }
185187
} operation: {
186188
let pkg = try await savePackage(on: app.db,
187189
"https://github.com/foo/1".url,

0 commit comments

Comments
 (0)