Skip to content

Commit

Permalink
Merge pull request #24 from artemnovichkov/master
Browse files Browse the repository at this point in the history
Add copy function for item
  • Loading branch information
JohnSundell authored Sep 18, 2017
2 parents b2c38a8 + 42bd47b commit c2b337c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
55 changes: 55 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 @@ -480,6 +499,24 @@ public final class File: FileSystem.Item, FileSystemIterable {

try write(data: data)
}

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

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

/**
Expand Down Expand Up @@ -742,6 +779,24 @@ public final class Folder: FileSystem.Item, FileSystemIterable {
try makeFileSequence(includeHidden: includeHidden).forEach { try $0.delete() }
try makeSubfolderSequence(includeHidden: includeHidden).forEach { try $0.delete() }
}

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

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

/// Protocol adopted by file system types that may be iterated over (this protocol is an implementation detail)
Expand Down
27 changes: 27 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ class FilesTests: XCTestCase {
}
}

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

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 testCopyingFolders() {
performTest {
let copyingFolder = try folder.createSubfolder(named: "A")

let subfolder = try folder.createSubfolder(named: "folder")
try copyingFolder.copy(to: subfolder)
XCTAssertTrue(folder.containsSubfolder(named: "A"))
XCTAssertTrue(subfolder.containsSubfolder(named: "A"))
XCTAssertEqual(folder.subfolders.count, 2)
XCTAssertEqual(subfolder.subfolders.count, 1)
}
}

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

0 comments on commit c2b337c

Please sign in to comment.