From 4de018826e9dce90fd5cb78d468641478327ec99 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 6 Aug 2026 01:50:34 +0900 Subject: feat(plugin): run plugin-provided installers through the RPC worker A plugin can now hand an InstallerInterface implementation to InstallationManager::addInstaller across the wire, and a legacy composer-installer package is loaded as one; both are backed by a PhpInstallerProxy forwarding the whole installer contract to the entity in the PHP worker. An installer returning a real promise is an explicit error until promises can cross the boundary. InstallationManager takes installers as shared handles instead of boxes, so the object identity removeInstaller and PluginManager's registeredPlugins compare against survives registration, and holds them in a RefCell: Installer::run keeps a shared borrow of the manager for the whole run, and a plugin activated inside it registers its installer from there. The type cache keys on the installer itself, like upstream, so re-entrant registration cannot leave a stale index behind. InstallerInterface::supports is fallible for the same reason getCapabilities and getCommands are: it answers over RPC. Cloning a proxy stub clones the Rust-side entity and rebinds the copy to the fresh handle. Previously only the classes declaring __clone got a throwing body, and the rest let two stubs share (and twice release) one handle. Package entities answer with AnyPackage::dup, which already carries BasePackage::__clone and the RootAliasPackage override; the others are an explicit error. The package proxy covers the whole PackageInterface surface; only the link maps and the release date still lack a wire image for their value objects. PluginManager gains a test-only seam for the reported Plugin API version, and the three PluginInstallerTest cases that need it are ported. Co-Authored-By: Claude Opus 5 (1M context) --- .../shirabe/tests/plugin/plugin_installer_test.rs | 122 ++++++++++++++++++--- 1 file changed, 104 insertions(+), 18 deletions(-) (limited to 'crates/shirabe/tests/plugin/plugin_installer_test.rs') diff --git a/crates/shirabe/tests/plugin/plugin_installer_test.rs b/crates/shirabe/tests/plugin/plugin_installer_test.rs index 35088c8d..06cd2ce9 100644 --- a/crates/shirabe/tests/plugin/plugin_installer_test.rs +++ b/crates/shirabe/tests/plugin/plugin_installer_test.rs @@ -16,7 +16,9 @@ use shirabe::io::IOInterface; use shirabe::io::buffer_io::BufferIO; use shirabe::json::JsonFile; use shirabe::package::loader::{ArrayLoader, JsonLoader, JsonLoaderInput}; -use shirabe::package::{Locker, LockerInterface, PackageInterfaceHandle, RootPackageHandle}; +use shirabe::package::{ + CompletePackageHandle, Locker, LockerInterface, PackageInterfaceHandle, RootPackageHandle, +}; use shirabe::plugin::plugin_interface::PluginInterface; use shirabe::plugin::{Capable, PluginManager, composer_handle_value, io_handle_value}; use shirabe::repository::{ @@ -30,6 +32,7 @@ use shirabe::util::process_executor::ProcessExecutor; use shirabe_external_packages::symfony::console::output::output_interface::VERBOSITY_NORMAL; use shirabe_external_packages::symfony::process::PhpExecutableFinder; use shirabe_php_shim::PhpMixed; +use shirabe_semver::VersionParser; use tempfile::TempDir; /// The register/activate flow runs the plugin in the real PHP worker; without a PHP binary the @@ -166,9 +169,9 @@ impl RepositoryManagerInterface for MockRepositoryManager { struct MockInstallationManager; impl InstallationManagerInterface for MockInstallationManager { - fn add_installer(&mut self, _installer: Box) {} + fn add_installer(&self, _installer: std::rc::Rc) {} - fn remove_installer(&mut self, _installer: &dyn InstallerInterface) {} + fn remove_installer(&self, _installer: &dyn InstallerInterface) {} fn disable_plugins(&mut self) {} @@ -606,31 +609,114 @@ fn test_register_plugin_only_one_time() { assert_eq!("activate v1\n", set_up.io.borrow().get_output()); } -// PluginManager::get_plugin_api_version returns a hardcoded constant -// (plugin_interface::PLUGIN_API_VERSION) with no seam to override it per-test the way PHP's -// `getMockBuilder(PluginManager::class)->onlyMethods(['getPluginApiVersion'])` does. -#[ignore = "Requires mocking getPluginApiVersion; PluginManager has no such seam (TODO(plugin))"] +/// PHP `setPluginApiVersionWithPlugins`: swaps in a plugin manager reporting +/// `new_plugin_api_version` (PHP mocks `getPluginApiVersion`; the Rust seam is +/// `__set_plugin_api_version`) and a local repository holding the internal composer-plugin-api +/// package plus `plugins` (PHP mocks `getPackages`), then loads the installed plugins. +fn set_plugin_api_version_with_plugins( + set_up: &SetUp, + new_plugin_api_version: &str, + plugins: Vec, +) -> std::rc::Rc> { + let pm = std::rc::Rc::new(std::cell::RefCell::new(PluginManager::new( + set_up.io_dyn.clone(), + set_up.composer.downgrade(), + None, + DisablePlugins::None, + ))); + pm.borrow_mut() + .__set_plugin_api_version(new_plugin_api_version); + set_up.composer.borrow_mut().set_plugin_manager(pm.clone()); + + let plug_api_internal_package: PackageInterfaceHandle = CompletePackageHandle::new( + "composer-plugin-api".to_string(), + VersionParser + .normalize(new_plugin_api_version, None) + .unwrap(), + new_plugin_api_version.to_string(), + ) + .into(); + let repository = + InstalledRepositoryInterfaceHandle::new(InstalledArrayRepository::new().unwrap()); + repository + .borrow_mut() + .add_package(plug_api_internal_package) + .unwrap(); + for plugin in plugins { + repository.borrow_mut().add_package(plugin).unwrap(); + } + set_up + .composer + .borrow() + .get_repository_manager() + .borrow_mut() + .set_local_repository(repository.as_repository_handle()); + + pm.borrow_mut().load_installed_plugins().unwrap(); + pm +} + #[test] fn test_star_plugin_version_works_with_any_api_version() { - // TODO(phase-d): requires mocking getPluginApiVersion; PluginManager has no such seam - // (TODO(plugin)). - todo!() + if !php_runtime_available() { + return; + } + let _worker = lock_php_worker(); + let set_up = set_up(); + let star_version_plugin = || vec![PackageInterfaceHandle::dup(&set_up.packages[4])]; + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.0.0", star_version_plugin()); + assert_eq!(1, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.9.9", star_version_plugin()); + assert_eq!(1, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "2.0.0-dev", star_version_plugin()); + assert_eq!(1, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "100.0.0-stable", star_version_plugin()); + assert_eq!(1, pm.borrow().get_plugins().len()); } -#[ignore = "Requires mocking getPluginApiVersion; PluginManager has no such seam (TODO(plugin))"] #[test] fn test_plugin_constraint_works_only_with_certain_api_version() { - // TODO(phase-d): requires mocking getPluginApiVersion; PluginManager has no such seam - // (TODO(plugin)). - todo!() + if !php_runtime_available() { + return; + } + let _worker = lock_php_worker(); + let set_up = set_up(); + let plugin_with_api_constraint = || vec![PackageInterfaceHandle::dup(&set_up.packages[5])]; + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.0.0", plugin_with_api_constraint()); + assert_eq!(0, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.1.9", plugin_with_api_constraint()); + assert_eq!(0, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.2.0", plugin_with_api_constraint()); + assert_eq!(1, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.9.9", plugin_with_api_constraint()); + assert_eq!(1, pm.borrow().get_plugins().len()); } -#[ignore = "Requires mocking getPluginApiVersion; PluginManager has no such seam (TODO(plugin))"] #[test] fn test_plugin_range_constraints_work_only_with_certain_api_version() { - // TODO(phase-d): requires mocking getPluginApiVersion; PluginManager has no such seam - // (TODO(plugin)). - todo!() + if !php_runtime_available() { + return; + } + let _worker = lock_php_worker(); + let set_up = set_up(); + let plugin_with_api_constraint = || vec![PackageInterfaceHandle::dup(&set_up.packages[6])]; + + let pm = set_plugin_api_version_with_plugins(&set_up, "1.0.0", plugin_with_api_constraint()); + assert_eq!(0, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "3.0.0", plugin_with_api_constraint()); + assert_eq!(1, pm.borrow().get_plugins().len()); + + let pm = set_plugin_api_version_with_plugins(&set_up, "5.5.0", plugin_with_api_constraint()); + assert_eq!(0, pm.borrow().get_plugins().len()); } #[test] -- cgit v1.3.1