Skip to content

Commit

Permalink
Fix copy(into:) overwrite mode
Browse files Browse the repository at this point in the history
Seems like Linux Foundation has a bug

I checked, seems fixed in Swift 5. But added a swift version check se we can verify and if not report the bug.
  • Loading branch information
mxcl committed Jan 22, 2019
1 parent 43d3e0a commit 58d026c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Sources/Path+FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public extension Path {
- Parameter to: Destination filename.
- Parameter overwrite: If true overwrites any file that already exists at `to`.
- Returns: `to` to allow chaining
- SeeAlso: copy(into:overwrite:)
- SeeAlso: `copy(into:overwrite:)`
*/
@discardableResult
public func copy(to: Path, overwrite: Bool = false) throws -> Path {
Expand All @@ -30,16 +30,26 @@ public extension Path {
- Parameter into: Destination directory
- Parameter overwrite: If true overwrites any file that already exists at `into`.
- Returns: The `Path` of the newly copied file.
- SeeAlso: copy(into:overwrite:)
- SeeAlso: `copy(into:overwrite:)`
*/
@discardableResult
public func copy(into: Path, overwrite: Bool = false) throws -> Path {
if !into.exists {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
} else if overwrite, !into.isDirectory {
try into.delete()
}
let rv = into/basename()
if overwrite, rv.isFile {
try rv.delete()
}
#if os(Linux)
#if swift(>=5)
// check if fixed
#else
if !overwrite, rv.isFile {
throw CocoaError.error(.fileWriteFileExists)
}
#endif
#endif
try FileManager.default.copyItem(at: url, to: rv.url)
return rv
}
Expand Down
11 changes: 11 additions & 0 deletions Tests/PathTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,15 @@ class PathTests: XCTestCase {
XCTAssertEqual(Path.root/"a/foo"/"../../bar", Path.root/"bar")
XCTAssertEqual(Path.root/"a/foo"/"../../../bar", Path.root/"bar")
}

func testCopyInto() throws {
try Path.mktemp { root in
let bar = try root.join("bar").touch()
try Path.mktemp { root in
try root.join("bar").touch()
XCTAssertThrowsError(try bar.copy(into: root))
try bar.copy(into: root, overwrite: true)
}
}
}
}
1 change: 1 addition & 0 deletions Tests/PathTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extension PathTests {
("testBasename", testBasename),
("testCodable", testCodable),
("testConcatenation", testConcatenation),
("testCopyInto", testCopyInto),
("testEnumeration", testEnumeration),
("testEnumerationSkippingHiddenFiles", testEnumerationSkippingHiddenFiles),
("testExists", testExists),
Expand Down

0 comments on commit 58d026c

Please sign in to comment.