From 42bd47b8d3c064c46fcc4819af40f4d3a47cfde2 Mon Sep 17 00:00:00 2001 From: Artem Novichkov Date: Sun, 10 Sep 2017 18:26:41 +0600 Subject: [PATCH] Update copying methods with return values and splitting for different types Related to Pull Request #24 --- Sources/Files.swift | 54 ++++++++++++++++++++----------- Tests/FilesTests/FilesTests.swift | 14 ++++++++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Sources/Files.swift b/Sources/Files.swift index 5664cf8..845fb9c 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -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 * @@ -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) + } + } } /** @@ -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) diff --git a/Tests/FilesTests/FilesTests.swift b/Tests/FilesTests/FilesTests.swift index 318afdd..fc746a4 100644 --- a/Tests/FilesTests/FilesTests.swift +++ b/Tests/FilesTests/FilesTests.swift @@ -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) @@ -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")