Skip to content

Commit a9e4d1f

Browse files
Merge pull request #27 from SwiftPackageIndex/regression-test
Fix swift-metrics unzipping
2 parents a21d31c + 65f65be commit a9e4d1f

File tree

5 files changed

+72
-37
lines changed

5 files changed

+72
-37
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@
244244
}
245245
},
246246
{
247-
"identity" : "zipfoundation",
247+
"identity" : "zip",
248248
"kind" : "remoteSourceControl",
249-
"location" : "https://github.com/weichsel/ZIPFoundation.git",
249+
"location" : "https://github.com/marmelroy/Zip.git",
250250
"state" : {
251-
"revision" : "02b6abe5f6eef7e3cbd5f247c5cc24e246efcfe0",
252-
"version" : "0.9.19"
251+
"revision" : "67fa55813b9e7b3b9acee9c0ae501def28746d76",
252+
"version" : "2.1.2"
253253
}
254254
}
255255
],

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let package = Package(
4040
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "0.1.0"),
4141
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.1"),
4242
.package(url: "https://github.com/soto-project/soto-s3-file-transfer.git", from: "1.2.0"),
43-
.package(url: "https://github.com/weichsel/ZIPFoundation.git", from: "0.9.19"),
43+
.package(url: "https://github.com/marmelroy/Zip.git", from: "2.1.2"),
4444
.package(url: "https://github.com/pointfreeco/swift-dependencies.git", from: "1.0.0")
4545
],
4646
targets: [
@@ -56,7 +56,7 @@ let package = Package(
5656
linkerSettings: linkerSettings
5757
),
5858
.target(name: "DocUploadBundle", dependencies: [
59-
.product(name: "ZIPFoundation", package: "zipfoundation"),
59+
.product(name: "Zip", package: "Zip"),
6060
.product(name: "Dependencies", package: "swift-dependencies")
6161
]),
6262
.testTarget(name: "DocUploadBundleTests", dependencies: ["DocUploadBundle"], exclude: ["Fixtures"]),

Sources/DocUploadBundle/Zipper.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@
1414

1515
import Foundation
1616

17-
import ZIPFoundation
17+
import Zip
1818

1919

2020
enum Zipper {
2121
static func zip(paths inputPaths: [URL], to outputPath: URL) throws {
22-
let archive = try Archive(url: outputPath, accessMode: .create)
23-
for url in inputPaths {
24-
try archive.addEntry(with: url.lastPathComponent, fileURL: url)
25-
}
22+
try Zip.zipFiles(paths: inputPaths, zipFilePath: outputPath, password: nil, progress: nil)
2623
}
2724

2825
static func unzip(from inputPath: URL, to outputPath: URL, fileOutputHandler: ((_ unzippedFile: URL) -> Void)? = nil) throws {
29-
try FileManager.default.createDirectory(at: outputPath, withIntermediateDirectories: true)
30-
try FileManager.default.unzipItem(at: inputPath, to: outputPath)
26+
do {
27+
try Zip.unzipFile(inputPath, destination: outputPath, overwrite: true, password: nil, fileOutputHandler: fileOutputHandler)
28+
} catch ZipError.unzipFail {
29+
// Try OS level unzip as a fallback
30+
// See https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/3069
31+
let unzip = URL(fileURLWithPath: "/usr/bin/unzip")
32+
let process = try Process.run(unzip, arguments: ["-q", inputPath.path, "-d", outputPath.path])
33+
process.waitUntilExit()
34+
}
3135
}
3236
}
3337

Tests/DocUploadBundleTests/DocUploadBundleTests.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,26 @@ final class DocUploadBundleTests: XCTestCase {
107107
XCTAssertNoThrow(
108108
try DocUploadBundle.unzip(bundle: url.path, outputPath: tempDir)
109109
)
110-
XCTAssert(FileManager.default.fileExists(atPath: tempDir + "/metadata.json"))
111-
XCTAssert(FileManager.default.fileExists(atPath: tempDir + "/main/index.html"))
112-
XCTAssert(FileManager.default.fileExists(atPath: tempDir + "/main/index/index.json"))
110+
for pathComponent in ["metadata.json",
111+
"main/index.html",
112+
"main/index/index.json"] {
113+
let path = tempDir + "/" + pathComponent
114+
XCTAssertTrue(FileManager.default.fileExists(atPath: path), "does not exist: \(path)")
115+
}
116+
// test roundtrip, to ensure the zip library can zip/unzip its own product
117+
// zip
118+
let urls = [tempDir + "/metadata.json",
119+
tempDir + "/main"].map(URL.init(fileURLWithPath:))
120+
let zipped = URL(fileURLWithPath: tempDir + "/out.zip")
121+
try Zipper.zip(paths: urls, to: zipped)
122+
XCTAssertTrue(FileManager.default.fileExists(atPath: zipped.path))
123+
// unzip
124+
let out = URL(fileURLWithPath: tempDir + "/out")
125+
try Zipper.unzip(from: zipped, to: out)
126+
XCTAssertTrue(FileManager.default.fileExists(atPath: out.path))
127+
XCTAssertTrue(FileManager.default.fileExists(atPath: out.appendingPathComponent("metadata.json").path))
128+
XCTAssertTrue(FileManager.default.fileExists(atPath: out.appendingPathComponent("main/index.html").path))
129+
XCTAssertTrue(FileManager.default.fileExists(atPath: out.appendingPathComponent("main/index/index.json").path))
113130
}
114131
}
115132

Tests/DocUploadBundleTests/ZipTests.swift

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@ import XCTest
1818

1919

2020
final class ZipTests: XCTestCase {
21+
22+
func test_unzip() async throws {
23+
// Test basic unzip behaviour we expect from the library we use
24+
try await withTempDir { tempDir in
25+
let tempURL = URL(fileURLWithPath: tempDir)
26+
let zipFile = fixtureUrl(for: "out.zip")
27+
let outDir = tempURL.appendingPathComponent("out")
28+
try Zipper.unzip(from: zipFile, to: outDir)
29+
XCTAssert(FileManager.default.fileExists(atPath: outDir.path))
30+
31+
// out/a.txt
32+
// out/subdir/b.txt
33+
let fileA = outDir.appendingPathComponent("a.txt")
34+
let fileB = outDir.appendingPathComponent("subdir").appendingPathComponent("b.txt")
35+
XCTAssert(FileManager.default.fileExists(atPath: fileA.path))
36+
XCTAssert(FileManager.default.fileExists(atPath: fileB.path))
37+
XCTAssertEqual(try String(contentsOf: fileA), "a")
38+
XCTAssertEqual(try String(contentsOf: fileB), "b")
39+
}
40+
}
2141

22-
func test_zip() async throws {
23-
// Test basic zip behaviour we expect from the library we use
42+
func test_zip_roundtrip() async throws {
43+
// Test basic zip roundtrip
2444
try await withTempDir { tempDir in
2545
// temp
2646
let tempURL = URL(fileURLWithPath: tempDir)
@@ -40,26 +60,20 @@ final class ZipTests: XCTestCase {
4060
let zipFile = tempURL.appendingPathComponent("out.zip")
4161
try Zipper.zip(paths: [fileA, subdir], to: zipFile)
4262
XCTAssert(FileManager.default.fileExists(atPath: zipFile.path))
43-
}
44-
}
4563

46-
func test_unzip() async throws {
47-
// Test basic unzip behaviour we expect from the library we use
48-
try await withTempDir { tempDir in
49-
let tempURL = URL(fileURLWithPath: tempDir)
50-
let zipFile = fixtureUrl(for: "out.zip")
51-
let outDir = tempURL.appendingPathComponent("out")
52-
try Zipper.unzip(from: zipFile, to: outDir)
53-
XCTAssert(FileManager.default.fileExists(atPath: outDir.path))
54-
55-
// out/a.txt
56-
// out/subdir/b.txt
57-
let fileA = outDir.appendingPathComponent("a.txt")
58-
let fileB = outDir.appendingPathComponent("subdir").appendingPathComponent("b.txt")
59-
XCTAssert(FileManager.default.fileExists(atPath: fileA.path))
60-
XCTAssert(FileManager.default.fileExists(atPath: fileB.path))
61-
XCTAssertEqual(try String(contentsOf: fileA), "a")
62-
XCTAssertEqual(try String(contentsOf: fileB), "b")
64+
do { // unzip what we zipped and check results
65+
let roundtrip = tempURL.appendingPathComponent("roundtrip")
66+
try Zipper.unzip(from: zipFile, to: roundtrip)
67+
XCTAssert(FileManager.default.fileExists(atPath: roundtrip.path))
68+
// roundtrip/a.txt
69+
// roundtrip/subdir/b.txt
70+
let fileA = roundtrip.appendingPathComponent("a.txt")
71+
let fileB = roundtrip.appendingPathComponent("subdir").appendingPathComponent("b.txt")
72+
XCTAssert(FileManager.default.fileExists(atPath: fileA.path))
73+
XCTAssert(FileManager.default.fileExists(atPath: fileB.path))
74+
XCTAssertEqual(try String(contentsOf: fileA), "a")
75+
XCTAssertEqual(try String(contentsOf: fileB), "b")
76+
}
6377
}
6478
}
6579

0 commit comments

Comments
 (0)