Skip to content

Commit e4162ec

Browse files
committed
Move currentReferenceCache to EnvironmentClient
1 parent 741c054 commit e4162ec

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import FoundationNetworking
2323

2424

2525
struct AppEnvironment: Sendable {
26-
var currentReferenceCache: @Sendable () -> CurrentReferenceCache?
2726
var dbId: @Sendable () -> String?
2827
var fetchDocumentation: @Sendable (_ client: Client, _ url: URI) async throws -> ClientResponse
2928
var fetchHTTPStatusCode: @Sendable (_ url: String) async throws -> HTTPStatus
@@ -87,7 +86,6 @@ extension AppEnvironment {
8786
nonisolated(unsafe) static var logger: Logger!
8887

8988
static let live = AppEnvironment(
90-
currentReferenceCache: { .live },
9189
dbId: { Environment.get("DATABASE_ID") },
9290
fetchDocumentation: { client, url in try await client.get(url) },
9391
fetchHTTPStatusCode: { url in try await Networking.fetchHTTPStatusCode(url) },

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct EnvironmentClient {
3939
var collectionSigningCertificateChain: @Sendable () -> [URL] = { XCTFail("collectionSigningCertificateChain"); return [] }
4040
var collectionSigningPrivateKey: @Sendable () -> Data?
4141
var current: @Sendable () -> Environment = { XCTFail("current"); return .development }
42+
var currentReferenceCache: @Sendable () -> CurrentReferenceCache?
4243
var mastodonCredentials: @Sendable () -> Mastodon.Credentials?
4344
var mastodonPost: @Sendable (_ client: Client, _ post: String) async throws -> Void
4445
var random: @Sendable (_ range: ClosedRange<Double>) -> Double = { XCTFail("random"); return Double.random(in: $0) }
@@ -91,6 +92,7 @@ extension EnvironmentClient: DependencyKey {
9192
Environment.get("COLLECTION_SIGNING_PRIVATE_KEY").map { Data($0.utf8) }
9293
},
9394
current: { (try? Environment.detect()) ?? .development },
95+
currentReferenceCache: { .live },
9496
mastodonCredentials: {
9597
Environment.get("MASTODON_ACCESS_TOKEN")
9698
.map(Mastodon.Credentials.init(accessToken:))

Sources/App/routes+documentation.swift

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

15-
15+
import Dependencies
1616
import Vapor
1717

1818

@@ -124,7 +124,7 @@ private extension Parameters {
124124
}
125125
}
126126
}
127-
127+
128128

129129
struct DocRedirect {
130130
var owner: String
@@ -152,15 +152,15 @@ extension Request {
152152
target = try await DocumentationTarget.query(on: db, owner: owner, repository: repository,
153153
docVersion: .reference(ref))
154154
}
155-
155+
156156
case .none:
157157
target = try await DocumentationTarget.query(on: db, owner: owner, repository: repository)
158158
}
159159
guard let target else { throw Abort(.notFound) }
160160

161161
return .init(owner: owner, repository: repository, target: target, path: path)
162162
}
163-
163+
164164
func getDocRoute(fragment: DocRoute.Fragment) async throws -> DocRoute {
165165
guard let owner = parameters.get("owner"),
166166
let repository = parameters.get("repository"),
@@ -170,16 +170,18 @@ extension Request {
170170
if fragment.requiresArchive && archive == nil { throw Abort(.badRequest) }
171171
let pathElements = parameters.pathElements(for: fragment, archive: archive)
172172

173+
@Dependency(\.environment) var environment
174+
173175
let docVersion = try await { () -> DocVersion in
174176
if reference == String.current {
175-
if let ref = Current.currentReferenceCache()?[owner: owner, repository: repository] {
177+
if let ref = environment.currentReferenceCache()?[owner: owner, repository: repository] {
176178
return .current(referencing: ref)
177179
}
178180

179181
guard let params = try await DocumentationTarget.query(on: db, owner: owner, repository: repository)?.internal
180182
else { throw Abort(.notFound) }
181183

182-
Current.currentReferenceCache()?[owner: owner, repository: repository] = "\(params.docVersion)"
184+
environment.currentReferenceCache()?[owner: owner, repository: repository] = "\(params.docVersion)"
183185
return .current(referencing: "\(params.docVersion)")
184186
} else {
185187
return .reference(reference)

Tests/AppTests/Mocks/AppEnvironment+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import Vapor
2222
extension AppEnvironment {
2323
static func mock(eventLoop: EventLoop) -> Self {
2424
.init(
25-
currentReferenceCache: { nil },
2625
dbId: { "db-id" },
2726
fetchDocumentation: { _, _ in .init(status: .ok) },
2827
fetchHTTPStatusCode: { _ in .ok },

Tests/AppTests/PackageController+routesTests.swift

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ class PackageController_routesTests: SnapshotTestCase {
538538
// /owner/package/documentation/~ + various path elements
539539
try await withDependencies {
540540
$0.environment.awsDocsBucket = { "docs-bucket" }
541+
$0.environment.currentReferenceCache = { nil }
541542
} operation: {
542543
// setup
543544
let pkg = try await savePackage(on: app.db, "1")
@@ -620,6 +621,7 @@ class PackageController_routesTests: SnapshotTestCase {
620621
// /owner/package/documentation/~ + various path elements
621622
try await withDependencies {
622623
$0.environment.awsDocsBucket = { "docs-bucket" }
624+
$0.environment.currentReferenceCache = { nil }
623625
} operation: {
624626
// setup
625627
let pkg = try await savePackage(on: app.db, "1")
@@ -905,6 +907,7 @@ class PackageController_routesTests: SnapshotTestCase {
905907
func test_documentation_current_css() async throws {
906908
try await withDependencies {
907909
$0.environment.awsDocsBucket = { "docs-bucket" }
910+
$0.environment.currentReferenceCache = { nil }
908911
} operation: {
909912
// setup
910913
Current.fetchDocumentation = { _, uri in
@@ -967,6 +970,7 @@ class PackageController_routesTests: SnapshotTestCase {
967970
func test_documentation_current_js() async throws {
968971
try await withDependencies {
969972
$0.environment.awsDocsBucket = { "docs-bucket" }
973+
$0.environment.currentReferenceCache = { nil }
970974
} operation: {
971975
// setup
972976
Current.fetchDocumentation = { _, uri in
@@ -1029,6 +1033,7 @@ class PackageController_routesTests: SnapshotTestCase {
10291033
func test_documentation_current_data() async throws {
10301034
try await withDependencies {
10311035
$0.environment.awsDocsBucket = { "docs-bucket" }
1036+
$0.environment.currentReferenceCache = { nil }
10321037
} operation: {
10331038
// setup
10341039
Current.fetchDocumentation = { _, uri in
@@ -1146,6 +1151,7 @@ class PackageController_routesTests: SnapshotTestCase {
11461151
// Ensure references are path encoded
11471152
try await withDependencies {
11481153
$0.environment.awsDocsBucket = { "docs-bucket" }
1154+
$0.environment.currentReferenceCache = { nil }
11491155
} operation: {
11501156
// setup
11511157
let pkg = try await savePackage(on: app.db, "1")
@@ -1209,6 +1215,7 @@ class PackageController_routesTests: SnapshotTestCase {
12091215
func test_documentation_routes_tutorials() async throws {
12101216
try await withDependencies {
12111217
$0.environment.awsDocsBucket = { "docs-bucket" }
1218+
$0.environment.currentReferenceCache = { nil }
12121219
} operation: {
12131220
// setup
12141221
let pkg = try await savePackage(on: app.db, "1")
@@ -1478,6 +1485,7 @@ class PackageController_routesTests: SnapshotTestCase {
14781485
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2288
14791486
try await withDependencies {
14801487
$0.environment.awsDocsBucket = { "docs-bucket" }
1488+
$0.environment.currentReferenceCache = { .live }
14811489
} operation: {
14821490
// setup
14831491
let pkg = try await savePackage(on: app.db, "https://github.com/foo/bar".url, processingStage: .ingestion)
@@ -1555,28 +1563,31 @@ class PackageController_routesTests: SnapshotTestCase {
15551563

15561564
func test_getDocRoute_documentation_current() async throws {
15571565
nonisolated(unsafe) let cache = CurrentReferenceCache()
1558-
Current.currentReferenceCache = { cache }
1559-
// owner/repo/~/documentation/archive
1560-
let req = Request(application: app, url: "", on: app.eventLoopGroup.next())
1561-
req.parameters.set("owner", to: "owner")
1562-
req.parameters.set("repository", to: "repo")
1563-
req.parameters.set("reference", to: "~")
1564-
req.parameters.set("archive", to: "archive")
1566+
try await withDependencies {
1567+
$0.environment.currentReferenceCache = { cache }
1568+
} operation: {
1569+
// owner/repo/~/documentation/archive
1570+
let req = Request(application: app, url: "", on: app.eventLoopGroup.next())
1571+
req.parameters.set("owner", to: "owner")
1572+
req.parameters.set("repository", to: "repo")
1573+
req.parameters.set("reference", to: "~")
1574+
req.parameters.set("archive", to: "archive")
15651575

1566-
do { // No cache value available and we've not set up the db with a record to be found -> notFound must be raised
1567-
_ = try await req.getDocRoute(fragment: .documentation)
1568-
XCTFail("expected a .notFound error")
1569-
} catch let error as Abort where error.status == .notFound {
1570-
// expected error
1571-
} catch {
1572-
XCTFail("unexpected error: \(error)")
1573-
}
1576+
do { // No cache value available and we've not set up the db with a record to be found -> notFound must be raised
1577+
_ = try await req.getDocRoute(fragment: .documentation)
1578+
XCTFail("expected a .notFound error")
1579+
} catch let error as Abort where error.status == .notFound {
1580+
// expected error
1581+
} catch {
1582+
XCTFail("unexpected error: \(error)")
1583+
}
15741584

1575-
cache[owner: "owner", repository: "repo"] = "1.2.3"
1585+
cache[owner: "owner", repository: "repo"] = "1.2.3"
15761586

1577-
do { // Now with the cache in place this resolves
1578-
let route = try await req.getDocRoute(fragment: .documentation)
1579-
XCTAssertEqual(route, .init(owner: "owner", repository: "repo", docVersion: .current(referencing: "1.2.3"), fragment: .documentation, pathElements: ["archive"]))
1587+
do { // Now with the cache in place this resolves
1588+
let route = try await req.getDocRoute(fragment: .documentation)
1589+
XCTAssertEqual(route, .init(owner: "owner", repository: "repo", docVersion: .current(referencing: "1.2.3"), fragment: .documentation, pathElements: ["archive"]))
1590+
}
15801591
}
15811592
}
15821593

0 commit comments

Comments
 (0)