diff --git a/Sources/Path+FileManager.swift b/Sources/Path+FileManager.swift index 5f3a428..2e3484a 100644 --- a/Sources/Path+FileManager.swift +++ b/Sources/Path+FileManager.swift @@ -174,6 +174,13 @@ public extension Path { } return self } + + @discardableResult + func rename(_ newname: String) throws -> Path { + let newpath = parent/newname + try FileManager.default.moveItem(atPath: string, toPath: newpath.string) + return newpath + } } /// Options for `Path.mkdir(_:)` diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 5224bb0..59784a7 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -165,12 +165,42 @@ class PathTests: XCTestCase { } func testCopyInto() throws { + try Path.mktemp { root1 in + let bar1 = try root1.join("bar").touch() + try Path.mktemp { root2 in + let bar2 = try root2.join("bar").touch() + XCTAssertThrowsError(try bar1.copy(into: root2)) + try bar1.copy(into: root2, overwrite: true) + XCTAssertTrue(bar1.exists) + XCTAssertTrue(bar2.exists) + } + } + } + + func testMoveInto() throws { + try Path.mktemp { root1 in + let bar1 = try root1.join("bar").touch() + try Path.mktemp { root2 in + let bar2 = try root2.join("bar").touch() + XCTAssertThrowsError(try bar1.move(into: root2)) + try bar1.move(into: root2, overwrite: true) + XCTAssertFalse(bar1.exists) + XCTAssertTrue(bar2.exists) + } + } + } + + func testRename() 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) + do { + let file = try root.bar.touch() + let foo = try file.rename("foo") + XCTAssertFalse(file.exists) + XCTAssertTrue(foo.isFile) + } + do { + let file = try root.bar.touch() + XCTAssertThrowsError(try file.rename("foo")) } } } diff --git a/Tests/PathTests/XCTestManifests.swift b/Tests/PathTests/XCTestManifests.swift index 06ae5f0..ccfec17 100644 --- a/Tests/PathTests/XCTestManifests.swift +++ b/Tests/PathTests/XCTestManifests.swift @@ -15,8 +15,10 @@ extension PathTests { ("testJoin", testJoin), ("testMkpathIfExists", testMkpathIfExists), ("testMktemp", testMktemp), + ("testMoveInto", testMoveInto), ("testRelativePathCodable", testRelativePathCodable), ("testRelativeTo", testRelativeTo), + ("testRename", testRename), ] }