Skip to content

Commit 0b76fa3

Browse files
committed
Current.buildTimeout → @dependency(\.environment.buildTimeout)
1 parent 082e486 commit 0b76fa3

File tree

7 files changed

+467
-425
lines changed

7 files changed

+467
-425
lines changed

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct AppEnvironment: Sendable {
2929
var awsDocsBucket: @Sendable () -> String?
3030
var awsReadmeBucket: @Sendable () -> String?
3131
var awsSecretAccessKey: @Sendable () -> String?
32-
var buildTimeout: @Sendable () -> Int
3332
var builderToken: @Sendable () -> String?
3433
var buildTriggerAllowList: @Sendable () -> [Package.Id]
3534
var buildTriggerDownscaling: @Sendable () -> Double
@@ -112,7 +111,6 @@ extension AppEnvironment {
112111
awsDocsBucket: { Environment.get("AWS_DOCS_BUCKET") },
113112
awsReadmeBucket: { Environment.get("AWS_README_BUCKET") },
114113
awsSecretAccessKey: { Environment.get("AWS_SECRET_ACCESS_KEY") },
115-
buildTimeout: { Environment.get("BUILD_TIMEOUT").flatMap(Int.init) ?? 10 },
116114
builderToken: { Environment.get("BUILDER_TOKEN") },
117115
buildTriggerAllowList: {
118116
Environment.get("BUILD_TRIGGER_ALLOW_LIST")

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct EnvironmentClient {
2323
// regarding the use of XCTFail here.
2424
var allowBuildTriggers: @Sendable () -> Bool = { XCTFail(#function); return true }
2525
var allowSocialPosts: @Sendable () -> Bool = { XCTFail(#function); return true }
26+
var buildTimeout: @Sendable () -> Int = { XCTFail(#function); return 10 }
2627
// We're not defaulting current to XCTFail, because its use is too pervasive and would require the vast
2728
// majority of tests to be wrapped with `withDependencies`.
2829
// We can do so at a later time once more tests are transitioned over for other dependencies. This is
@@ -45,6 +46,7 @@ extension EnvironmentClient: DependencyKey {
4546
.flatMap(\.asBool)
4647
?? Constants.defaultAllowSocialPosts
4748
},
49+
buildTimeout: { Environment.get("BUILD_TIMEOUT").flatMap(Int.init) ?? 10 },
4850
current: { (try? Environment.detect()) ?? .development },
4951
mastodonCredentials: {
5052
Environment.get("MASTODON_ACCESS_TOKEN")

Sources/App/Core/Gitlab.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Dependencies
1516
import Vapor
1617

1718

@@ -76,13 +77,15 @@ extension Gitlab.Builder {
7677
reference: Reference,
7778
swiftVersion: SwiftVersion,
7879
versionID: Version.Id) async throws -> Build.TriggerResponse {
80+
@Dependency(\.environment) var environment
81+
7982
guard let pipelineToken = Current.gitlabPipelineToken(),
8083
let builderToken = Current.builderToken()
8184
else { throw Gitlab.Error.missingToken }
8285
guard let awsDocsBucket = Current.awsDocsBucket() else {
8386
throw Gitlab.Error.missingConfiguration("AWS_DOCS_BUCKET")
8487
}
85-
let timeout = Current.buildTimeout() + (isDocBuild ? 5 : 0)
88+
let timeout = environment.buildTimeout() + (isDocBuild ? 5 : 0)
8689

8790
let uri: URI = .init(string: "\(projectURL)/trigger/pipeline")
8891
let response = try await client

Tests/AppTests/BuildTests.swift

Lines changed: 116 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
@testable import App
1616

17+
import Dependencies
1718
import Fluent
1819
import PostgresNIO
1920
import SQLKit
@@ -128,118 +129,126 @@ class BuildTests: AppTestCase {
128129
}
129130

130131
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+
}
187182

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+
}
191196
}
192197

193198
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)
229251
}
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)
243252
}
244253

245254
func test_query() async throws {

0 commit comments

Comments
 (0)