diff options
Diffstat (limited to 'crates/shirabe')
7 files changed, 361 insertions, 0 deletions
diff --git a/crates/shirabe/tests/plugin/e2e_installer_test.rs b/crates/shirabe/tests/plugin/e2e_installer_test.rs new file mode 100644 index 00000000..dadb5b59 --- /dev/null +++ b/crates/shirabe/tests/plugin/e2e_installer_test.rs @@ -0,0 +1,164 @@ +//! Plugin-provided installer E2E compatibility check: upstream Composer and Shirabe each install +//! a fixture plugin that registers its own `InstallerInterface` through +//! `InstallationManager::addInstaller`, and the resulting project trees — including the trace the +//! installer writes on every contract call — are compared. +//! +//! The whole fixture is Shirabe-authored (`fixtures/e2e-installer/`), so nothing has to be +//! fetched; the test skips only while the PHP runtime or the Composer checkout is missing. + +use crate::e2e_extension_installer_test::{copy_dir, upstream_composer_bin}; +use crate::plugin_installer_test::{lock_php_worker, php_runtime_available}; +use std::path::{Path, PathBuf}; +use tempfile::TempDir; + +fn fixture_dir() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/plugin/fixtures/e2e-installer") +} + +struct CommandRun { + exit_code: i32, + stdout: String, +} + +fn run_command(work: &Path, program: &str, prefix_args: &[&str], args: &[&str]) -> CommandRun { + let output = std::process::Command::new(program) + .args(prefix_args) + .args(args) + .current_dir(work.join("project")) + .env("COMPOSER_HOME", work.join("home")) + .env("COMPOSER_CACHE_DIR", work.join("cache")) + .env("COMPOSER_NO_INTERACTION", "1") + .env("COLUMNS", "120") + .env("LINES", "30") + .output() + .unwrap(); + CommandRun { + exit_code: output.status.code().unwrap_or(-1), + stdout: String::from_utf8_lossy(&output.stdout).into_owned(), + } +} + +/// Every file under `dir` as (relative path, contents), so two project trees compare as a whole. +fn tree(dir: &Path) -> Vec<(String, String)> { + let mut files = Vec::new(); + collect(dir, dir, &mut files); + files.sort(); + files +} + +fn collect(root: &Path, dir: &Path, files: &mut Vec<(String, String)>) { + for entry in std::fs::read_dir(dir).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); + if entry.file_type().unwrap().is_dir() { + collect(root, &path, files); + } else { + let relative = path + .strip_prefix(root) + .unwrap() + .to_str() + .unwrap() + .to_string(); + files.push((relative, std::fs::read_to_string(&path).unwrap_or_default())); + } + } +} + +fn installer_lines(stdout: &str) -> Vec<&str> { + stdout + .lines() + .filter(|line| line.starts_with("asset-installer:")) + .collect() +} + +/// Runs `install` in a fresh copy of the fixture and returns the run plus the resulting tree. +fn install(program: &str, prefix: &[&str]) -> (CommandRun, Vec<(String, String)>, TempDir) { + let work = TempDir::new().unwrap(); + copy_dir(&fixture_dir(), work.path()); + let run = run_command(work.path(), program, prefix, &["install"]); + let tree = tree(&work.path().join("project")); + (run, tree, work) +} + +#[test] +fn test_plugin_provided_installer_matches_upstream_composer() { + if !php_runtime_available() { + return; + } + let Some(composer_bin) = upstream_composer_bin() else { + return; + }; + let _worker = lock_php_worker(); + let composer_bin = composer_bin.to_str().unwrap().to_string(); + + let (upstream, upstream_tree, _upstream_work) = install("php", &[composer_bin.as_str()]); + let (shirabe, shirabe_tree, _shirabe_work) = install(env!("CARGO_BIN_EXE_shirabe"), &[]); + + assert_eq!(0, upstream.exit_code, "upstream install must succeed"); + assert_eq!(upstream.exit_code, shirabe.exit_code); + assert_eq!( + installer_lines(&upstream.stdout), + installer_lines(&shirabe.stdout) + ); + assert_eq!( + vec!["asset-installer: installed shirabe-test/asset-a"], + installer_lines(&shirabe.stdout) + ); + + // The whole project tree, which covers composer.lock, installed.json/installed.php, the + // manifest the plugin-provided installer wrote under assets/, and installer-trace.txt — the + // ordered log of every InstallerInterface call the installer received. + assert_eq!( + upstream_tree.iter().map(|(p, _)| p).collect::<Vec<_>>(), + shirabe_tree.iter().map(|(p, _)| p).collect::<Vec<_>>() + ); + assert_eq!(upstream_tree, shirabe_tree); +} + +// Re-running `install` over an already installed project, and removing the asset package +// afterwards, produce the same files on both sides but consult the installers in a different +// order: upstream resolves `getInstaller('composer-plugin')` before the asset operations run, +// Shirabe only when the autoload dump asks for the install paths. The counts differ too once a +// `remove` re-creates the Composer instance. +#[ignore = "InstallationManager consults getInstaller at different points than upstream; installer-trace.txt diverges in order and count (TODO(plugin))"] +#[test] +fn test_plugin_provided_installer_call_order_matches_upstream_composer() { + if !php_runtime_available() { + return; + } + let Some(composer_bin) = upstream_composer_bin() else { + return; + }; + let _worker = lock_php_worker(); + let composer_bin = composer_bin.to_str().unwrap().to_string(); + let implementations: [(&str, Vec<&str>); 2] = [ + ("php", vec![composer_bin.as_str()]), + (env!("CARGO_BIN_EXE_shirabe"), vec![]), + ]; + + let mut results = Vec::new(); + for (program, prefix) in &implementations { + let work = TempDir::new().unwrap(); + copy_dir(&fixture_dir(), work.path()); + let first = run_command(work.path(), program, prefix, &["install"]); + let second = run_command(work.path(), program, prefix, &["install"]); + let remove = run_command( + work.path(), + program, + prefix, + &["remove", "shirabe-test/asset-a"], + ); + let tree = tree(&work.path().join("project")); + results.push((first, second, remove, tree)); + } + + let [upstream, shirabe] = <[_; 2]>::try_from(results).ok().unwrap(); + let (u_first, u_second, u_remove, u_tree) = upstream; + let (s_first, s_second, s_remove, s_tree) = shirabe; + + assert_eq!(0, u_first.exit_code); + assert_eq!(u_first.exit_code, s_first.exit_code); + assert_eq!(u_second.exit_code, s_second.exit_code); + assert_eq!(u_remove.exit_code, s_remove.exit_code); + assert_eq!(u_tree, s_tree); +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-installer/packages/asset-a/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-installer/packages/asset-a/composer.json new file mode 100644 index 00000000..4fe63968 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-installer/packages/asset-a/composer.json @@ -0,0 +1,6 @@ +{ + "name": "shirabe-test/asset-a", + "version": "1.0.0", + "type": "shirabe-asset", + "description": "Fixture package installed by the plugin-provided installer." +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/composer.json new file mode 100644 index 00000000..8275f2c0 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/composer.json @@ -0,0 +1,17 @@ +{ + "name": "shirabe-test/asset-installer", + "version": "1.0.0", + "type": "composer-plugin", + "description": "Fixture plugin registering a custom installer through InstallationManager::addInstaller.", + "autoload": { + "psr-4": { + "ShirabeTest\\AssetInstaller\\": "src/" + } + }, + "require": { + "composer-plugin-api": "^2.0" + }, + "extra": { + "class": "ShirabeTest\\AssetInstaller\\Plugin" + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/AssetInstaller.php b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/AssetInstaller.php new file mode 100644 index 00000000..a80eb678 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/AssetInstaller.php @@ -0,0 +1,113 @@ +<?php + +namespace ShirabeTest\AssetInstaller; + +use Composer\Composer; +use Composer\Installer\InstallerInterface; +use Composer\IO\IOInterface; +use Composer\Package\PackageInterface; +use Composer\Repository\InstalledRepositoryInterface; +use React\Promise\PromiseInterface; + +/** + * Installs `shirabe-asset` packages by writing a manifest of what the installer observed, + * without going through the download manager. Every call also appends a trace line, so the + * order and arguments of the installer contract are comparable between implementations. + */ +class AssetInstaller implements InstallerInterface +{ + /** @var IOInterface */ + private $io; + + /** @var Composer */ + private $composer; + + public function __construct(IOInterface $io, Composer $composer) + { + $this->io = $io; + $this->composer = $composer; + } + + public function supports(string $packageType): bool + { + $this->trace('supports ' . $packageType); + + return $packageType === 'shirabe-asset'; + } + + public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package): bool + { + $this->trace('isInstalled ' . $package->getPrettyName()); + + return is_file($this->getInstallPath($package) . '/asset.txt'); + } + + public function download(PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface + { + $this->trace('download ' . $package->getPrettyName() . ' prev=' . $this->name($prevPackage)); + + return null; + } + + public function prepare(string $type, PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface + { + $this->trace('prepare ' . $type . ' ' . $package->getPrettyName() . ' prev=' . $this->name($prevPackage)); + + return null; + } + + public function install(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface + { + $path = $this->getInstallPath($package); + $this->trace('install ' . $package->getPrettyName() . ' -> ' . $path); + @mkdir($path, 0777, true); + file_put_contents($path . '/asset.txt', implode("\n", [ + 'name=' . $package->getPrettyName(), + 'version=' . $package->getPrettyVersion(), + 'type=' . $package->getType(), + 'root=' . $this->composer->getPackage()->getName(), + ]) . "\n"); + $this->io->write('asset-installer: installed ' . $package->getPrettyName()); + + return null; + } + + public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target): ?PromiseInterface + { + $this->trace('update ' . $initial->getPrettyName() . ' -> ' . $target->getPrettyName()); + + return $this->install($repo, $target); + } + + public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface + { + $path = $this->getInstallPath($package); + $this->trace('uninstall ' . $package->getPrettyName()); + @unlink($path . '/asset.txt'); + @rmdir($path); + + return null; + } + + public function cleanup(string $type, PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface + { + $this->trace('cleanup ' . $type . ' ' . $package->getPrettyName()); + + return null; + } + + public function getInstallPath(PackageInterface $package): string + { + return 'assets/' . $package->getPrettyName(); + } + + private function name(?PackageInterface $package): string + { + return $package === null ? 'null' : $package->getPrettyName(); + } + + private function trace(string $line): void + { + file_put_contents('installer-trace.txt', $line . "\n", FILE_APPEND); + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/Plugin.php b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/Plugin.php new file mode 100644 index 00000000..24a1d295 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-installer/plugin/src/Plugin.php @@ -0,0 +1,28 @@ +<?php + +namespace ShirabeTest\AssetInstaller; + +use Composer\Composer; +use Composer\IO\IOInterface; +use Composer\Plugin\PluginInterface; + +class Plugin implements PluginInterface +{ + /** @var AssetInstaller */ + private $installer; + + public function activate(Composer $composer, IOInterface $io): void + { + $this->installer = new AssetInstaller($io, $composer); + $composer->getInstallationManager()->addInstaller($this->installer); + } + + public function deactivate(Composer $composer, IOInterface $io): void + { + $composer->getInstallationManager()->removeInstaller($this->installer); + } + + public function uninstall(Composer $composer, IOInterface $io): void + { + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-installer/project/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-installer/project/composer.json new file mode 100644 index 00000000..64c1bb32 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-installer/project/composer.json @@ -0,0 +1,32 @@ +{ + "name": "shirabe/e2e-installer", + "description": "E2E fixture project: install a custom package type through a plugin-provided installer.", + "repositories": [ + { + "type": "path", + "url": "../plugin", + "options": { + "symlink": false + } + }, + { + "type": "path", + "url": "../packages/*", + "options": { + "symlink": false + } + }, + { + "packagist.org": false + } + ], + "require": { + "shirabe-test/asset-installer": "1.0.0", + "shirabe-test/asset-a": "1.0.0" + }, + "config": { + "allow-plugins": { + "shirabe-test/asset-installer": true + } + } +} diff --git a/crates/shirabe/tests/plugin/main.rs b/crates/shirabe/tests/plugin/main.rs index a65594be..02db6b03 100644 --- a/crates/shirabe/tests/plugin/main.rs +++ b/crates/shirabe/tests/plugin/main.rs @@ -5,6 +5,7 @@ mod config_stub; mod e2e_command_provider_test; mod e2e_extension_installer_test; +mod e2e_installer_test; mod e2e_normalize_test; mod plugin_installer_test; mod subscriber_test; |
