Skip to content

NSFileManager.moveItem - destination path should not exist before moving #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public class NSFileManager : NSObject {
}

public func moveItem(atPath srcPath: String, toPath dstPath: String) throws {
guard self.fileExists(atPath: dstPath) else {
guard !self.fileExists(atPath: dstPath) else {
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileWriteFileExistsError.rawValue, userInfo: [NSFilePathErrorKey : NSString(dstPath)])
}
if rename(srcPath, dstPath) != 0 {
Expand Down
27 changes: 25 additions & 2 deletions TestFoundation/TestNSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ class TestNSFileManager : XCTestCase {
return [
("test_createDirectory", test_createDirectory ),
("test_createFile", test_createFile ),
("test_moveFile", test_moveFile),
("test_fileSystemRepresentation", test_fileSystemRepresentation),
("test_fileAttributes", test_fileAttributes),
("test_setFileAttributes", test_setFileAttributes),
("test_directoryEnumerator", test_directoryEnumerator),
("test_pathEnumerator",test_pathEnumerator),
("test_contentsOfDirectoryAtPath", test_contentsOfDirectoryAtPath),
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath)
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath),
]
}

Expand Down Expand Up @@ -79,7 +80,29 @@ class TestNSFileManager : XCTestCase {
XCTFail("Failed to clean up file")
}
}


func test_moveFile() {
let fm = NSFileManager.defaultManager()
let path = "/tmp/testfile"
let path2 = "/tmp/testfile2"

func cleanup() {
ignoreError { try fm.removeItem(atPath: path) }
ignoreError { try fm.removeItem(atPath: path2) }
}

cleanup()

XCTAssertTrue(fm.createFile(atPath: path, contents: NSData(), attributes: nil))
defer { cleanup() }

do {
try fm.moveItem(atPath: path, toPath: path2)
} catch let error {
XCTFail("Failed to move file: \(error)")
}
}

func test_fileSystemRepresentation() {
let str = "☃"
let result = NSFileManager.defaultManager().fileSystemRepresentation(withPath: str)
Expand Down