mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 03:50:08 +02:00
✅ Added more tests, added to fake & real FS
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
|
@ -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"))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user