Skip to content

Commit 14e2805

Browse files
committed
Move common dependencies into invokeTest override
1 parent 1d2dc6e commit 14e2805

File tree

1 file changed

+87
-107
lines changed

1 file changed

+87
-107
lines changed

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 87 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -100,135 +100,115 @@ final class AnalyzeErrorTests: AppTestCase {
100100
Current.shell.run = Self.defaultShellRun
101101
}
102102

103-
func test_analyze_refreshCheckout_failed() async throws {
104-
try await withDependencies {
103+
override func invokeTest() {
104+
withDependencies {
105105
$0.date.now = .t0
106106
$0.environment.allowSocialPosts = { true }
107107
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
108108
socialPosts.withValue { $0.append(message) }
109109
}
110110
} operation: {
111-
Current.shell.run = { @Sendable cmd, path in
112-
switch cmd {
113-
case _ where cmd.description.contains("git clone https://github.com/foo/1"):
114-
throw SimulatedError()
115-
116-
case .gitFetchAndPruneTags where path.hasSuffix("foo-1"):
117-
throw SimulatedError()
118-
119-
default:
120-
return try Self.defaultShellRun(cmd, path)
121-
}
122-
}
123-
124-
// MUT
125-
try await Analyze.analyze(client: app.client,
126-
database: app.db,
127-
mode: .limit(10))
128-
129-
// validate
130-
try await defaultValidation()
131-
try logger.logs.withValue { logs in
132-
XCTAssertEqual(logs.count, 2)
133-
let error = try logs.last.unwrap()
134-
XCTAssertTrue(error.message.contains("refreshCheckout failed"), "was: \(error.message)")
111+
super.invokeTest()
112+
}
113+
}
114+
115+
func test_analyze_refreshCheckout_failed() async throws {
116+
Current.shell.run = { @Sendable cmd, path in
117+
switch cmd {
118+
case _ where cmd.description.contains("git clone https://github.com/foo/1"):
119+
throw SimulatedError()
120+
121+
case .gitFetchAndPruneTags where path.hasSuffix("foo-1"):
122+
throw SimulatedError()
123+
124+
default:
125+
return try Self.defaultShellRun(cmd, path)
135126
}
136127
}
128+
129+
// MUT
130+
try await Analyze.analyze(client: app.client,
131+
database: app.db,
132+
mode: .limit(10))
133+
134+
// validate
135+
try await defaultValidation()
136+
try logger.logs.withValue { logs in
137+
XCTAssertEqual(logs.count, 2)
138+
let error = try logs.last.unwrap()
139+
XCTAssertTrue(error.message.contains("refreshCheckout failed"), "was: \(error.message)")
140+
}
137141
}
138142

139143
func test_analyze_updateRepository_invalidPackageCachePath() async throws {
140-
try await withDependencies {
141-
$0.date.now = .t0
142-
$0.environment.allowSocialPosts = { true }
143-
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
144-
socialPosts.withValue { $0.append(message) }
145-
}
146-
} operation: {
147-
// setup
148-
let pkg = try await Package.find(badPackageID, on: app.db).unwrap()
149-
// This may look weird but its currently the only way to actually create an
150-
// invalid package cache path - we need to mess up the package url.
151-
pkg.url = "foo/1"
152-
XCTAssertNil(pkg.cacheDirectoryName)
153-
try await pkg.save(on: app.db)
154-
155-
// MUT
156-
try await Analyze.analyze(client: app.client,
157-
database: app.db,
158-
mode: .limit(10))
159-
160-
// validate
161-
try await defaultValidation()
162-
try logger.logs.withValue { logs in
163-
XCTAssertEqual(logs.count, 2)
164-
let error = try logs.last.unwrap()
165-
XCTAssertTrue(error.message.contains( "AppError.invalidPackageCachePath"), "was: \(error.message)")
166-
}
144+
// setup
145+
let pkg = try await Package.find(badPackageID, on: app.db).unwrap()
146+
// This may look weird but its currently the only way to actually create an
147+
// invalid package cache path - we need to mess up the package url.
148+
pkg.url = "foo/1"
149+
XCTAssertNil(pkg.cacheDirectoryName)
150+
try await pkg.save(on: app.db)
151+
152+
// MUT
153+
try await Analyze.analyze(client: app.client,
154+
database: app.db,
155+
mode: .limit(10))
156+
157+
// validate
158+
try await defaultValidation()
159+
try logger.logs.withValue { logs in
160+
XCTAssertEqual(logs.count, 2)
161+
let error = try logs.last.unwrap()
162+
XCTAssertTrue(error.message.contains( "AppError.invalidPackageCachePath"), "was: \(error.message)")
167163
}
168164
}
169165

170166
func test_analyze_getPackageInfo_gitCheckout_error() async throws {
171-
try await withDependencies {
172-
$0.date.now = .t0
173-
$0.environment.allowSocialPosts = { true }
174-
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
175-
socialPosts.withValue { $0.append(message) }
176-
}
177-
} operation: {
178-
// setup
179-
Current.shell.run = { @Sendable cmd, path in
180-
switch cmd {
181-
case .gitCheckout(branch: "main", quiet: true) where path.hasSuffix("foo-1"):
182-
throw SimulatedError()
183-
184-
default:
185-
return try Self.defaultShellRun(cmd, path)
186-
}
167+
// setup
168+
Current.shell.run = { @Sendable cmd, path in
169+
switch cmd {
170+
case .gitCheckout(branch: "main", quiet: true) where path.hasSuffix("foo-1"):
171+
throw SimulatedError()
172+
173+
default:
174+
return try Self.defaultShellRun(cmd, path)
187175
}
176+
}
188177

189-
// MUT
190-
try await Analyze.analyze(client: app.client,
191-
database: app.db,
192-
mode: .limit(10))
193-
194-
// validate
195-
try await defaultValidation()
196-
try logger.logs.withValue { logs in
197-
XCTAssertEqual(logs.count, 2)
198-
let error = try logs.last.unwrap()
199-
XCTAssertTrue(error.message.contains("AppError.noValidVersions"), "was: \(error.message)")
200-
}
178+
// MUT
179+
try await Analyze.analyze(client: app.client,
180+
database: app.db,
181+
mode: .limit(10))
182+
183+
// validate
184+
try await defaultValidation()
185+
try logger.logs.withValue { logs in
186+
XCTAssertEqual(logs.count, 2)
187+
let error = try logs.last.unwrap()
188+
XCTAssertTrue(error.message.contains("AppError.noValidVersions"), "was: \(error.message)")
201189
}
202190
}
203191

204192
func test_analyze_dumpPackage_missing_manifest() async throws {
205-
try await withDependencies {
206-
$0.date.now = .t0
207-
$0.environment.allowSocialPosts = { true }
208-
$0.environment.mastodonPost = { @Sendable [socialPosts = self.socialPosts] _, message in
209-
socialPosts.withValue { $0.append(message) }
210-
}
211-
} operation: {
212-
// setup
213-
Current.fileManager.fileExists = { @Sendable path in
214-
if path.hasSuffix("github.com-foo-1/Package.swift") {
215-
return false
216-
}
217-
return true
218-
}
219-
220-
// MUT
221-
try await Analyze.analyze(client: app.client,
222-
database: app.db,
223-
mode: .limit(10))
224-
225-
// validate
226-
try await defaultValidation()
227-
try logger.logs.withValue { logs in
228-
XCTAssertEqual(logs.count, 2)
229-
let error = try logs.last.unwrap()
230-
XCTAssertTrue(error.message.contains("AppError.noValidVersions"), "was: \(error.message)")
193+
// setup
194+
Current.fileManager.fileExists = { @Sendable path in
195+
if path.hasSuffix("github.com-foo-1/Package.swift") {
196+
return false
231197
}
198+
return true
199+
}
200+
201+
// MUT
202+
try await Analyze.analyze(client: app.client,
203+
database: app.db,
204+
mode: .limit(10))
205+
206+
// validate
207+
try await defaultValidation()
208+
try logger.logs.withValue { logs in
209+
XCTAssertEqual(logs.count, 2)
210+
let error = try logs.last.unwrap()
211+
XCTAssertTrue(error.message.contains("AppError.noValidVersions"), "was: \(error.message)")
232212
}
233213
}
234214

0 commit comments

Comments
 (0)