diff options
Diffstat (limited to 'crates/shirabe/tests/plugin')
5 files changed, 213 insertions, 0 deletions
diff --git a/crates/shirabe/tests/plugin/e2e_exception_test.rs b/crates/shirabe/tests/plugin/e2e_exception_test.rs new file mode 100644 index 00000000..ddbd1cf8 --- /dev/null +++ b/crates/shirabe/tests/plugin/e2e_exception_test.rs @@ -0,0 +1,79 @@ +//! Exception fidelity E2E check: upstream Composer and Shirabe each install a fixture project +//! whose plugin catches the exceptions a Composer service raises at it and writes what it saw to +//! a trace file. Upstream has no test that inspects an exception from plugin code, so the whole +//! fixture is Shirabe-authored (`fixtures/e2e-exception/`) and 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::php_worker::{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-exception") +} + +struct Run { + exit_code: i32, + trace: String, +} + +/// Runs `install` in a fresh copy of the fixture and returns the exit code with the plugin's trace. +fn install(program: &str, prefix_args: &[&str]) -> Run { + let work = TempDir::new().unwrap(); + copy_dir(&fixture_dir(), work.path()); + let project = work.path().join("project"); + let output = std::process::Command::new(program) + .args(prefix_args) + .arg("install") + .current_dir(&project) + .env("COMPOSER_HOME", work.path().join("home")) + .env("COMPOSER_CACHE_DIR", work.path().join("cache")) + .env("COMPOSER_NO_INTERACTION", "1") + .env("COLUMNS", "120") + .env("LINES", "30") + .output() + .unwrap(); + Run { + exit_code: output.status.code().unwrap_or(-1), + trace: std::fs::read_to_string(project.join("exception-trace.txt")).unwrap_or_default(), + } +} + +#[test] +fn test_exceptions_reach_a_plugin_as_their_own_class() { + 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 = install("php", &[composer_bin.as_str()]); + let shirabe = 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!(upstream.trace, shirabe.trace); + + // Pinned as well as compared, so a run where neither side wrote a trace cannot pass. The + // class names and the two hierarchy answers are the evidence that the exception crossed as + // itself rather than as one collapsed shape. + assert_eq!( + "\ +event=post-update-cmd +findShortestPath class=\"InvalidArgumentException\" \ +message=\"$from (relative) and $to (\\/absolute) must be absolute paths.\" code=0 \ +logic=true runtime=false +findShortestPathCode class=\"InvalidArgumentException\" \ +message=\"$from (\\/absolute) and $to (relative) must be absolute paths.\" code=0 \ +logic=true runtime=false +ensureDirectoryExists class=\"RuntimeException\" \ +message=\"not-a-directory exists and is not a directory.\" code=0 logic=false runtime=true +catch-clause=InvalidArgumentException +", + upstream.trace + ); +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/composer.json new file mode 100644 index 00000000..4bc436ec --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/composer.json @@ -0,0 +1,17 @@ +{ + "name": "shirabe-test/exception-probe", + "version": "1.0.0", + "type": "composer-plugin", + "description": "Fixture plugin recording the exceptions a Composer service raises at it.", + "autoload": { + "psr-4": { + "ShirabeTest\\Exception\\": "src/" + } + }, + "require": { + "composer-plugin-api": "^2.0" + }, + "extra": { + "class": "ShirabeTest\\Exception\\Plugin" + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/src/Plugin.php b/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/src/Plugin.php new file mode 100644 index 00000000..547f4fbb --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-exception/plugin/src/Plugin.php @@ -0,0 +1,92 @@ +<?php + +namespace ShirabeTest\Exception; + +use Composer\Composer; +use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\IO\IOInterface; +use Composer\Plugin\PluginInterface; +use Composer\Script\Event; +use Composer\Script\ScriptEvents; +use Composer\Util\Filesystem; + +/** + * Records what a plugin sees when a Composer service raises an exception at it: the class it was + * thrown as, its message and code, whether it is still an instance of the parent classes the real + * hierarchy gives it, and whether a `catch` naming that class matches. Composer plugins branch on + * the exception class rather than on its message, so the whole surface is compared line by line + * between implementations. + */ +class Plugin implements PluginInterface, EventSubscriberInterface +{ + public function activate(Composer $composer, IOInterface $io): void + { + } + + public function deactivate(Composer $composer, IOInterface $io): void + { + } + + public function uninstall(Composer $composer, IOInterface $io): void + { + } + + public static function getSubscribedEvents() + { + // Whether an install resolves or replays a lock file decides which of the two fires, so + // both are subscribed and the trace records the one that ran. + return [ + ScriptEvents::POST_INSTALL_CMD => 'onPostCommand', + ScriptEvents::POST_UPDATE_CMD => 'onPostCommand', + ]; + } + + public function onPostCommand(Event $event): void + { + $filesystem = new Filesystem(); + $lines = ['event=' . $event->getName()]; + + $lines[] = 'findShortestPath ' . $this->describe(static function () use ($filesystem): void { + $filesystem->findShortestPath('relative', '/absolute'); + }); + $lines[] = 'findShortestPathCode ' . $this->describe(static function () use ($filesystem): void { + $filesystem->findShortestPathCode('/absolute', 'relative'); + }); + + // A different class through the same seam, so the trace shows the class travelling rather + // than every failure arriving under one name. + file_put_contents('not-a-directory', ''); + $lines[] = 'ensureDirectoryExists ' . $this->describe(static function () use ($filesystem): void { + $filesystem->ensureDirectoryExists('not-a-directory'); + }); + + // get_class() answers for the object; a catch clause answers for the class hierarchy the + // child holds, which is what plugin code is actually written against. + try { + $filesystem->findShortestPath('relative', '/absolute'); + $caught = 'nothing-thrown'; + } catch (\InvalidArgumentException $e) { + $caught = 'InvalidArgumentException'; + } catch (\Throwable $e) { + $caught = 'unmatched:' . \get_class($e); + } + $lines[] = 'catch-clause=' . $caught; + + file_put_contents('exception-trace.txt', implode("\n", $lines) . "\n"); + } + + private function describe(callable $call): string + { + try { + $call(); + + return 'class=none'; + } catch (\Throwable $e) { + return 'class=' . json_encode(\get_class($e)) + . ' message=' . json_encode($e->getMessage()) + . ' code=' . json_encode($e->getCode()) + . ' logic=' . json_encode($e instanceof \LogicException) + . ' runtime=' . json_encode($e instanceof \RuntimeException); + } + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-exception/project/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-exception/project/composer.json new file mode 100644 index 00000000..da06cc93 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-exception/project/composer.json @@ -0,0 +1,24 @@ +{ + "name": "shirabe/e2e-exception", + "description": "E2E fixture project: record the exceptions a plugin catches from Composer services.", + "repositories": [ + { + "type": "path", + "url": "../plugin", + "options": { + "symlink": false + } + }, + { + "packagist.org": false + } + ], + "require": { + "shirabe-test/exception-probe": "1.0.0" + }, + "config": { + "allow-plugins": { + "shirabe-test/exception-probe": true + } + } +} diff --git a/crates/shirabe/tests/plugin/main.rs b/crates/shirabe/tests/plugin/main.rs index 35f5beb5..3ef3f647 100644 --- a/crates/shirabe/tests/plugin/main.rs +++ b/crates/shirabe/tests/plugin/main.rs @@ -7,6 +7,7 @@ mod php_worker; mod alias_package_test; mod e2e_command_provider_test; +mod e2e_exception_test; mod e2e_extension_installer_test; mod e2e_installer_test; mod e2e_installers_test; |
