Skip to content

Commit d83dadf

Browse files
committed
Fix paths by copying inputs into staging directory
1 parent 1ee1592 commit d83dadf

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

Tests/DocUploadBundleTests/TempDir.swift renamed to Sources/DocUploadBundle/TempDir.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ func withTempDir<T>(body: (String) async throws -> T) async throws -> T {
3232
let tmp = try TempDir()
3333
return try await body(tmp.path)
3434
}
35+
36+
func withTempDir<T>(body: (String) throws -> T) throws -> T {
37+
let tmp = try TempDir()
38+
return try body(tmp.path)
39+
}

Sources/DocUploadBundle/Zipper.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ public enum Zipper {
3434
throw Error.generic(reason: "\(error)")
3535
}
3636

37-
case let .zipTool(cwd):
37+
case .zipTool:
3838
do {
39-
let process = Process()
40-
process.executableURL = zip
41-
process.arguments = ["-q", "-r", outputPath.path] + inputPaths.map(\.lastPathComponent)
42-
process.currentDirectoryURL = cwd.map(URL.init(fileURLWithPath:))
43-
try process.run()
44-
process.waitUntilExit()
39+
try withTempDir { tempDir in
40+
let tempURL = URL(fileURLWithPath: tempDir)
41+
// Copy inputs to tempDir
42+
for source in inputPaths {
43+
let target = tempURL.appendingPathComponent(source.lastPathComponent)
44+
try FileManager.default.copyItem(at: source, to: target)
45+
}
46+
47+
// Run zip
48+
let process = Process()
49+
process.executableURL = zip
50+
process.arguments = ["-q", "-r", outputPath.path] + inputPaths.map(\.lastPathComponent)
51+
process.currentDirectoryURL = tempURL
52+
try process.run()
53+
process.waitUntilExit()
54+
}
4555
} catch {
4656
throw Error.generic(reason: "\(error)")
4757
}

Tests/DocUploadBundleTests/ZipTests.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class ZipTests: XCTestCase {
2121

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

4242
func test_zip_roundtrip() async throws {
4343
// Test basic zip roundtrip
44-
try await withTempDir { tempDir in
44+
try withTempDir { tempDir in
4545
// temp
4646
let tempURL = URL(fileURLWithPath: tempDir)
4747

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

9494
// Test basic zip roundtrip with the shellTool method
95-
try await withTempDir { tempDir in
95+
try withTempDir { tempDir in
9696
// temp
9797
let tempURL = URL(fileURLWithPath: tempDir)
9898

@@ -139,4 +139,41 @@ final class ZipTests: XCTestCase {
139139
}
140140
}
141141

142+
func test_zip_roundtrip_shellTool_relative_paths() async throws {
143+
try XCTSkipIf(!FileManager.default.fileExists(atPath: Zipper.zip.path))
144+
145+
// Test basic zip roundtrip with the shellTool method and relative paths
146+
try withTempDir { tempDir in
147+
// DocBundle components
148+
// metadataURL: tempDir/metadata.json
149+
// sourceURL: tempDir/.docs/owner/repo/ref
150+
// should be zipped as
151+
// - metadata.json
152+
// - ref
153+
// at the top level as relative paths.
154+
let tempURL = URL(fileURLWithPath: tempDir)
155+
let metadataURL = tempURL.appendingPathComponent("metadata.json")
156+
try "metadata".write(to: metadataURL, atomically: true, encoding: .utf8)
157+
let sourceURL = tempURL.appendingPathComponent("docs/owner/repo/ref")
158+
try FileManager.default.createDirectory(at: sourceURL, withIntermediateDirectories: true)
159+
let indexHTML = sourceURL.appendingPathComponent("index.html")
160+
try "index".write(to: indexHTML, atomically: true, encoding: .utf8)
161+
162+
// MUT
163+
let zipFile = tempURL.appendingPathComponent("out.zip")
164+
try Zipper.zip(paths: [metadataURL, sourceURL], to: zipFile, method: .zipTool(workingDirectory: tempDir))
165+
166+
do { // validate
167+
let unzipDir = tempURL.appendingPathComponent("unzip")
168+
try Zipper.unzip(from: zipFile, to: unzipDir)
169+
let metadataURL = unzipDir.appendingPathComponent("metadata.json")
170+
let indexHTML = unzipDir.appendingPathComponent("ref/index.html")
171+
XCTAssert(FileManager.default.fileExists(atPath: metadataURL.path))
172+
XCTAssert(FileManager.default.fileExists(atPath: indexHTML.path))
173+
XCTAssertEqual(try String(contentsOf: metadataURL), "metadata")
174+
XCTAssertEqual(try String(contentsOf: indexHTML), "index")
175+
}
176+
}
177+
}
178+
142179
}

0 commit comments

Comments
 (0)