From ce44166b487f6721766d39a26fd7d70fc4f0464f Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 1 Nov 2022 17:02:26 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Added=20more=20tests,=20added=20to?= =?UTF-8?q?=20fake=20&=20real=20FS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Filesystem/RealFileSystem.swift | 10 ++--- .../Common/Testables/TestableFileSystem.swift | 43 ++++++++++++++++--- .../Filesystem/TestableFileSystemTest.swift | 24 ++++++++++- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/phpmon/Common/Filesystem/RealFileSystem.swift b/phpmon/Common/Filesystem/RealFileSystem.swift index e3ed7cf..7e8ef30 100644 --- a/phpmon/Common/Filesystem/RealFileSystem.swift +++ b/phpmon/Common/Filesystem/RealFileSystem.swift @@ -41,23 +41,21 @@ class RealFileSystem: FileSystemProtocol { } func getShallowContentsOfDirectory(_ path: String) throws -> [String] { - todo() - return [] + return try FileManager.default.contentsOfDirectory(atPath: path) } func getDestinationOfSymlink(_ path: String) throws -> String { - todo() - return "" + return try FileManager.default.destinationOfSymbolicLink(atPath: path) } // MARK: - Move & Delete Files func move(from path: String, to newPath: String) throws { - // TODO + try FileManager.default.moveItem(atPath: path, toPath: newPath) } func remove(_ path: String) throws { - // TODO + try FileManager.default.removeItem(atPath: path) } // MARK: — FS Attributes diff --git a/phpmon/Common/Testables/TestableFileSystem.swift b/phpmon/Common/Testables/TestableFileSystem.swift index ccddc7a..9492260 100644 --- a/phpmon/Common/Testables/TestableFileSystem.swift +++ b/phpmon/Common/Testables/TestableFileSystem.swift @@ -9,23 +9,41 @@ import Foundation class TestableFileSystem: FileSystemProtocol { + + /** + Initialize a fake filesystem with a bunch of files. + You do not need to specify directories (unless symlinks), those will be created automatically. + */ init(files: [String: FakeFile]) { self.files = files - for key in files.keys where key.contains("~") { + // Ensure that each of the ~ characters are replaced with the home directory path + for key in self.files.keys where key.contains("~") { self.files.renameKey( fromKey: key, toKey: key.replacingOccurrences(of: "~", with: self.homeDirectory) ) } + // Ensure that intermediate directories are created for file in self.files { self.createIntermediateDirectories(file.key) } } - var files: [String: FakeFile] + /** + Internal file handling of the fake filesystem. + You can easily dump what's in here by using: + ``` + let fs = FileSystem as! TestableFileSystem + fs.printContents() + ``` + */ + private(set) var files: [String: FakeFile] + /** + The home directory for the fake filesystem. + */ private(set) var homeDirectory = "/Users/fake" // MARK: - Basics @@ -104,12 +122,27 @@ class TestableFileSystem: FileSystemProtocol { let path = path.replacingTildeWithHomeDirectory let newPath = newPath.replacingTildeWithHomeDirectory - // TODO + self.files.keys.forEach { key in + if key.hasPrefix(path) { + self.files.renameKey( + fromKey: key, + toKey: key.replacingOccurrences(of: path, with: newPath) + ) + } + } + + self.files.renameKey(fromKey: path, toKey: newPath) } func remove(_ path: String) throws { - let path = path.replacingTildeWithHomeDirectory - // TODO + // Remove recursively + self.files.keys.forEach { key in + if key.hasPrefix(path) { + self.files.removeValue(forKey: key) + } + } + + self.files.removeValue(forKey: path) } // MARK: — Attributes diff --git a/tests/unit/Testables/Filesystem/TestableFileSystemTest.swift b/tests/unit/Testables/Filesystem/TestableFileSystemTest.swift index 4e2b8ec..e465e7d 100644 --- a/tests/unit/Testables/Filesystem/TestableFileSystemTest.swift +++ b/tests/unit/Testables/Filesystem/TestableFileSystemTest.swift @@ -89,5 +89,27 @@ class TestableFileSystemTest: XCTestCase { ) } - // TODO: Implement and test the remove() and move() methods and reorganize method order + func test_can_delete_directory_recursively() { + XCTAssertTrue(FileSystem.directoryExists("/home/user/documents")) + XCTAssertTrue(FileSystem.directoryExists("/home/user/documents/filters")) + XCTAssertTrue(FileSystem.fileExists("/home/user/documents/filters/filter1.txt")) + + try! FileSystem.remove("/home/user/documents") + + XCTAssertFalse(FileSystem.directoryExists("/home/user/documents")) + XCTAssertFalse(FileSystem.directoryExists("/home/user/documents/filters")) + XCTAssertFalse(FileSystem.fileExists("/home/user/documents/filters/filter1.txt")) + } + + func test_can_move_directory() { + XCTAssertTrue(FileSystem.directoryExists("/home/user/documents")) + XCTAssertTrue(FileSystem.directoryExists("/home/user/documents/filters")) + XCTAssertTrue(FileSystem.fileExists("/home/user/documents/filters/filter1.txt")) + + try! FileSystem.move(from: "/home/user/documents", to: "/home/user/new") + + XCTAssertTrue(FileSystem.directoryExists("/home/user/new")) + XCTAssertTrue(FileSystem.directoryExists("/home/user/new/filters")) + XCTAssertTrue(FileSystem.fileExists("/home/user/new/filters/filter1.txt")) + } }