From ad46f51d73650473d86a12d17edea38833387601 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Wed, 12 Oct 2022 22:38:03 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Use=20fake=20filesystem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 2 +- .../Testables/TestableConfigurations.swift | 12 +++++-- .../Common/Testables/TestableFileSystem.swift | 31 +++++++++++++------ phpmon/Domain/App/App.swift | 10 ++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 3258cb4..34ad757 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -989,8 +989,8 @@ isa = PBXGroup; children = ( C4F787A728EF812600790735 /* Testables */, - C4C8900128F0E27900CE5E97 /* Filesystem */, C4F787A628EF811000790735 /* Shell */, + C4C8900128F0E27900CE5E97 /* Filesystem */, C40C7F2127721F7300DDDCDC /* Core */, 54B20EDF263AA22C00D3250E /* PHP */, C44CCD4327AFE93300CE40E5 /* Errors */, diff --git a/phpmon/Common/Testables/TestableConfigurations.swift b/phpmon/Common/Testables/TestableConfigurations.swift index f5525d0..20457e2 100644 --- a/phpmon/Common/Testables/TestableConfigurations.swift +++ b/phpmon/Common/Testables/TestableConfigurations.swift @@ -38,7 +38,11 @@ class TestableConfigurations { return TestableConfiguration( architecture: "arm64", filesystem: [ - "/opt/homebrew/brew" + "/opt/homebrew/bin/brew" + : .fake(.binary), + "/opt/homebrew/bin/php" + : .fake(.binary), + "/opt/homebrew/bin/valet" : .fake(.binary), "/opt/homebrew/opt/php" : .fake(.symlink, "/opt/homebrew/Cellar/php/8.1.10_1"), @@ -47,13 +51,15 @@ class TestableConfigurations { "/opt/homebrew/Cellar/php/8.1.10_1/bin/php" : .fake(.binary), "/opt/homebrew/Cellar/php/8.1.10_1/bin/php-config" - : .fake(.binary) + : .fake(.binary), + "~/.config/valet" + : .fake(.directory) ], shellOutput: [ "sysctl -n sysctl.proc_translated" : .instant("0"), "id -un" - : .instant("nicoverbruggen"), + : .instant("user"), "which node" : .instant("/opt/homebrew/bin/node"), "php -v" diff --git a/phpmon/Common/Testables/TestableFileSystem.swift b/phpmon/Common/Testables/TestableFileSystem.swift index f814e59..f5b4964 100644 --- a/phpmon/Common/Testables/TestableFileSystem.swift +++ b/phpmon/Common/Testables/TestableFileSystem.swift @@ -16,28 +16,39 @@ class TestableFileSystem: FileSystemProtocol { var files: [String: FakeFile] func isExecutableFile(_ path: String) -> Bool { - // TODO - return false + guard let file = files[path] else { + return false + } + + return file.type == .binary } func exists(_ path: String) -> Bool { - // TODO - return false + return files.keys.contains(path) } func fileExists(_ path: String) -> Bool { - // TODO - return false + guard let file = files[path] else { + return false + } + + return [.binary, .symlink, .text].contains(file.type) } func directoryExists(_ path: String) -> Bool { - // TODO - return false + guard let file = files[path] else { + return false + } + + return [.directory].contains(file.type) } func fileIsSymlink(_ path: String) -> Bool { - // TODO - return false + guard let file = files[path] else { + return false + } + + return file.type == .symlink } } diff --git a/phpmon/Domain/App/App.swift b/phpmon/Domain/App/App.swift index 308b099..83dd659 100644 --- a/phpmon/Domain/App/App.swift +++ b/phpmon/Domain/App/App.swift @@ -31,7 +31,17 @@ class App { return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String } + /** + A fake architecture. + When set, the real machine's system architecture is not used, + but this fixed value is used instead. + */ + static var fakeArchitecture: String? + + /** The system architecture. Paths differ based on this value. */ static var architecture: String { + if fakeArchitecture != nil { return fakeArchitecture! } + var systeminfo = utsname() uname(&systeminfo) let machine = withUnsafeBytes(of: &systeminfo.machine) {bufPtr->String in