|
14 | 14 |
|
15 | 15 | @testable import App |
16 | 16 |
|
| 17 | +import Dependencies |
17 | 18 | import Fluent |
18 | 19 | import PostgresNIO |
19 | 20 | import SQLKit |
@@ -128,118 +129,126 @@ class BuildTests: AppTestCase { |
128 | 129 | } |
129 | 130 |
|
130 | 131 | func test_trigger() async throws { |
131 | | - Current.builderToken = { "builder token" } |
132 | | - Current.gitlabPipelineToken = { "pipeline token" } |
133 | | - Current.siteURL = { "http://example.com" } |
134 | | - // setup |
135 | | - let p = try await savePackage(on: app.db, "1") |
136 | | - let v = try Version(package: p, reference: .branch("main")) |
137 | | - try await v.save(on: app.db) |
138 | | - let buildId = UUID() |
139 | | - let versionID = try XCTUnwrap(v.id) |
140 | | - |
141 | | - // Use live dependency but replace actual client with a mock so we can |
142 | | - // assert on the details being sent without actually making a request |
143 | | - Current.triggerBuild = { client, buildId, cloneURL, isDocBuild, platform, ref, swiftVersion, versionID in |
144 | | - try await Gitlab.Builder.triggerBuild(client: client, |
145 | | - buildId: buildId, |
146 | | - cloneURL: cloneURL, |
147 | | - isDocBuild: isDocBuild, |
148 | | - platform: platform, |
149 | | - reference: ref, |
150 | | - swiftVersion: swiftVersion, |
151 | | - versionID: versionID) |
152 | | - } |
153 | | - var called = false |
154 | | - let client = MockClient { req, res in |
155 | | - called = true |
156 | | - res.status = .created |
157 | | - try? res.content.encode( |
158 | | - Gitlab.Builder.Response.init(webUrl: "http://web_url") |
159 | | - ) |
160 | | - // validate request data |
161 | | - XCTAssertEqual(try? req.query.decode(Gitlab.Builder.PostDTO.self), |
162 | | - Gitlab.Builder.PostDTO( |
163 | | - token: "pipeline token", |
164 | | - ref: "main", |
165 | | - variables: [ |
166 | | - "API_BASEURL": "http://example.com/api", |
167 | | - "AWS_DOCS_BUCKET": "awsDocsBucket", |
168 | | - "BUILD_ID": buildId.uuidString, |
169 | | - "BUILD_PLATFORM": "macos-xcodebuild", |
170 | | - "BUILDER_TOKEN": "builder token", |
171 | | - "CLONE_URL": "1", |
172 | | - "REFERENCE": "main", |
173 | | - "SWIFT_VERSION": "5.2", |
174 | | - "TIMEOUT": "10m", |
175 | | - "VERSION_ID": versionID.uuidString, |
176 | | - ])) |
177 | | - } |
178 | | - |
179 | | - // MUT |
180 | | - let res = try await Build.trigger(database: app.db, |
181 | | - client: client, |
182 | | - buildId: buildId, |
183 | | - isDocBuild: false, |
184 | | - platform: .macosXcodebuild, |
185 | | - swiftVersion: .init(5, 2, 4), |
186 | | - versionId: versionID) |
| 132 | + try await withDependencies { |
| 133 | + $0.environment.buildTimeout = { 10 } |
| 134 | + } operation: { |
| 135 | + Current.builderToken = { "builder token" } |
| 136 | + Current.gitlabPipelineToken = { "pipeline token" } |
| 137 | + Current.siteURL = { "http://example.com" } |
| 138 | + // setup |
| 139 | + let p = try await savePackage(on: app.db, "1") |
| 140 | + let v = try Version(package: p, reference: .branch("main")) |
| 141 | + try await v.save(on: app.db) |
| 142 | + let buildId = UUID() |
| 143 | + let versionID = try XCTUnwrap(v.id) |
| 144 | + |
| 145 | + // Use live dependency but replace actual client with a mock so we can |
| 146 | + // assert on the details being sent without actually making a request |
| 147 | + Current.triggerBuild = { client, buildId, cloneURL, isDocBuild, platform, ref, swiftVersion, versionID in |
| 148 | + try await Gitlab.Builder.triggerBuild(client: client, |
| 149 | + buildId: buildId, |
| 150 | + cloneURL: cloneURL, |
| 151 | + isDocBuild: isDocBuild, |
| 152 | + platform: platform, |
| 153 | + reference: ref, |
| 154 | + swiftVersion: swiftVersion, |
| 155 | + versionID: versionID) |
| 156 | + } |
| 157 | + var called = false |
| 158 | + let client = MockClient { req, res in |
| 159 | + called = true |
| 160 | + res.status = .created |
| 161 | + try? res.content.encode( |
| 162 | + Gitlab.Builder.Response.init(webUrl: "http://web_url") |
| 163 | + ) |
| 164 | + // validate request data |
| 165 | + XCTAssertEqual(try? req.query.decode(Gitlab.Builder.PostDTO.self), |
| 166 | + Gitlab.Builder.PostDTO( |
| 167 | + token: "pipeline token", |
| 168 | + ref: "main", |
| 169 | + variables: [ |
| 170 | + "API_BASEURL": "http://example.com/api", |
| 171 | + "AWS_DOCS_BUCKET": "awsDocsBucket", |
| 172 | + "BUILD_ID": buildId.uuidString, |
| 173 | + "BUILD_PLATFORM": "macos-xcodebuild", |
| 174 | + "BUILDER_TOKEN": "builder token", |
| 175 | + "CLONE_URL": "1", |
| 176 | + "REFERENCE": "main", |
| 177 | + "SWIFT_VERSION": "5.2", |
| 178 | + "TIMEOUT": "10m", |
| 179 | + "VERSION_ID": versionID.uuidString, |
| 180 | + ])) |
| 181 | + } |
187 | 182 |
|
188 | | - // validate |
189 | | - XCTAssertTrue(called) |
190 | | - XCTAssertEqual(res.status, .created) |
| 183 | + // MUT |
| 184 | + let res = try await Build.trigger(database: app.db, |
| 185 | + client: client, |
| 186 | + buildId: buildId, |
| 187 | + isDocBuild: false, |
| 188 | + platform: .macosXcodebuild, |
| 189 | + swiftVersion: .init(5, 2, 4), |
| 190 | + versionId: versionID) |
| 191 | + |
| 192 | + // validate |
| 193 | + XCTAssertTrue(called) |
| 194 | + XCTAssertEqual(res.status, .created) |
| 195 | + } |
191 | 196 | } |
192 | 197 |
|
193 | 198 | func test_trigger_isDocBuild() async throws { |
194 | | - // Same test as test_trigger above, except we trigger with isDocBuild: true |
195 | | - // and expect a 15m TIMEOUT instead of 10m |
196 | | - Current.builderToken = { "builder token" } |
197 | | - Current.gitlabPipelineToken = { "pipeline token" } |
198 | | - Current.siteURL = { "http://example.com" } |
199 | | - // setup |
200 | | - let p = try await savePackage(on: app.db, "1") |
201 | | - let v = try Version(package: p, reference: .branch("main")) |
202 | | - try await v.save(on: app.db) |
203 | | - let buildId = UUID() |
204 | | - let versionID = try XCTUnwrap(v.id) |
205 | | - |
206 | | - // Use live dependency but replace actual client with a mock so we can |
207 | | - // assert on the details being sent without actually making a request |
208 | | - Current.triggerBuild = { client, buildId, cloneURL, isDocBuild, platform, ref, swiftVersion, versionID in |
209 | | - try await Gitlab.Builder.triggerBuild(client: client, |
210 | | - buildId: buildId, |
211 | | - cloneURL: cloneURL, |
212 | | - isDocBuild: isDocBuild, |
213 | | - platform: platform, |
214 | | - reference: ref, |
215 | | - swiftVersion: swiftVersion, |
216 | | - versionID: versionID) |
217 | | - } |
218 | | - var called = false |
219 | | - let client = MockClient { req, res in |
220 | | - called = true |
221 | | - res.status = .created |
222 | | - try? res.content.encode( |
223 | | - Gitlab.Builder.Response.init(webUrl: "http://web_url") |
224 | | - ) |
225 | | - // only test the TIMEOUT value, the rest is already tested in `test_trigger` above |
226 | | - let response = try? req.query.decode(Gitlab.Builder.PostDTO.self) |
227 | | - XCTAssertNotNil(response) |
228 | | - XCTAssertEqual(response?.variables["TIMEOUT"], "15m") |
| 199 | + try await withDependencies { |
| 200 | + $0.environment.buildTimeout = { 10 } |
| 201 | + } operation: { |
| 202 | + // Same test as test_trigger above, except we trigger with isDocBuild: true |
| 203 | + // and expect a 15m TIMEOUT instead of 10m |
| 204 | + Current.builderToken = { "builder token" } |
| 205 | + Current.gitlabPipelineToken = { "pipeline token" } |
| 206 | + Current.siteURL = { "http://example.com" } |
| 207 | + // setup |
| 208 | + let p = try await savePackage(on: app.db, "1") |
| 209 | + let v = try Version(package: p, reference: .branch("main")) |
| 210 | + try await v.save(on: app.db) |
| 211 | + let buildId = UUID() |
| 212 | + let versionID = try XCTUnwrap(v.id) |
| 213 | + |
| 214 | + // Use live dependency but replace actual client with a mock so we can |
| 215 | + // assert on the details being sent without actually making a request |
| 216 | + Current.triggerBuild = { client, buildId, cloneURL, isDocBuild, platform, ref, swiftVersion, versionID in |
| 217 | + try await Gitlab.Builder.triggerBuild(client: client, |
| 218 | + buildId: buildId, |
| 219 | + cloneURL: cloneURL, |
| 220 | + isDocBuild: isDocBuild, |
| 221 | + platform: platform, |
| 222 | + reference: ref, |
| 223 | + swiftVersion: swiftVersion, |
| 224 | + versionID: versionID) |
| 225 | + } |
| 226 | + var called = false |
| 227 | + let client = MockClient { req, res in |
| 228 | + called = true |
| 229 | + res.status = .created |
| 230 | + try? res.content.encode( |
| 231 | + Gitlab.Builder.Response.init(webUrl: "http://web_url") |
| 232 | + ) |
| 233 | + // only test the TIMEOUT value, the rest is already tested in `test_trigger` above |
| 234 | + let response = try? req.query.decode(Gitlab.Builder.PostDTO.self) |
| 235 | + XCTAssertNotNil(response) |
| 236 | + XCTAssertEqual(response?.variables["TIMEOUT"], "15m") |
| 237 | + } |
| 238 | + |
| 239 | + // MUT |
| 240 | + let res = try await Build.trigger(database: app.db, |
| 241 | + client: client, |
| 242 | + buildId: buildId, |
| 243 | + isDocBuild: true, |
| 244 | + platform: .macosXcodebuild, |
| 245 | + swiftVersion: .init(5, 2, 4), |
| 246 | + versionId: versionID) |
| 247 | + |
| 248 | + // validate |
| 249 | + XCTAssertTrue(called) |
| 250 | + XCTAssertEqual(res.status, .created) |
229 | 251 | } |
230 | | - |
231 | | - // MUT |
232 | | - let res = try await Build.trigger(database: app.db, |
233 | | - client: client, |
234 | | - buildId: buildId, |
235 | | - isDocBuild: true, |
236 | | - platform: .macosXcodebuild, |
237 | | - swiftVersion: .init(5, 2, 4), |
238 | | - versionId: versionID) |
239 | | - |
240 | | - // validate |
241 | | - XCTAssertTrue(called) |
242 | | - XCTAssertEqual(res.status, .created) |
243 | 252 | } |
244 | 253 |
|
245 | 254 | func test_query() async throws { |
|
0 commit comments