diff --git a/Sources/Path+FileManager.swift b/Sources/Path+FileManager.swift index 6c89082..2202229 100644 --- a/Sources/Path+FileManager.swift +++ b/Sources/Path+FileManager.swift @@ -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 { @@ -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 } diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index db691e0..6d23262 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -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) + } + } + } } diff --git a/Tests/PathTests/XCTestManifests.swift b/Tests/PathTests/XCTestManifests.swift index 9104303..d92bebc 100644 --- a/Tests/PathTests/XCTestManifests.swift +++ b/Tests/PathTests/XCTestManifests.swift @@ -5,6 +5,7 @@ extension PathTests { ("testBasename", testBasename), ("testCodable", testCodable), ("testConcatenation", testConcatenation), + ("testCopyInto", testCopyInto), ("testEnumeration", testEnumeration), ("testEnumerationSkippingHiddenFiles", testEnumerationSkippingHiddenFiles), ("testExists", testExists),