Skip to content

Commit

Permalink
Update copying methods with return values and splitting for different…
Browse files Browse the repository at this point in the history
… types

Related to Pull Request #24
  • Loading branch information
artemnovichkov committed Sep 10, 2017
1 parent cf1b97a commit 42bd47b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
54 changes: 36 additions & 18 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,6 @@ 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 Expand Up @@ -517,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 @@ -779,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
14 changes: 14 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ 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)
Expand All @@ -273,6 +274,19 @@ class FilesTests: XCTestCase {
}
}

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 42bd47b

Please sign in to comment.