Skip to content

Commit

Permalink
Revert "Revert "Avoid use of temp_await in `PackageRegistryToolTest…
Browse files Browse the repository at this point in the history
…s.swift`" (#7169)"

This reverts commit 1eb64ab.
  • Loading branch information
MaxDesiatov committed Dec 11, 2023
1 parent d457fa4 commit 924b612
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
31 changes: 31 additions & 0 deletions Sources/Basics/Archiver/Archiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public protocol Archiver {
}

extension Archiver {
/// Asynchronously extracts the contents of an archive to a destination folder.
///
/// - Parameters:
/// - archivePath: The `AbsolutePath` to the archive to extract.
/// - destinationPath: The `AbsolutePath` to the directory to extract to.
public func extract(
from archivePath: AbsolutePath,
to destinationPath: AbsolutePath
Expand All @@ -80,5 +85,31 @@ extension Archiver {
try await safe_async {
self.validate(path: path, completion: $0)
}
}

/// Asynchronously compresses the contents of a directory to a destination archive.
///
/// - Parameters:
/// - directory: The `AbsolutePath` to the archive to extract.
/// - destinationPath: The `AbsolutePath` to the directory to extract to.
public func compress(
directory: AbsolutePath,
to destinationPath: AbsolutePath
) async throws {
try await withCheckedThrowingContinuation {
self.compress(directory: directory, to: destinationPath, completion: $0.resume(with:))
}
}

/// Asynchronously validates if a file is an archive.
///
/// - Parameters:
/// - path: The `AbsolutePath` to the archive to validate.
public func validate(
path: AbsolutePath
) async throws -> Bool {
try await withCheckedThrowingContinuation {
self.validate(path: path, completion: $0.resume(with:))
}
}
}
22 changes: 11 additions & 11 deletions Tests/CommandsTests/PackageRegistryToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ final class PackageRegistryToolTests: CommandsTestCase {

// TODO: Test example with login and password

func testArchiving() throws {
func testArchiving() async throws {
#if os(Linux)
// needed for archiving
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
Expand All @@ -293,7 +293,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
let metadataFilename = SwiftPackageRegistryTool.Publish.metadataFilename

// git repo
try withTemporaryDirectory { temporaryDirectory in
try await withTemporaryDirectory { temporaryDirectory in
let packageDirectory = temporaryDirectory.appending("MyPackage")
try localFileSystem.createDirectory(packageDirectory)

Expand All @@ -320,12 +320,12 @@ final class PackageRegistryToolTests: CommandsTestCase {
observabilityScope: observability.topScope
)

try validatePackageArchive(at: archivePath)
try await validatePackageArchive(at: archivePath)
XCTAssertTrue(archivePath.isDescendant(of: workingDirectory))
}

// not a git repo
try withTemporaryDirectory { temporaryDirectory in
try await withTemporaryDirectory { temporaryDirectory in
let packageDirectory = temporaryDirectory.appending("MyPackage")
try localFileSystem.createDirectory(packageDirectory)

Expand All @@ -350,11 +350,11 @@ final class PackageRegistryToolTests: CommandsTestCase {
observabilityScope: observability.topScope
)

try validatePackageArchive(at: archivePath)
try await validatePackageArchive(at: archivePath)
}

// canonical metadata location
try withTemporaryDirectory { temporaryDirectory in
try await withTemporaryDirectory { temporaryDirectory in
let packageDirectory = temporaryDirectory.appending("MyPackage")
try localFileSystem.createDirectory(packageDirectory)

Expand Down Expand Up @@ -385,17 +385,17 @@ final class PackageRegistryToolTests: CommandsTestCase {
observabilityScope: observability.topScope
)

let extractedPath = try validatePackageArchive(at: archivePath)
let extractedPath = try await validatePackageArchive(at: archivePath)
XCTAssertFileExists(extractedPath.appending(component: metadataFilename))
}

@discardableResult
func validatePackageArchive(at archivePath: AbsolutePath) throws -> AbsolutePath {
func validatePackageArchive(at archivePath: AbsolutePath) async throws -> AbsolutePath {
XCTAssertFileExists(archivePath)
let archiver = ZipArchiver(fileSystem: localFileSystem)
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
try localFileSystem.createDirectory(extractPath)
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
try await archiver.extract(from: archivePath, to: extractPath)
try localFileSystem.stripFirstLevel(of: extractPath)
XCTAssertFileExists(extractPath.appending("Package.swift"))
return extractPath
Expand Down Expand Up @@ -550,7 +550,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
let archiver = ZipArchiver(fileSystem: localFileSystem)
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
try localFileSystem.createDirectory(extractPath)
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
try await archiver.extract(from: archivePath, to: extractPath)
try localFileSystem.stripFirstLevel(of: extractPath)

let manifestInArchive = try localFileSystem.readFileContents(extractPath.appending(manifestFile)).contents
Expand Down Expand Up @@ -963,7 +963,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
let archiver = ZipArchiver(fileSystem: localFileSystem)
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
try localFileSystem.createDirectory(extractPath)
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
try await archiver.extract(from: archivePath, to: extractPath)
try localFileSystem.stripFirstLevel(of: extractPath)

let manifestSignature = try ManifestSignatureParser.parse(
Expand Down

0 comments on commit 924b612

Please sign in to comment.