Refactor rename -> rename(to:)

This commit is contained in:
Max Howell
2019-01-31 10:15:34 -05:00
parent 889d825b3a
commit 49ef073e34
3 changed files with 14 additions and 7 deletions

View File

@@ -175,8 +175,16 @@ public extension Path {
return self
}
/**
Renames the file at path.
Path.root.foo.bar.rename(to: "baz") // => /foo/baz
- Parameter to: the new basename for the file
- Returns: The renamed path.
*/
@discardableResult
func rename(_ newname: String) throws -> Path {
func rename(to newname: String) throws -> Path {
let newpath = parent/newname
try FileManager.default.moveItem(atPath: string, toPath: newpath.string)
return newpath

View File

@@ -9,7 +9,7 @@ import Foundation
`Path` supports `Codable`, and can be configured to
[encode paths *relatively*](https://github.com/mxcl/Path.swift/#codable).
Sorting a `Sequence` of `Path`s will return the locale-aware sort order, which
Sorting a `Sequence` of paths will return the locale-aware sort order, which
will give you the same order as Finder.
Converting from a `String` is a common first step, here are the recommended
@@ -20,13 +20,12 @@ import Foundation
let p3 = Path.cwd/relativePathString
let p4 = Path(userInput) ?? Path.cwd/userInput
If you are constructing Paths from static-strings we provide support for
If you are constructing paths from static-strings we provide support for
dynamic members:
let p1 = Path.root.usr.bin.ls // => /usr/bin/ls
- Note: There may not be an actual filesystem entry at the path. The underlying
representation for `Path` is `String`.
- Note: A `Path` does not necessarily represent an actual filesystem entry.
*/
@dynamicMemberLookup

View File

@@ -194,13 +194,13 @@ class PathTests: XCTestCase {
try Path.mktemp { root in
do {
let file = try root.bar.touch()
let foo = try file.rename("foo")
let foo = try file.rename(to: "foo")
XCTAssertFalse(file.exists)
XCTAssertTrue(foo.isFile)
}
do {
let file = try root.bar.touch()
XCTAssertThrowsError(try file.rename("foo"))
XCTAssertThrowsError(try file.rename(to: "foo"))
}
}
}