/
GitHubPackageMetadataProviderTests.swift
376 lines (328 loc) · 19.7 KB
/
GitHubPackageMetadataProviderTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import XCTest
import Basics
@testable import PackageCollections
import PackageModel
import SourceControl
import SPMTestSupport
import enum TSCBasic.ProcessEnv
import struct TSCUtility.Version
class GitHubPackageMetadataProviderTests: XCTestCase {
func testBaseURL() throws {
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
do {
let sshURLRetVal = GitHubPackageMetadataProvider.apiURL("[email protected]:octocat/Hello-World.git")
XCTAssertEqual(apiURL, sshURLRetVal)
}
do {
let httpsURLRetVal = GitHubPackageMetadataProvider.apiURL("https://github.com/octocat/Hello-World.git")
XCTAssertEqual(apiURL, httpsURLRetVal)
}
do {
let httpsURLRetVal = GitHubPackageMetadataProvider.apiURL("https://github.com/octocat/Hello-World")
XCTAssertEqual(apiURL, httpsURLRetVal)
}
XCTAssertNil(GitHubPackageMetadataProvider.apiURL("bad/Hello-World.git"))
}
func testGood() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
let releasesURL = URL("https://api.github.com/repos/octocat/Hello-World/releases?per_page=20")
try await fixture(name: "Collections", createGitRepo: false) { fixturePath in
let handler: LegacyHTTPClient.Handler = { request, _, completion in
switch (request.method, request.url) {
case (.get, apiURL):
let path = fixturePath.appending(components: "GitHub", "metadata.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
case (.get, releasesURL):
let path = fixturePath.appending(components: "GitHub", "releases.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
case (.get, apiURL.appendingPathComponent("contributors")):
let path = fixturePath.appending(components: "GitHub", "contributors.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
case (.get, apiURL.appendingPathComponent("readme")):
let path = fixturePath.appending(components: "GitHub", "readme.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
case (.get, apiURL.appendingPathComponent("license")):
let path = fixturePath.appending(components: "GitHub", "license.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
case (.get, apiURL.appendingPathComponent("languages")):
let path = fixturePath.appending(components: "GitHub", "languages.json")
let data: Data = try! localFileSystem.readFileContents(path)
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
default:
XCTFail("method and url should match")
}
}
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
let metadata = try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString)
XCTAssertEqual(metadata.summary, "This your first repo!")
XCTAssertEqual(metadata.versions.count, 2)
XCTAssertEqual(metadata.versions[0].version, TSCUtility.Version(tag: "v2.0.0"))
XCTAssertEqual(metadata.versions[0].title, "2.0.0")
XCTAssertEqual(metadata.versions[0].summary, "Description of the release")
XCTAssertEqual(metadata.versions[0].author?.username, "octocat")
XCTAssertEqual(metadata.versions[1].version, TSCUtility.Version("1.0.0"))
XCTAssertEqual(metadata.versions[1].title, "1.0.0")
XCTAssertEqual(metadata.versions[1].summary, "Description of the release")
XCTAssertEqual(metadata.versions[1].author?.username, "octocat")
XCTAssertEqual(metadata.authors, [PackageCollectionsModel.Package.Author(username: "octocat",
url: "https://api.github.com/users/octocat",
service: .init(name: "GitHub"))])
XCTAssertEqual(metadata.readmeURL, "https://raw.githubusercontent.com/octokit/octokit.rb/master/README.md")
XCTAssertEqual(metadata.license?.type, PackageCollectionsModel.LicenseType.MIT)
XCTAssertEqual(metadata.license?.url, "https://raw.githubusercontent.com/benbalter/gman/master/LICENSE?lab=true")
XCTAssertEqual(metadata.watchersCount, 80)
XCTAssertEqual(metadata.languages, ["Swift", "Shell", "C"])
}
}
}
func testRepoNotFound() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let handler: LegacyHTTPClient.Handler = { _, _, completion in
completion(.success(.init(statusCode: 404)))
}
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString), "should throw error") { error in
XCTAssert(error is NotFoundError, "\(error)")
}
}
}
func testOthersNotFound() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
try await fixture(name: "Collections", createGitRepo: false) { fixturePath in
let path = fixturePath.appending(components: "GitHub", "metadata.json")
let data = try Data(localFileSystem.readFileContents(path).contents)
let handler: LegacyHTTPClient.Handler = { request, _, completion in
switch (request.method, request.url) {
case (.get, apiURL):
completion(.success(.init(statusCode: 200,
headers: .init([.init(name: "Content-Length", value: "\(data.count)")]),
body: data)))
default:
completion(.success(.init(statusCode: 500)))
}
}
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
let metadata = try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString)
XCTAssertEqual(metadata.summary, "This your first repo!")
XCTAssertEqual(metadata.versions, [])
XCTAssertNil(metadata.authors)
XCTAssertNil(metadata.readmeURL)
XCTAssertEqual(metadata.watchersCount, 80)
}
}
}
func testPermissionDenied() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
let handler: LegacyHTTPClient.Handler = { _, _, completion in
completion(.success(.init(statusCode: 401)))
}
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString), "should throw error") { error in
XCTAssertEqual(error as? GitHubPackageMetadataProviderError, .permissionDenied(apiURL))
}
}
}
func testInvalidAuthToken() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
let authTokens = [AuthTokenType.github("github.com"): "foo"]
let handler: LegacyHTTPClient.Handler = { request, _, completion in
if request.headers.get("Authorization").first == "token \(authTokens.first!.value)" {
completion(.success(.init(statusCode: 401)))
} else {
XCTFail("expected correct authorization header")
completion(.success(.init(statusCode: 500)))
}
}
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
configuration.authTokens = { authTokens }
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString), "should throw error") { error in
XCTAssertEqual(error as? GitHubPackageMetadataProviderError, .invalidAuthToken(apiURL))
}
}
}
func testAPILimit() async throws {
try await testWithTemporaryDirectory { tmpPath in
let repoURL = SourceControlURL("https://github.com/octocat/Hello-World.git")
let apiURL = URL("https://api.github.com/repos/octocat/Hello-World")
let total = 5
var remaining = total
try await fixture(name: "Collections", createGitRepo: false) { fixturePath in
let path = fixturePath.appending(components: "GitHub", "metadata.json")
let data = try Data(localFileSystem.readFileContents(path).contents)
let handler: LegacyHTTPClient.Handler = { request, _, completion in
var headers = HTTPClientHeaders()
headers.add(name: "X-RateLimit-Limit", value: "\(total)")
headers.add(name: "X-RateLimit-Remaining", value: "\(remaining)")
if remaining == 0 {
completion(.success(.init(statusCode: 403, headers: headers)))
} else if request.url == apiURL {
remaining = remaining - 1
headers.add(name: "Content-Length", value: "\(data.count)")
completion(.success(.init(statusCode: 200,
headers: headers,
body: data)))
} else {
completion(.success(.init(statusCode: 500)))
}
}
// Disable cache so we hit the API
let configuration = GitHubPackageMetadataProvider.Configuration(disableCache: true)
let httpClient = LegacyHTTPClient(handler: handler)
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
for index in 0 ... total * 2 {
if index >= total {
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString), "should throw error") { error in
XCTAssertEqual(error as? GitHubPackageMetadataProviderError, .apiLimitsExceeded(apiURL, total))
}
} else {
_ = try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString)
}
}
}
}
}
func testInvalidURL() async throws {
try await testWithTemporaryDirectory { tmpPath in
try await fixture(name: "Collections", createGitRepo: false) { _ in
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration)
defer { XCTAssertNoThrow(try provider.close()) }
let url = UUID().uuidString
let identity = PackageIdentity(urlString: url)
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: identity, location: url), "should throw error") { error in
XCTAssertEqual(error as? GitHubPackageMetadataProviderError, .invalidSourceControlURL(url))
}
}
}
}
func testInvalidURL2() async throws {
try await testWithTemporaryDirectory { tmpPath in
try await fixture(name: "Collections", createGitRepo: false) { _ in
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
let provider = GitHubPackageMetadataProvider(configuration: configuration)
defer { XCTAssertNoThrow(try provider.close()) }
let path = AbsolutePath.root
let identity = PackageIdentity(path: path)
await XCTAssertAsyncThrowsError(try await provider.syncGet(identity: identity, location: path.pathString), "should throw error") { error in
XCTAssertEqual(error as? GitHubPackageMetadataProviderError, .invalidSourceControlURL(path.pathString))
}
}
}
}
func testForRealz() async throws {
#if ENABLE_GITHUB_NETWORK_TEST
#else
try XCTSkipIf(true)
#endif
let repoURL = SourceControlURL("https://github.com/apple/swift-numerics.git")
let httpClient = LegacyHTTPClient()
httpClient.configuration.circuitBreakerStrategy = .none
httpClient.configuration.retryStrategy = .none
httpClient.configuration.requestHeaders = .init()
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
var configuration = GitHubPackageMetadataProvider.Configuration(disableCache: true) // Disable cache so we hit the API
if let token = ProcessEnv.block["GITHUB_API_TOKEN"] {
configuration.authTokens = { [.github("github.com"): token] }
}
configuration.apiLimitWarningThreshold = 50
let provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
defer { XCTAssertNoThrow(try provider.close()) }
for _ in 0 ... 60 {
let metadata = try await provider.syncGet(identity: .init(url: repoURL), location: repoURL.absoluteString)
XCTAssertNotNil(metadata)
XCTAssert(metadata.versions.count > 0)
XCTAssert(metadata.keywords!.count > 0)
XCTAssertNotNil(metadata.license)
XCTAssert(metadata.authors!.count > 0)
}
}
}
internal extension GitHubPackageMetadataProvider {
init(configuration: Configuration = .init(), httpClient: LegacyHTTPClient? = nil) {
self.init(
configuration: configuration,
observabilityScope: ObservabilitySystem.NOOP,
httpClient: httpClient
)
}
}
private extension GitHubPackageMetadataProvider {
func syncGet(identity: PackageIdentity, location: String) async throws -> Model.PackageBasicMetadata {
try await safe_async { callback in
self.get(identity: identity, location: location) { result, _ in callback(result) }
}
}
}