Skip to content

Commit c27410e

Browse files
committed
Change authTokens to be a function instead
1 parent 0e4a847 commit c27410e

File tree

5 files changed

+25
-29
lines changed

5 files changed

+25
-29
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
@@ -68,14 +68,6 @@ public struct PackageCollections: PackageCollectionsProtocol {
6868
try self.metadataProvider.close()
6969
}
7070

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

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

Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

Lines changed: 5 additions & 5 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
}
@@ -212,22 +212,22 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
212212
}
213213

214214
public struct Configuration {
215+
public var authTokens: () -> [AuthTokenType: String]?
215216
public var apiLimitWarningThreshold: Int
216-
public var authTokens: [AuthTokenType: String]?
217217
public var cacheDir: AbsolutePath
218-
public var cacheSizeInMegabytes: Int
219218
public var cacheTTLInSeconds: Int
219+
public var cacheSizeInMegabytes: Int
220220

221-
public init(authTokens: [AuthTokenType: String]? = nil,
221+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil },
222222
apiLimitWarningThreshold: Int? = nil,
223223
cacheDir: AbsolutePath? = nil,
224224
cacheTTLInSeconds: Int? = nil,
225225
cacheSizeInMegabytes: Int? = nil) {
226226
self.authTokens = authTokens
227227
self.apiLimitWarningThreshold = apiLimitWarningThreshold ?? 5
228228
self.cacheDir = cacheDir.map(resolveSymlinks) ?? localFileSystem.swiftPMCacheDirectory.appending(components: "package-metadata")
229-
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
230229
self.cacheTTLInSeconds = cacheTTLInSeconds ?? 3600
230+
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
231231
}
232232
}
233233

Tests/PackageCollectionsTests/GitHubPackageMetadataProviderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
226226
var configuration = GitHubPackageMetadataProvider.Configuration()
227227
configuration.cacheDir = tmpPath
228228
var provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
229-
provider.configuration.authTokens = authTokens
229+
provider.configuration.authTokens = { authTokens }
230230
defer { XCTAssertNoThrow(try provider.close()) }
231231

232232
let reference = PackageReference(repository: RepositorySpecifier(url: repoURL))
@@ -335,7 +335,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
335335
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
336336
var configuration = GitHubPackageMetadataProvider.Configuration()
337337
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
338-
configuration.authTokens = [.github("api.github.com"): token]
338+
configuration.authTokens = { [.github("api.github.com"): token] }
339339
}
340340
configuration.apiLimitWarningThreshold = 50
341341
configuration.cacheTTLInSeconds = -1 // Disable cache so we hit the API

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)