From 0b33116eb0c3a243ff916b21cafe60a9bccc7375 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 4 Oct 2022 19:39:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Fake=20shell=20in=20use?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 6 ++++++ phpmon/Domain/App/AppDelegate.swift | 2 ++ phpmon/Domain/App/Startup.swift | 14 ++++++------- phpmon/Next/TestableShell.swift | 8 ++++++++ phpmon/Next/Testables.swift | 29 +++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 phpmon/Next/Testables.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 4ad3a28..d9c5848 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -59,6 +59,8 @@ C40C7F2927721FF600DDDCDC /* ActivePhpInstallation+Checks.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F2727721FF600DDDCDC /* ActivePhpInstallation+Checks.swift */; }; C40C7F3027722E8D00DDDCDC /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F2F27722E8D00DDDCDC /* Logger.swift */; }; C40C7F3127722E8D00DDDCDC /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F2F27722E8D00DDDCDC /* Logger.swift */; }; + C40F505528ECA64E004AD45B /* Testables.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40F505428ECA64E004AD45B /* Testables.swift */; }; + C40F505628ECA64E004AD45B /* Testables.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40F505428ECA64E004AD45B /* Testables.swift */; }; C40FE737282ABA4F00A302C2 /* AppVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40FE736282ABA4F00A302C2 /* AppVersion.swift */; }; C40FE738282ABA4F00A302C2 /* AppVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40FE736282ABA4F00A302C2 /* AppVersion.swift */; }; C40FE73B282ABB2E00A302C2 /* AppVersionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40FE739282ABB2E00A302C2 /* AppVersionTest.swift */; }; @@ -364,6 +366,7 @@ C40C7F1D2772136000DDDCDC /* PhpEnv.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpEnv.swift; sourceTree = ""; }; C40C7F2727721FF600DDDCDC /* ActivePhpInstallation+Checks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ActivePhpInstallation+Checks.swift"; sourceTree = ""; }; C40C7F2F27722E8D00DDDCDC /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; + C40F505428ECA64E004AD45B /* Testables.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Testables.swift; sourceTree = ""; }; C40FE736282ABA4F00A302C2 /* AppVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersion.swift; sourceTree = ""; }; C40FE739282ABB2E00A302C2 /* AppVersionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionTest.swift; sourceTree = ""; }; C412E5FB25700D5300A1FB67 /* HomebrewPackage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomebrewPackage.swift; sourceTree = ""; }; @@ -883,6 +886,7 @@ C46EBC4628DB9644007ACC74 /* SystemShell.swift */, C46EBC4928DB966A007ACC74 /* TestableShell.swift */, C46EBC4328DB95F0007ACC74 /* Shellable.swift */, + C40F505428ECA64E004AD45B /* Testables.swift */, ); path = Next; sourceTree = ""; @@ -1488,6 +1492,7 @@ C4C3ED412783497000AB15D8 /* MainMenu+Startup.swift in Sources */, C40508AF28ADA23D008FAC1F /* NoDomainResultsView.swift in Sources */, C4D89BC62783C99400A02B68 /* ComposerJson.swift in Sources */, + C40F505528ECA64E004AD45B /* Testables.swift in Sources */, C46FA23F246C358E00944F05 /* StringExtension.swift in Sources */, C42E3BF428A9BF5100AFECFC /* LegacyShell+PATH.swift in Sources */, C42337A3281F19F000459A48 /* Xdebug.swift in Sources */, @@ -1615,6 +1620,7 @@ C44C198E276E3A1C0072762D /* TerminalProgressWindowController.swift in Sources */, C485707828BF456300539B36 /* Warning.swift in Sources */, C415938027A1B54F00D2E1B7 /* PhpFrameworks.swift in Sources */, + C40F505628ECA64E004AD45B /* Testables.swift in Sources */, C4D9ADC9277611A0007277F4 /* InternalSwitcher.swift in Sources */, C449B4F227EE7FC400C47E8A /* DomainListPhpCell.swift in Sources */, C42CFB1A27DFE8BD00862737 /* NginxConfigurationTest.swift in Sources */, diff --git a/phpmon/Domain/App/AppDelegate.swift b/phpmon/Domain/App/AppDelegate.swift index f817de0..14fa164 100644 --- a/phpmon/Domain/App/AppDelegate.swift +++ b/phpmon/Domain/App/AppDelegate.swift @@ -66,6 +66,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele logger.verbosity = .info #if DEBUG logger.verbosity = .performance + // TODO: Enable to fake broken setup during testing + ActiveShell.useTestable(Testables.broken) #endif if CommandLine.arguments.contains("--v") { logger.verbosity = .performance diff --git a/phpmon/Domain/App/Startup.swift b/phpmon/Domain/App/Startup.swift index 0f49448..82e306c 100644 --- a/phpmon/Domain/App/Startup.swift +++ b/phpmon/Domain/App/Startup.swift @@ -115,7 +115,7 @@ class Startup { // Make sure we can detect one or more PHP installations. // ================================================================================= EnvironmentCheck( - command: { return !LegacyShell.pipe("ls \(Paths.optPath) | grep php").contains("php") }, + command: { return await !Shell.pipe("ls \(Paths.optPath) | grep php").out.contains("php") }, name: "`ls \(Paths.optPath) | grep php` returned php result", titleText: "startup.errors.php_opt.title".localized, subtitleText: "startup.errors.php_opt.subtitle".localized( @@ -143,14 +143,14 @@ class Startup { // functioning correctly. Let the user know that they need to run `valet trust`. // ================================================================================= EnvironmentCheck( - command: { return !LegacyShell.pipe("cat /private/etc/sudoers.d/brew").contains(Paths.brew) }, + command: { return await !Shell.pipe("cat /private/etc/sudoers.d/brew").out.contains(Paths.brew) }, name: "`/private/etc/sudoers.d/brew` contains brew", titleText: "startup.errors.sudoers_brew.title".localized, subtitleText: "startup.errors.sudoers_brew.subtitle".localized, descriptionText: "startup.errors.sudoers_brew.desc".localized ), EnvironmentCheck( - command: { return !LegacyShell.pipe("cat /private/etc/sudoers.d/valet").contains(Paths.valet) }, + command: { return await !Shell.pipe("cat /private/etc/sudoers.d/valet").out.contains(Paths.valet) }, name: "`/private/etc/sudoers.d/valet` contains valet", titleText: "startup.errors.sudoers_valet.title".localized, subtitleText: "startup.errors.sudoers_valet.subtitle".localized, @@ -200,10 +200,10 @@ class Startup { // ================================================================================= EnvironmentCheck( command: { + let nodePath = await Shell.pipe("which node").out return App.architecture == "x86_64" && FileManager.default.fileExists(atPath: "/usr/local/bin/which") - && LegacyShell.pipe("which node", requiresPath: false) - .contains("env: node: No such file or directory") + && nodePath.contains("env: node: No such file or directory") }, name: "`env: node` issue does not apply", titleText: "startup.errors.which_alias_issue.title".localized, @@ -215,7 +215,7 @@ class Startup { // ================================================================================= EnvironmentCheck( command: { - return valet("--version", sudo: false) + return await Shell.pipe("valet --version").out .contains("Composer detected issues in your platform") }, name: "`no global composer issues", @@ -228,7 +228,7 @@ class Startup { // ================================================================================= EnvironmentCheck( command: { - let output = valet("--version", sudo: false) + let output = await Shell.pipe("valet --version").out // Failure condition #1: does not contain Laravel Valet if !output.contains("Laravel Valet") { return true diff --git a/phpmon/Next/TestableShell.swift b/phpmon/Next/TestableShell.swift index eaad914..d934a1a 100644 --- a/phpmon/Next/TestableShell.swift +++ b/phpmon/Next/TestableShell.swift @@ -62,6 +62,14 @@ struct FakeShellOutput { struct BatchFakeShellOutput { var items: [FakeShellOutput] + static func with(_ items: [FakeShellOutput]) -> BatchFakeShellOutput { + return BatchFakeShellOutput(items: items) + } + + static func instant(_ output: String, _ stream: ShellStream = .stdOut) -> BatchFakeShellOutput { + return BatchFakeShellOutput(items: [.instant(output, stream)]) + } + /** Outputs the fake shell output as expected. */ diff --git a/phpmon/Next/Testables.swift b/phpmon/Next/Testables.swift new file mode 100644 index 0000000..f53d357 --- /dev/null +++ b/phpmon/Next/Testables.swift @@ -0,0 +1,29 @@ +// +// Testables.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 04/10/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation + +// swiftlint:disable colon trailing_comma +class Testables { + + typealias Configuration = [String: BatchFakeShellOutput] + + // TODO: Complete broken configuration setup + static var broken: Configuration { + return [ + "php -v" : .instant(""), + "ls /opt/homebrew/opt | grep php" : .instant(""), + ] + } + + // TODO: All expected, correct Terminal responses + static var working: Configuration { + return [:] + } + +}