From 564e47f66ef4fd16717546937514275ed2c3f214 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 2 Dec 2025 15:20:58 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20minimal=20Container?= =?UTF-8?q?=20boot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Container/Container+Real.swift | 4 ++-- phpmon/Container/Container.swift | 16 ++++++++++++---- tests/unit/Commands/CommandTest.swift | 2 +- tests/unit/Parsers/CaskFileParserTest.swift | 3 +-- tests/unit/Parsers/HomebrewPackageTest.swift | 4 ++-- tests/unit/Parsers/NginxConfigurationTest.swift | 2 +- .../unit/Parsers/PhpConfigurationFileTest.swift | 3 +-- tests/unit/Parsers/PhpExtensionTest.swift | 14 +++++++------- .../Filesystem/RealFileSystemTest.swift | 7 ++----- tests/unit/Testables/Shell/RealShellTest.swift | 3 +-- .../unit/Testables/Shell/TestableShellTest.swift | 1 - .../Testables/TestableConfigurationTest.swift | 2 +- tests/unit/Testables/WebApi/RealWebApiTest.swift | 3 +-- tests/unit/Watchers/FSNotifierTest.swift | 1 - 14 files changed, 32 insertions(+), 33 deletions(-) diff --git a/phpmon/Container/Container+Real.swift b/phpmon/Container/Container+Real.swift index ded175d2..cb435d0e 100644 --- a/phpmon/Container/Container+Real.swift +++ b/phpmon/Container/Container+Real.swift @@ -7,9 +7,9 @@ // extension Container { - public static func real() -> Container { + public static func real(minimal: Bool = false) -> Container { let container = Container() - container.bind() + container.bind(coreOnly: minimal) return container } } diff --git a/phpmon/Container/Container.swift b/phpmon/Container/Container.swift index 9f20e40b..f9892739 100644 --- a/phpmon/Container/Container.swift +++ b/phpmon/Container/Container.swift @@ -51,11 +51,18 @@ class Container { /// (Swapping instances for specific dependencies can be introduced later with dedicated /// methods if it ever becomes truly necessary.) /// - public func bind() { + /// - Parameter coreOnly: Only binds `shell`, `filesystem`, `command`, `paths` and `webApi`. + /// Use this to prevent slowing down tests for a minimal container. + /// + public func bind(coreOnly: Bool = false) { if self.bound { fatalError("You cannot call `bind` on a Container more than once.") } + defer { + self.bound = true + } + // These are the most basic building blocks. We need these before // any of the other classes can be initialized! self.shell = RealShell(container: self) @@ -64,6 +71,10 @@ class Container { self.paths = Paths(container: self) self.webApi = RealWebApi(container: self) + if coreOnly { + return + } + // Please note that the order in which these are initialized, matters! // For example, preferences leverages the Paths instance, so don't just // swap these around for no reason... the order is very intentional. @@ -71,9 +82,6 @@ class Container { self.phpEnvs = PhpEnvironments(container: self) self.favorites = Favorites() self.warningManager = WarningManager(container: self) - - // At this point, our container has been bound. - self.bound = true } /** diff --git a/tests/unit/Commands/CommandTest.swift b/tests/unit/Commands/CommandTest.swift index ee1ccb86..b3f26ae3 100644 --- a/tests/unit/Commands/CommandTest.swift +++ b/tests/unit/Commands/CommandTest.swift @@ -10,7 +10,7 @@ import Testing struct CommandTest { @Test func determinePhpVersion() { - let container = Container.real() + let container = Container.real(minimal: true) let version = container.command.execute( path: container.paths.php, diff --git a/tests/unit/Parsers/CaskFileParserTest.swift b/tests/unit/Parsers/CaskFileParserTest.swift index c60e34f0..9661214e 100644 --- a/tests/unit/Parsers/CaskFileParserTest.swift +++ b/tests/unit/Parsers/CaskFileParserTest.swift @@ -9,12 +9,11 @@ import Testing import Foundation -@Suite(.serialized) struct CaskFileParserTest { var container: Container init() async throws { - container = Container.real() + container = Container.real(minimal: true) } var Shell: ShellProtocol { diff --git a/tests/unit/Parsers/HomebrewPackageTest.swift b/tests/unit/Parsers/HomebrewPackageTest.swift index db6d8f27..f002d5c3 100644 --- a/tests/unit/Parsers/HomebrewPackageTest.swift +++ b/tests/unit/Parsers/HomebrewPackageTest.swift @@ -53,7 +53,7 @@ struct HomebrewPackageTest { /// or the JSON API of the Homebrew output may have changed. @Test(.disabled("Uses system command; enable at your own risk")) func can_parse_services_json_from_cli_output() async throws { - let container = Container.real() + let container = Container.real(minimal: true) let services = try! JSONDecoder().decode( [HomebrewService].self, @@ -76,7 +76,7 @@ struct HomebrewPackageTest { /// or the JSON API of the Homebrew output may have changed. @Test(.disabled("Uses system command; enable at your own risk")) func can_load_extension_json_from_cli_output() async throws { - let container = Container.real() + let container = Container.real(minimal: true) let package = try! JSONDecoder().decode( [HomebrewPackage].self, diff --git a/tests/unit/Parsers/NginxConfigurationTest.swift b/tests/unit/Parsers/NginxConfigurationTest.swift index 04e25332..ccb5f744 100644 --- a/tests/unit/Parsers/NginxConfigurationTest.swift +++ b/tests/unit/Parsers/NginxConfigurationTest.swift @@ -13,7 +13,7 @@ struct NginxConfigurationTest { var container: Container init () async throws { - container = Container.real() + container = Container.real(minimal: true) } // MARK: - Test Files diff --git a/tests/unit/Parsers/PhpConfigurationFileTest.swift b/tests/unit/Parsers/PhpConfigurationFileTest.swift index fd7665e4..aca08551 100644 --- a/tests/unit/Parsers/PhpConfigurationFileTest.swift +++ b/tests/unit/Parsers/PhpConfigurationFileTest.swift @@ -9,12 +9,11 @@ import Testing import Foundation -@Suite(.serialized) class PhpConfigurationFileTest { var container: Container init() { - self.container = Container.real() + self.container = Container.real(minimal: true) } static var phpIniFileUrl: URL { diff --git a/tests/unit/Parsers/PhpExtensionTest.swift b/tests/unit/Parsers/PhpExtensionTest.swift index f2948fdb..2b348e9b 100644 --- a/tests/unit/Parsers/PhpExtensionTest.swift +++ b/tests/unit/Parsers/PhpExtensionTest.swift @@ -9,25 +9,22 @@ import Testing import Foundation -@Suite(.serialized) struct PhpExtensionTest { - var container: Container - - init () async throws { - container = Container.real() - } - static var phpIniFileUrl: URL { TestBundle.url(forResource: "php", withExtension: "ini")! } @Test func can_load_extension() throws { + let container = Container.real(minimal: true) + let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path) #expect(!extensions.isEmpty) } @Test func extension_name_is_correct() throws { + let container = Container.real(minimal: true) + let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path) let extensionNames = extensions.map { (ext) -> String in @@ -47,6 +44,8 @@ struct PhpExtensionTest { } @Test func extension_status_is_correct() throws { + let container = Container.real(minimal: true) + let extensions = PhpExtension.from(container, filePath: Self.phpIniFileUrl.path) // xdebug should be enabled @@ -57,6 +56,7 @@ struct PhpExtensionTest { } @Test func toggle_works_as_expected() async throws { + let container = Container.real(minimal: true) let destination = Utility.copyToTemporaryFile(resourceName: "php", fileExtension: "ini")! let extensions = PhpExtension.from(container, filePath: destination.path) #expect(extensions.count == 6) diff --git a/tests/unit/Testables/Filesystem/RealFileSystemTest.swift b/tests/unit/Testables/Filesystem/RealFileSystemTest.swift index 17434791..783c36d7 100644 --- a/tests/unit/Testables/Filesystem/RealFileSystemTest.swift +++ b/tests/unit/Testables/Filesystem/RealFileSystemTest.swift @@ -9,20 +9,17 @@ import Testing import Foundation -@Suite(.serialized) // serialized due to how unique temp directory works struct RealFileSystemTest { var filesystem: FileSystemProtocol init() throws { - let container = Container() - container.bind() - + let container = Container.real(minimal: true) filesystem = container.filesystem } private func createUniqueTemporaryDirectory() -> String { let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true) - let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests").path + let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests-\(UUID().uuidString)").path try? FileManager.default.removeItem(atPath: fullTempDirectoryPath) try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false) return fullTempDirectoryPath diff --git a/tests/unit/Testables/Shell/RealShellTest.swift b/tests/unit/Testables/Shell/RealShellTest.swift index 88450bd3..36ee551a 100644 --- a/tests/unit/Testables/Shell/RealShellTest.swift +++ b/tests/unit/Testables/Shell/RealShellTest.swift @@ -9,13 +9,12 @@ import Testing import Foundation -@Suite(.serialized) struct RealShellTest { var container: Container init() async throws { // Reset to the default shell - container = Container.real() + container = Container.real(minimal: true) } @Test func system_shell_is_default() async { diff --git a/tests/unit/Testables/Shell/TestableShellTest.swift b/tests/unit/Testables/Shell/TestableShellTest.swift index ef9c75a7..036d9415 100644 --- a/tests/unit/Testables/Shell/TestableShellTest.swift +++ b/tests/unit/Testables/Shell/TestableShellTest.swift @@ -9,7 +9,6 @@ import Testing import Foundation -@Suite(.serialized) struct TestableShellTest { @Test func fake_shell_output_can_be_declared() async { let greeting = BatchFakeShellOutput(items: [ diff --git a/tests/unit/Testables/TestableConfigurationTest.swift b/tests/unit/Testables/TestableConfigurationTest.swift index 659d6311..595e5eef 100644 --- a/tests/unit/Testables/TestableConfigurationTest.swift +++ b/tests/unit/Testables/TestableConfigurationTest.swift @@ -11,7 +11,7 @@ import Foundation struct TestableConfigurationTest { @Test func configuration_can_be_saved_as_json() async { - let container = Container.real() + let container = Container.real(minimal: true) // WORKING var configuration = TestableConfigurations.working diff --git a/tests/unit/Testables/WebApi/RealWebApiTest.swift b/tests/unit/Testables/WebApi/RealWebApiTest.swift index 0a0995a3..1c4c848b 100644 --- a/tests/unit/Testables/WebApi/RealWebApiTest.swift +++ b/tests/unit/Testables/WebApi/RealWebApiTest.swift @@ -13,8 +13,7 @@ struct RealWebApiTest { private var container: Container init() throws { - self.container = Container() - container.bind() + self.container = Container.real(minimal: true) } var WebApi: RealWebApi { diff --git a/tests/unit/Watchers/FSNotifierTest.swift b/tests/unit/Watchers/FSNotifierTest.swift index fd59c3e5..a9ca987f 100644 --- a/tests/unit/Watchers/FSNotifierTest.swift +++ b/tests/unit/Watchers/FSNotifierTest.swift @@ -9,7 +9,6 @@ import Testing import Foundation -@Suite(.serialized) struct FSNotifierTest { @Test func notifier_fires_when_file_is_modified() async throws {