Skip to content

remove SourceArchiver #3874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/Basics/FileSystem+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import class Foundation.FileManager
import struct Foundation.Data
import struct Foundation.UUID
import TSCBasic

// MARK: - user level
Expand Down Expand Up @@ -144,3 +145,22 @@ extension FileSystem {
try self.createDirectory(path, recursive: true)
}
}

extension FileSystem {
public func stripFirstLevel(of path: AbsolutePath) throws {
let topLevelContents = try self.getDirectoryContents(path)
guard topLevelContents.count == 1, let rootPath = topLevelContents.first.map({ path.appending(component: $0) }), self.isDirectory(rootPath) else {
throw StringError("stripFirstLevel requires single top level directory")
}

let tempDirectory = path.parentDirectory.appending(component: UUID().uuidString)
try self.move(from: rootPath, to: tempDirectory)

let rootContents = try self.getDirectoryContents(tempDirectory)
for entry in rootContents {
try self.move(from: tempDirectory.appending(component: entry), to: path.appending(component: entry))
}

try self.removeFileTree(tempDirectory)
}
}
3 changes: 1 addition & 2 deletions Sources/PackageRegistry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
add_library(PackageRegistry
Registry.swift
RegistryConfiguration.swift
RegistryClient.swift
SourceArchiver.swift)
RegistryClient.swift)
target_link_libraries(PackageRegistry PUBLIC
Basics
PackageLoading
Expand Down
8 changes: 6 additions & 2 deletions Sources/PackageRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import PackageLoading
import PackageModel
import TSCBasic
import protocol TSCUtility.Archiver
import struct TSCUtility.ZipArchiver

/// Package registry client.
/// API specification: https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md
Expand Down Expand Up @@ -55,7 +56,7 @@ public final class RegistryClient {
{
self.configuration = configuration
self.identityResolver = identityResolver
self.archiverProvider = customArchiverProvider ?? { fileSystem in SourceArchiver(fileSystem: fileSystem) }
self.archiverProvider = customArchiverProvider ?? { fileSystem in ZipArchiver(fileSystem: fileSystem) }
self.httpClient = customHTTPClient ?? HTTPClient()
self.authorizationProvider = authorizationProvider
self.jsonDecoder = JSONDecoder.makeWithDefaults()
Expand Down Expand Up @@ -426,7 +427,10 @@ public final class RegistryClient {
// TODO: Bail if archive contains relative paths or overlapping files
archiver.extract(from: downloadPath, to: destinationPath) { result in
defer { try? fileSystem.removeFileTree(downloadPath) }
completion(result.mapError { error in
completion(result.tryMap {
// strip first level component
try fileSystem.stripFirstLevel(of: destinationPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yim-lee we would need to adopt this also in the compatibility test suite and update the fixtures accordingly

}.mapError { error in
StringError("failed extracting '\(downloadPath)' to '\(destinationPath)': \(error)")
})
}
Expand Down
72 changes: 0 additions & 72 deletions Sources/PackageRegistry/SourceArchiver.swift

This file was deleted.

16 changes: 10 additions & 6 deletions Sources/SPMTestSupport/MockArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import TSCBasic
import TSCUtility

public class MockArchiver: Archiver {
public typealias Handler = (MockArchiver, AbsolutePath, AbsolutePath, (Result<Void, Error>) -> Void) -> Void
public typealias Handler = (MockArchiver, AbsolutePath, AbsolutePath, (Result<Void, Error>) -> Void) throws -> Void

public struct Extraction: Equatable {
public let archivePath: AbsolutePath
Expand All @@ -38,11 +38,15 @@ public class MockArchiver: Archiver {
to destinationPath: AbsolutePath,
completion: @escaping (Result<Void, Error>) -> Void
) {
if let handler = self.handler {
handler(self, archivePath, destinationPath, completion)
} else {
self.extractions.append(Extraction(archivePath: archivePath, destinationPath: destinationPath))
completion(.success(()))
do {
if let handler = self.handler {
try handler(self, archivePath, destinationPath, completion)
} else {
self.extractions.append(Extraction(archivePath: archivePath, destinationPath: destinationPath))
completion(.success(()))
}
} catch {
completion(.failure(error))
}
}
}
2 changes: 1 addition & 1 deletion Sources/SPMTestSupport/MockRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private struct MockRegistryArchiver: Archiver {
let rootPath = lines[1]
for path in lines[2..<lines.count] {
let relativePath = String(path.dropFirst(rootPath.count + 1))
let targetPath = destinationPath.appending(RelativePath(relativePath))
let targetPath = destinationPath.appending(component: "package").appending(RelativePath(relativePath))
if !self.fileSystem.exists(targetPath.parentDirectory) {
try self.fileSystem.createDirectory(targetPath.parentDirectory, recursive: true)
}
Expand Down
95 changes: 95 additions & 0 deletions Tests/BasicsTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2021 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 Swift project authors
*/


@testable import Basics
import TSCBasic
import XCTest
import TSCTestSupport

final class FileSystemTests: XCTestCase {
func testStripFirstLevelComponent() throws {
let fileSystem = InMemoryFileSystem()

let rootPath = AbsolutePath("/root")
try fileSystem.createDirectory(rootPath)

let totalDirectories = Int.random(in: 0 ..< 100)
for index in 0 ..< totalDirectories {
let path = rootPath.appending(component: "dir\(index)")
try fileSystem.createDirectory(path, recursive: false)
}

let totalFiles = Int.random(in: 0 ..< 100)
for index in 0 ..< totalFiles {
let path = rootPath.appending(component: "file\(index)")
try fileSystem.writeFileContents(path, string: "\(index)")
}

do {
let contents = try fileSystem.getDirectoryContents(.root)
XCTAssertEqual(contents.count, 1)
}

try fileSystem.stripFirstLevel(of: .root)

do {
let contents = Set(try fileSystem.getDirectoryContents(.root))
XCTAssertEqual(contents.count, totalDirectories + totalFiles)

for index in 0 ..< totalDirectories {
XCTAssertTrue(contents.contains("dir\(index)"))
}
for index in 0 ..< totalFiles {
XCTAssertTrue(contents.contains("file\(index)"))
}
}
}

func testStripFirstLevelComponentErrors() throws {
do {
let fileSystem = InMemoryFileSystem()
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
}
}

do {
let fileSystem = InMemoryFileSystem()
for index in 0 ..< 3 {
let path = AbsolutePath.root.appending(component: "dir\(index)")
try fileSystem.createDirectory(path, recursive: false)
}
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
}
}

do {
let fileSystem = InMemoryFileSystem()
for index in 0 ..< 3 {
let path = AbsolutePath.root.appending(component: "file\(index)")
try fileSystem.writeFileContents(path, string: "\(index)")
}
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
}
}

do {
let fileSystem = InMemoryFileSystem()
let path = AbsolutePath.root.appending(component: "file")
try fileSystem.writeFileContents(path, string: "")
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
}
}
}
}
36 changes: 26 additions & 10 deletions Tests/PackageRegistryTests/RegistryManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,17 @@ final class RegistryManagerTests: XCTestCase {
let registryManager = RegistryClient(
configuration: configuration,
identityResolver: DefaultIdentityResolver(),
customArchiverProvider: { _ in MockArchiver() },
customArchiverProvider: { fileSystem in
MockArchiver(handler: { _, from, to, callback in
let data = try fileSystem.readFileContents(from)
XCTAssertEqual(data, emptyZipFile)

let packagePath = to.appending(component: "package")
try fileSystem.createDirectory(packagePath, recursive: true)
try fileSystem.writeFileContents(packagePath.appending(component: "Package.swift"), string: "")
callback(.success(()))
})
},
customHTTPClient: httpClient
)

Expand All @@ -368,10 +378,8 @@ final class RegistryManagerTests: XCTestCase {
checksumAlgorithm: checksumAlgorithm
)

XCTAssertNoThrow {
let data = try fileSystem.readFileContents(path)
XCTAssertEqual(data, emptyZipFile)
}
let contents = try fileSystem.getDirectoryContents(path)
XCTAssertEqual(contents, ["Package.swift"])
}

func testDownloadSourceArchiveWithoutExpectedChecksumProvided() throws {
Expand Down Expand Up @@ -448,7 +456,17 @@ final class RegistryManagerTests: XCTestCase {
let registryManager = RegistryClient(
configuration: configuration,
identityResolver: DefaultIdentityResolver(),
customArchiverProvider: { _ in MockArchiver() },
customArchiverProvider: { fileSystem in
MockArchiver(handler: { _, from, to, callback in
let data = try fileSystem.readFileContents(from)
XCTAssertEqual(data, emptyZipFile)

let packagePath = to.appending(component: "package")
try fileSystem.createDirectory(packagePath, recursive: true)
try fileSystem.writeFileContents(packagePath.appending(component: "Package.swift"), string: "")
callback(.success(()))
})
},
customHTTPClient: httpClient
)

Expand All @@ -464,10 +482,8 @@ final class RegistryManagerTests: XCTestCase {
checksumAlgorithm: checksumAlgorithm
)

XCTAssertNoThrow {
let data = try fileSystem.readFileContents(path)
XCTAssertEqual(data, emptyZipFile)
}
let contents = try fileSystem.getDirectoryContents(path)
XCTAssertEqual(contents, ["Package.swift"])
}

func testLookupIdentities() throws {
Expand Down