Skip to content

Commit bb67334

Browse files
committed
Change authTokens to be a function instead
1 parent 2c7b4d9 commit bb67334

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

Sources/PackageCollections/PackageCollections+Configuration.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift project authors
4+
Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -16,9 +16,9 @@ extension PackageCollections {
1616
// JSONPackageCollectionValidator: maximumPackageCount, maximumMajorVersionCount, maximumMinorVersionCount
1717

1818
/// Auth tokens for the collections or metadata provider
19-
public var authTokens: [AuthTokenType: String]?
19+
public var authTokens: () -> [AuthTokenType: String]?
2020

21-
public init(authTokens: [AuthTokenType: String]? = nil) {
21+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil }) {
2222
self.authTokens = authTokens
2323
}
2424
}

Sources/PackageCollections/PackageCollections.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public struct PackageCollections: PackageCollectionsProtocol {
2121
static let isSupportedPlatform = false
2222
#endif
2323

24-
var configuration: Configuration
24+
let configuration: Configuration
2525
private let diagnosticsEngine: DiagnosticsEngine?
2626
private let storageContainer: (storage: Storage, owned: Bool)
2727
private let collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider]
28-
var metadataProvider: PackageMetadataProvider
28+
let metadataProvider: PackageMetadataProvider
2929

3030
private var storage: Storage {
3131
self.storageContainer.storage
@@ -67,14 +67,6 @@ public struct PackageCollections: PackageCollectionsProtocol {
6767
}
6868
}
6969

70-
public mutating func updateAuthTokens(_ authTokens: [AuthTokenType: String]?) {
71-
self.configuration.authTokens = authTokens
72-
if var metadataProvider = self.metadataProvider as? GitHubPackageMetadataProvider {
73-
metadataProvider.configuration.authTokens = authTokens
74-
self.metadataProvider = metadataProvider
75-
}
76-
}
77-
7870
// MARK: - Collections
7971

8072
public func listCollections(identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,

Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
194194
options.validResponseCodes = validResponseCodes
195195
options.authorizationProvider = { url in
196196
url.host.flatMap { host in
197-
self.configuration.authTokens?[.github(host)].flatMap { token in
197+
self.configuration.authTokens()?[.github(host)].flatMap { token in
198198
"token \(token)"
199199
}
200200
}
@@ -213,11 +213,11 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
213213

214214
public struct Configuration {
215215
public var apiLimitWarningThreshold: Int
216-
public var authTokens: [AuthTokenType: String]?
216+
public var authTokens: () -> [AuthTokenType: String]?
217217
public var cacheTTLInSeconds: Int
218218
public var cacheSize: Int
219219

220-
public init(authTokens: [AuthTokenType: String]? = nil,
220+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil },
221221
apiLimitWarningThreshold: Int? = nil,
222222
cacheTTLInSeconds: Int? = nil,
223223
cacheSize: Int? = nil) {

Tests/PackageCollectionsTests/GitHubPackageMetadataProviderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
200200
httpClient.configuration.circuitBreakerStrategy = .none
201201
httpClient.configuration.retryStrategy = .none
202202
var provider = GitHubPackageMetadataProvider(httpClient: httpClient)
203-
provider.configuration.authTokens = authTokens
203+
provider.configuration.authTokens = { authTokens }
204204
let reference = PackageReference(repository: RepositorySpecifier(url: repoURL))
205205
XCTAssertThrowsError(try tsc_await { callback in provider.get(reference, callback: callback) }, "should throw error") { error in
206206
XCTAssertEqual(error as? GitHubPackageMetadataProvider.Errors, .invalidAuthToken(apiURL))
@@ -290,7 +290,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
290290
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
291291
var configuration = GitHubPackageMetadataProvider.Configuration()
292292
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
293-
configuration.authTokens = [.github("api.github.com"): token]
293+
configuration.authTokens = { [.github("api.github.com"): token] }
294294
}
295295
configuration.apiLimitWarningThreshold = 50
296296
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)

Tests/PackageCollectionsTests/PackageCollectionsTests.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,35 @@ import TSCUtility
1919

2020
final class PackageCollectionsTests: XCTestCase {
2121
func testUpdateAuthTokens() throws {
22-
let configuration = PackageCollections.Configuration()
22+
var authTokens: [AuthTokenType: String]? = [:]
23+
24+
let configuration = PackageCollections.Configuration(authTokens: { authTokens })
2325
let storage = makeMockStorage()
2426
defer { XCTAssertNoThrow(try storage.close()) }
2527

26-
let metadataProviderConfig = GitHubPackageMetadataProvider.Configuration(cacheTTLInSeconds: -1)
28+
// disable cache for this test to avoid setup/cleanup
29+
let metadataProviderConfig = GitHubPackageMetadataProvider.Configuration(authTokens: configuration.authTokens, cacheTTLInSeconds: -1)
2730
let metadataProvider = GitHubPackageMetadataProvider(configuration: metadataProviderConfig)
28-
var packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: [:], metadataProvider: metadataProvider)
31+
let packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: [:], metadataProvider: metadataProvider)
2932

30-
XCTAssertNil(packageCollections.configuration.authTokens)
33+
XCTAssertEqual(0, packageCollections.configuration.authTokens()?.count)
3134
do {
3235
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
3336
return XCTFail("Expected GitHubPackageMetadataProvider")
3437
}
35-
XCTAssertNil(githubMetadataProvider.configuration.authTokens)
38+
XCTAssertEqual(0, githubMetadataProvider.configuration.authTokens()?.count)
3639
}
3740

38-
let authTokens: [AuthTokenType: String] = [.github("github.test"): "topsekret"]
39-
packageCollections.updateAuthTokens(authTokens)
41+
authTokens![.github("github.test")] = "topsekret"
4042

41-
XCTAssertEqual(authTokens, packageCollections.configuration.authTokens)
43+
// Check that authTokens change is propagated to PackageMetadataProvider
44+
XCTAssertEqual(1, packageCollections.configuration.authTokens()?.count)
4245
do {
4346
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
4447
return XCTFail("Expected GitHubPackageMetadataProvider")
4548
}
46-
XCTAssertEqual(authTokens, githubMetadataProvider.configuration.authTokens)
49+
XCTAssertEqual(1, githubMetadataProvider.configuration.authTokens()?.count)
50+
XCTAssertEqual(authTokens, githubMetadataProvider.configuration.authTokens())
4751
}
4852
}
4953

0 commit comments

Comments
 (0)