Skip to content

Commit

Permalink
Add copy function for item
Browse files Browse the repository at this point in the history
Related to #22
  • Loading branch information
artemnovichkov committed Sep 9, 2017
1 parent b2c38a8 commit cf1b97a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public class FileSystem {
case renameFailed(Item)
/// Thrown when a file or folder couldn't be moved (contains the item)
case moveFailed(Item)
/// Thrown when a file or folder couldn't be copied (contains the item)
case copyFailed(Item)
/// Thrown when a file or folder couldn't be deleted (contains the item)
case deleteFailed(Item)

Expand All @@ -101,6 +103,8 @@ public class FileSystem {
return itemA == itemB
case .moveFailed(_):
return false
case .copyFailed(_):
return false
case .deleteFailed(_):
return false
}
Expand All @@ -110,6 +114,19 @@ public class FileSystem {
return false
case .moveFailed(let itemB):
return itemA == itemB
case .copyFailed(_):
return false
case .deleteFailed(_):
return false
}
case .copyFailed(let itemA):
switch rhs {
case .renameFailed(_):
return false
case .moveFailed(_):
return false
case .copyFailed(let itemB):
return itemA == itemB
case .deleteFailed(_):
return false
}
Expand All @@ -119,6 +136,8 @@ public class FileSystem {
return false
case .moveFailed(_):
return false
case .copyFailed(_):
return false
case .deleteFailed(let itemB):
return itemA == itemB
}
Expand Down Expand Up @@ -264,6 +283,24 @@ public class FileSystem {
}
}

/**
* Copy this item to a new folder
*
* - parameter folder: The folder that the item should be copy to
*
* - throws: `FileSystem.Item.OperationError.copyFailed` if the item couldn't be copied
*/
public func copy(to folder: Folder) throws {
let newPath = folder.path + name

do {
try fileManager.copyItem(atPath: path, toPath: newPath)
path = newPath
} catch {
throw OperationError.copyFailed(self)
}
}

/**
* Delete the item from disk
*
Expand Down
13 changes: 13 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ class FilesTests: XCTestCase {
}
}

func testCopyingFiles() {
performTest {
let file = try folder.createFile(named: "A")

let subfolder = try folder.createSubfolder(named: "folder")
try file.copy(to: subfolder)
try XCTAssertNotNil(folder.file(named: "A"))
try XCTAssertNotNil(subfolder.file(named: "A"))
try XCTAssertEqual(file.read(), subfolder.file(named: "A").read())
XCTAssertEqual(folder.files.count, 1)
}
}

func testEnumeratingFiles() {
performTest {
try folder.createFile(named: "1")
Expand Down

0 comments on commit cf1b97a

Please sign in to comment.