Skip to content

Commit 29e2e6c

Browse files
committed
Add internal archiverFactory to RegistryManager for DI
1 parent 48c3a02 commit 29e2e6c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

Sources/PackageRegistry/RegistryManager.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public enum RegistryError: Error {
2929
}
3030

3131
public final class RegistryManager {
32+
internal static var archiverFactory: (FileSystem) -> Archiver = { fileSystem in
33+
return ZipArchiver(fileSystem: fileSystem)
34+
}
35+
3236
internal static var clientFactory: () -> HTTPClientProtocol = {
3337
var configuration = HTTPClientConfiguration()
3438
configuration.followRedirects = false
@@ -169,7 +173,7 @@ public final class RegistryManager {
169173
for version: Version,
170174
of package: PackageReference,
171175
into fileSystem: FileSystem,
172-
at path: AbsolutePath,
176+
at destinationPath: AbsolutePath,
173177
expectedChecksum: ByteString? = nil,
174178
completion: @escaping (Result<Void, Error>) -> Void
175179
) {
@@ -184,12 +188,14 @@ public final class RegistryManager {
184188
method: .get,
185189
url: url,
186190
headers: [
187-
"Accept": "application/vnd.swift.registry.v1+zip"
191+
"Accept": "application/vnd.swift.registry.v1+zip",
188192
]
189193
)
190194

195+
let client = Self.clientFactory()
191196
client.execute(request) { result in
192-
completion(result.tryMap { response in
197+
switch result {
198+
case .success(let response):
193199
if response.statusCode == 200,
194200
response.headers.get("Content-Version").first == "1",
195201
response.headers.get("Content-Type").first?.hasPrefix("application/zip") == true,
@@ -210,24 +216,26 @@ public final class RegistryManager {
210216
)
211217
}
212218

213-
let archivePath = path.withExtension("zip")
219+
let archivePath = destinationPath.withExtension("zip")
214220
try fileSystem.writeFileContents(archivePath, bytes: contents)
215221

216-
try fileSystem.createDirectory(path, recursive: true)
222+
try fileSystem.createDirectory(destinationPath, recursive: true)
217223

218-
let archiver = ZipArchiver(fileSystem: fileSystem)
219-
archiver.extract(from: archivePath, to: path) { result in
220-
try? fileSystem.removeFileTree(archivePath)
224+
let archiver = Self.archiverFactory(fileSystem)
225+
archiver.extract(from: archivePath, to: destinationPath) { result in
221226
completion(result)
227+
try? fileSystem.removeFileTree(archivePath)
222228
}
223229
} catch {
224-
try? fileSystem.removeFileTree(path)
225-
throw error
230+
try? fileSystem.removeFileTree(destinationPath)
231+
completion(.failure(error))
226232
}
227233
} else {
228-
throw RegistryError.invalidResponse
234+
completion(.failure(RegistryError.invalidResponse))
229235
}
230-
})
236+
case .failure(let error):
237+
completion(.failure(error))
238+
}
231239
}
232240
}
233241
}

Tests/PackageRegistryTests/RegistryManagerTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ final class RegistryManagerTests: XCTestCase {
2727
override func setUp() {
2828
PackageModel._useLegacyIdentities = false
2929

30+
RegistryManager.archiverFactory = { _ in
31+
return MockArchiver()
32+
}
33+
3034
RegistryManager.clientFactory = {
3135
let configuration = URLSessionConfiguration.default
3236
configuration.protocolClasses = [MockPackageRegistryURLProtocol.self] + (configuration.protocolClasses ?? [])

0 commit comments

Comments
 (0)