Skip to content

Commit

Permalink
Revert "Merge pull request #29 from SwiftPackageIndex/fix-relative-pa…
Browse files Browse the repository at this point in the history
…th-zipping"

This reverts commit 717b3ce, reversing
changes made to 5edeb1e.
  • Loading branch information
finestructure committed Jul 11, 2024
1 parent 2882b7c commit 9cb9357
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v3
- name: Run tests
run: docker run -v "$PWD":/host -w /host swift:5.10.0-amazonlinux2 swift test
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
with:
# Fetch all history to ensure the version number (tag) is resolved to create the
# version info during the build step.
Expand Down
9 changes: 8 additions & 1 deletion Sources/DocUploadBundle/DocUploadBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ public struct DocUploadBundle {
let metadataURL = URL(fileURLWithPath: "\(workDir)/metadata.json")
try JSONEncoder().encode(metadata).write(to: metadataURL)

try Zipper.zip(paths: [metadataURL, URL(fileURLWithPath: sourcePath)], to: archiveURL, method: method)
switch method {
case .library, .zipTool(workingDirectory: .some(_)):
try Zipper.zip(paths: [metadataURL, URL(fileURLWithPath: sourcePath)], to: archiveURL, method: method)

case .zipTool(.none):
// By default, run the zip tool in the working directory
try Zipper.zip(paths: [metadataURL, URL(fileURLWithPath: sourcePath)], to: archiveURL, method: .zipTool(workingDirectory: workDir))
}

return archiveURL.path
}
Expand Down
60 changes: 18 additions & 42 deletions Sources/DocUploadBundle/Zipper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,39 @@ public enum Zipper {
public static func zip(paths inputPaths: [URL], to outputPath: URL, method: Method = .library) throws {
switch method {
case .library:
do {
try Zip.zipFiles(paths: inputPaths, zipFilePath: outputPath, password: nil, progress: nil)
} catch let error as ZipError {
switch error {
case .fileNotFound: throw Error.fileNotFound
case .unzipFail: throw Error.unzipFail
case .zipFail: throw Error.zipFail
}
}
catch {
throw Error.generic(reason: "\(error)")
}
do { try Zip.zipFiles(paths: inputPaths, zipFilePath: outputPath, password: nil, progress: nil) }
catch ZipError.fileNotFound { throw Error.fileNotFound }
catch ZipError.unzipFail { throw Error.unzipFail }
catch ZipError.zipFail { throw Error.zipFail }
catch { throw Error.generic(reason: "\(error)") }

case .zipTool:
case let .zipTool(cwd):
do {
try withTempDir { tempDir in
let tempURL = URL(fileURLWithPath: tempDir)
// Copy inputs to tempDir
for source in inputPaths {
let target = tempURL.appendingPathComponent(source.lastPathComponent)
try FileManager.default.copyItem(at: source, to: target)
}

// Run zip
let process = Process()
process.executableURL = zip
process.arguments = ["-q", "-r", outputPath.path] + inputPaths.map(\.lastPathComponent)
process.currentDirectoryURL = tempURL
try process.run()
process.waitUntilExit()
}
let process = Process()
process.executableURL = zip
process.arguments = ["-q", "-r", outputPath.path] + inputPaths.map(\.lastPathComponent)
process.currentDirectoryURL = cwd.map(URL.init(fileURLWithPath:))
try process.run()
process.waitUntilExit()
} catch {
throw Error.generic(reason: "\(error)")
}
}
}

public static func unzip(from inputPath: URL, to outputPath: URL, fileOutputHandler: ((_ unzippedFile: URL) -> Void)? = nil) throws {
do {
try Zip.unzipFile(inputPath, destination: outputPath, overwrite: true, password: nil, fileOutputHandler: fileOutputHandler)
} catch let error as ZipError {
switch error {
case .fileNotFound: throw Error.fileNotFound
case .unzipFail: throw Error.unzipFail
case .zipFail: throw Error.zipFail
}
}
catch {
throw Error.generic(reason: "\(error)")
}
do { try Zip.unzipFile(inputPath, destination: outputPath, overwrite: true, password: nil, fileOutputHandler: fileOutputHandler) }
catch ZipError.fileNotFound { throw Error.fileNotFound }
catch ZipError.unzipFail { throw Error.unzipFail }
catch ZipError.zipFail { throw Error.zipFail }
catch { throw Error.generic(reason: "\(error)") }
}

static let zip = URL(fileURLWithPath: "/usr/bin/zip")

public enum Method {
case library
case zipTool
case zipTool(workingDirectory: String? = nil)
}

public enum Error: Swift.Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,3 @@ func withTempDir<T>(body: (String) async throws -> T) async throws -> T {
let tmp = try TempDir()
return try await body(tmp.path)
}

func withTempDir<T>(body: (String) throws -> T) throws -> T {
let tmp = try TempDir()
return try body(tmp.path)
}
45 changes: 4 additions & 41 deletions Tests/DocUploadBundleTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ZipTests: XCTestCase {

func test_unzip() async throws {
// Test basic unzip behaviour we expect from the library we use
try withTempDir { tempDir in
try await withTempDir { tempDir in
let tempURL = URL(fileURLWithPath: tempDir)
let zipFile = fixtureUrl(for: "out.zip")
let outDir = tempURL.appendingPathComponent("out")
Expand All @@ -41,7 +41,7 @@ final class ZipTests: XCTestCase {

func test_zip_roundtrip() async throws {
// Test basic zip roundtrip
try withTempDir { tempDir in
try await withTempDir { tempDir in
// temp
let tempURL = URL(fileURLWithPath: tempDir)

Expand Down Expand Up @@ -92,7 +92,7 @@ final class ZipTests: XCTestCase {
try XCTSkipIf(!FileManager.default.fileExists(atPath: Zipper.zip.path))

// Test basic zip roundtrip with the shellTool method
try withTempDir { tempDir in
try await withTempDir { tempDir in
// temp
let tempURL = URL(fileURLWithPath: tempDir)

Expand All @@ -117,7 +117,7 @@ final class ZipTests: XCTestCase {
try "c".write(to: fileC, atomically: true, encoding: .utf8)

let zipFile = tempURL.appendingPathComponent("out.zip")
try Zipper.zip(paths: [fileA, subdir], to: zipFile, method: .zipTool)
try Zipper.zip(paths: [fileA, subdir], to: zipFile, method: .zipTool(workingDirectory: tempDir))
XCTAssert(FileManager.default.fileExists(atPath: zipFile.path))

do { // unzip what we zipped and check results
Expand All @@ -139,41 +139,4 @@ final class ZipTests: XCTestCase {
}
}

func test_zip_roundtrip_shellTool_relative_paths() async throws {
try XCTSkipIf(!FileManager.default.fileExists(atPath: Zipper.zip.path))

// Test basic zip roundtrip with the shellTool method and relative paths
try withTempDir { tempDir in
// DocBundle components
// metadataURL: tempDir/metadata.json
// sourceURL: tempDir/.docs/owner/repo/ref
// should be zipped as
// - metadata.json
// - ref
// at the top level as relative paths.
let tempURL = URL(fileURLWithPath: tempDir)
let metadataURL = tempURL.appendingPathComponent("metadata.json")
try "metadata".write(to: metadataURL, atomically: true, encoding: .utf8)
let sourceURL = tempURL.appendingPathComponent("docs/owner/repo/ref")
try FileManager.default.createDirectory(at: sourceURL, withIntermediateDirectories: true)
let indexHTML = sourceURL.appendingPathComponent("index.html")
try "index".write(to: indexHTML, atomically: true, encoding: .utf8)

// MUT
let zipFile = tempURL.appendingPathComponent("out.zip")
try Zipper.zip(paths: [metadataURL, sourceURL], to: zipFile, method: .zipTool)

do { // validate
let unzipDir = tempURL.appendingPathComponent("unzip")
try Zipper.unzip(from: zipFile, to: unzipDir)
let metadataURL = unzipDir.appendingPathComponent("metadata.json")
let indexHTML = unzipDir.appendingPathComponent("ref/index.html")
XCTAssert(FileManager.default.fileExists(atPath: metadataURL.path))
XCTAssert(FileManager.default.fileExists(atPath: indexHTML.path))
XCTAssertEqual(try String(contentsOf: metadataURL), "metadata")
XCTAssertEqual(try String(contentsOf: indexHTML), "index")
}
}
}

}

0 comments on commit 9cb9357

Please sign in to comment.