diff options
Diffstat (limited to 'crates/shirabe/tests')
5 files changed, 244 insertions, 0 deletions
diff --git a/crates/shirabe/tests/plugin/e2e_process_executor_test.rs b/crates/shirabe/tests/plugin/e2e_process_executor_test.rs new file mode 100644 index 00000000..e1bb2aa6 --- /dev/null +++ b/crates/shirabe/tests/plugin/e2e_process_executor_test.rs @@ -0,0 +1,84 @@ +//! ProcessExecutor E2E compatibility check: upstream Composer and Shirabe each install a fixture +//! project whose plugin builds its own `ProcessExecutor` and writes what every call on it reports +//! to a trace file. Upstream has no test that drives a process executor from plugin code, so the +//! whole fixture is Shirabe-authored (`fixtures/e2e-process-executor/`) 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-process-executor") +} + +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("process-executor-trace.txt")) + .unwrap_or_default(), + } +} + +#[test] +fn test_plugin_owned_process_executor_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 = 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 + // timeout is the project's `process-timeout`, which is what makes it evidence that both + // worlds read one value rather than each holding its own default. + assert_eq!( + "\ +event=post-update-cmd +timeout=42 +timeout-after-set=7 +capture code=0 output=\"captured\\n\" error=\"\" +list code=0 output=\"from a list\\n\" +failing code=3 output=\"out\\n\" error=\"err\\n\" +forwarded code=0 file=\"forwarded\" +callback code=0 seen=[\"out:through-a-callback\"] argument=true +cwd code=0 basename=\"vendor\" +splitLines code=0 lines=[\"x\",\"y\"] empty=[] +escape=\"'a b'\\\\''c'\" +requiresGitDirEnv status=false +maxJobs=ok +filesystem normalizePath=\"\\/a\\/c\" isLocalPath=true getPlatformPath=\"\\/a\\/b\" +", + upstream.trace + ); +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/composer.json new file mode 100644 index 00000000..5d5609c1 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/composer.json @@ -0,0 +1,17 @@ +{ + "name": "shirabe-test/process-executor-probe", + "version": "1.0.0", + "type": "composer-plugin", + "description": "Fixture plugin driving a ProcessExecutor it constructs itself.", + "autoload": { + "psr-4": { + "ShirabeTest\\ProcessExecutor\\": "src/" + } + }, + "require": { + "composer-plugin-api": "^2.0" + }, + "extra": { + "class": "ShirabeTest\\ProcessExecutor\\Plugin" + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/src/Plugin.php b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/src/Plugin.php new file mode 100644 index 00000000..1f58a11d --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/plugin/src/Plugin.php @@ -0,0 +1,117 @@ +<?php + +namespace ShirabeTest\ProcessExecutor; + +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; +use Composer\Util\ProcessExecutor; + +/** + * Drives a ProcessExecutor the plugin constructs itself and appends what every call reports to + * process-executor-trace.txt, so the whole surface can be compared line by line between + * implementations: the timeout shared with the rest of the run, both command forms, the three + * ways the second argument is treated, the error output, and a Filesystem built on the executor. + */ +class Plugin implements PluginInterface, EventSubscriberInterface +{ + /** @var IOInterface */ + private $io; + + public function activate(Composer $composer, IOInterface $io): void + { + $this->io = $io; + } + + 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 + { + $process = new ProcessExecutor($this->io); + $lines = ['event=' . $event->getName()]; + + // The timeout is process-wide state Composer seeds from the config, so both worlds have + // to report the value this project asked for and to observe each other's writes. + $original = ProcessExecutor::getTimeout(); + $lines[] = 'timeout=' . $original; + ProcessExecutor::setTimeout(7); + $lines[] = 'timeout-after-set=' . ProcessExecutor::getTimeout(); + ProcessExecutor::setTimeout($original); + + $code = $process->execute('echo captured', $captured); + $lines[] = 'capture code=' . $code . ' output=' . json_encode($captured) + . ' error=' . json_encode($process->getErrorOutput()); + + $code = $process->execute(['echo', 'from', 'a', 'list'], $listed); + $lines[] = 'list code=' . $code . ' output=' . json_encode($listed); + + $code = $process->execute('echo out; echo err 1>&2; exit 3', $failed); + $lines[] = 'failing code=' . $code . ' output=' . json_encode($failed) + . ' error=' . json_encode($process->getErrorOutput()); + + // Without a second argument the child's output is forwarded rather than captured, which + // is a different branch of the same method; the redirection keeps it out of the terminal + // so the trace stays the only thing under comparison. + $code = $process->execute('echo forwarded > forwarded.txt'); + $lines[] = 'forwarded code=' . $code + . ' file=' . json_encode(trim((string) @file_get_contents('forwarded.txt'))); + + // A callable second argument drives the child's output itself and is never assigned to. + $seen = []; + $callback = static function (string $type, string $buffer) use (&$seen): void { + $seen[] = $type . ':' . trim($buffer); + }; + $code = $process->execute('echo through-a-callback', $callback); + $lines[] = 'callback code=' . $code . ' seen=' . json_encode($seen) + . ' argument=' . json_encode(\is_callable($callback)); + + $code = $process->execute('pwd', $cwdOutput, 'vendor'); + $lines[] = 'cwd code=' . $code . ' basename=' . json_encode(basename(trim((string) $cwdOutput))); + + $code = $process->execute('echo x; echo y', $multiline); + $lines[] = 'splitLines code=' . $code + . ' lines=' . json_encode($process->splitLines($multiline)) + . ' empty=' . json_encode($process->splitLines(null)); + + $lines[] = 'escape=' . json_encode(ProcessExecutor::escape("a b'c")); + // TODO(php-semantics): a command matching GIT_CMDS_NEED_GIT_DIR has no agreed value to + // compare. array_intersect() keeps its first argument's keys and `===` compares an + // array's keys too, so Composer answers false for those patterns as well; the shim's + // array_intersect drops the keys and Shirabe answers true. + $lines[] = 'requiresGitDirEnv status=' + . json_encode($process->requiresGitDirEnv('git status')); + + $process->setMaxJobs(4); + $process->resetMaxJobs(); + $lines[] = 'maxJobs=ok'; + + // The executor is a constructor argument of other Composer utilities, so a plugin-built + // one has to be accepted wherever the real class is. + $filesystem = new Filesystem($process); + $lines[] = 'filesystem normalizePath=' . json_encode($filesystem->normalizePath('/a/b/../c')) + . ' isLocalPath=' . json_encode(Filesystem::isLocalPath('/a/b')) + . ' getPlatformPath=' . json_encode(Filesystem::getPlatformPath('file:///a/b')); + + file_put_contents('process-executor-trace.txt', implode("\n", $lines) . "\n"); + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/project/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/project/composer.json new file mode 100644 index 00000000..61ca9a5a --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-process-executor/project/composer.json @@ -0,0 +1,25 @@ +{ + "name": "shirabe/e2e-process-executor", + "description": "E2E fixture project: record what a plugin's own ProcessExecutor reports.", + "repositories": [ + { + "type": "path", + "url": "../plugin", + "options": { + "symlink": false + } + }, + { + "packagist.org": false + } + ], + "require": { + "shirabe-test/process-executor-probe": "1.0.0" + }, + "config": { + "process-timeout": 42, + "allow-plugins": { + "shirabe-test/process-executor-probe": true + } + } +} diff --git a/crates/shirabe/tests/plugin/main.rs b/crates/shirabe/tests/plugin/main.rs index 000ca3a9..35f5beb5 100644 --- a/crates/shirabe/tests/plugin/main.rs +++ b/crates/shirabe/tests/plugin/main.rs @@ -12,6 +12,7 @@ mod e2e_installer_test; mod e2e_installers_test; mod e2e_normalize_test; mod e2e_package_event_test; +mod e2e_process_executor_test; mod e2e_script_command_test; mod e2e_script_event_test; mod plugin_installer_test; |
