From 3b0787fdecc21edf9ad22b82f547363c1aa79b15 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 5 Aug 2026 03:58:11 +0900 Subject: test(plugin): compare plugin-provided command execution against upstream A Shirabe-authored CommandProvider fixture plugin (greet) runs under both implementations: exit codes, the command's own output, help/list rendering, alias resolution, a validation failure, and a file recording the shared object graph the command observed (root package, strict application FQCN, and the exit code of the built-in about command it invoked) must match. Co-Authored-By: Claude Fable 5 --- .../tests/plugin/e2e_command_provider_test.rs | 145 +++++++++++++++++++++ .../tests/plugin/e2e_extension_installer_test.rs | 4 +- .../fixtures/e2e-command/plugin/composer.json | 17 +++ .../e2e-command/plugin/src/GreetCommand.php | 48 +++++++ .../plugin/src/GreetCommandProvider.php | 13 ++ .../fixtures/e2e-command/plugin/src/Plugin.php | 30 +++++ .../fixtures/e2e-command/project/composer.json | 24 ++++ crates/shirabe/tests/plugin/main.rs | 1 + 8 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 crates/shirabe/tests/plugin/e2e_command_provider_test.rs create mode 100644 crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/composer.json create mode 100644 crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommand.php create mode 100644 crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommandProvider.php create mode 100644 crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/Plugin.php create mode 100644 crates/shirabe/tests/plugin/fixtures/e2e-command/project/composer.json (limited to 'crates/shirabe/tests') diff --git a/crates/shirabe/tests/plugin/e2e_command_provider_test.rs b/crates/shirabe/tests/plugin/e2e_command_provider_test.rs new file mode 100644 index 00000000..4d04fb22 --- /dev/null +++ b/crates/shirabe/tests/plugin/e2e_command_provider_test.rs @@ -0,0 +1,145 @@ +//! Plugin-provided command E2E compatibility check: upstream Composer and Shirabe each +//! install a CommandProvider fixture plugin, and then `list`, `help greet`, an actual `greet` +//! run (which itself invokes the built-in `about` command from inside the plugin command) and +//! an alias invocation are compared between the two. +//! +//! The whole fixture is Shirabe-authored (`fixtures/e2e-command/`), 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-command") +} + +struct CommandRun { + exit_code: i32, + stdout: String, +} + +/// One composer-CLI invocation inside the prepared project. +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") + // Rendering width must not depend on the invoking terminal. + .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(), + } +} + +fn greet_lines(stdout: &str) -> Vec<&str> { + stdout + .lines() + .filter(|line| line.starts_with("greet:")) + .collect() +} + +fn list_lines_mentioning_greet(stdout: &str) -> Vec<&str> { + stdout + .lines() + .filter(|line| line.contains("greet")) + .collect() +} + +#[test] +fn test_command_provider_execution_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<( + CommandRun, + CommandRun, + CommandRun, + CommandRun, + CommandRun, + String, + )> = Vec::new(); + for (program, prefix) in &implementations { + let work = TempDir::new().unwrap(); + copy_dir(&fixture_dir(), work.path()); + let install = run_command(work.path(), program, prefix, &["install"]); + assert_eq!(0, install.exit_code, "{program}: install must succeed"); + + let observed = work.path().join("project/observed.txt"); + let observed_arg = format!("--out={}", observed.to_str().unwrap()); + let greet = run_command( + work.path(), + program, + prefix, + &["greet", "World", "--shout", &observed_arg], + ); + let observed = std::fs::read_to_string(&observed).unwrap_or_default(); + let alias = run_command( + work.path(), + program, + prefix, + &["hi", "there", "--out=o2.txt"], + ); + let help = run_command(work.path(), program, prefix, &["help", "greet"]); + let list = run_command(work.path(), program, prefix, &["list"]); + let missing_argument = run_command(work.path(), program, prefix, &["greet"]); + results.push((greet, alias, help, list, missing_argument, observed)); + } + + let [upstream, shirabe] = <[_; 2]>::try_from(results).ok().unwrap(); + let (u_greet, u_alias, u_help, u_list, u_missing, u_observed) = upstream; + let (s_greet, s_alias, s_help, s_list, s_missing, s_observed) = shirabe; + + assert_eq!(0, u_greet.exit_code, "upstream greet must succeed"); + assert_eq!(u_greet.exit_code, s_greet.exit_code); + assert_eq!(greet_lines(&u_greet.stdout), greet_lines(&s_greet.stdout)); + assert_eq!(vec!["greet: HELLO WORLD"], greet_lines(&s_greet.stdout)); + + // The file the plugin command wrote proves it observed the shared object graph — the root + // package through requireComposer(), a strict get_class() on the application FQCN, and the + // exit code of the built-in `about` command it invoked through the application. + assert_eq!(u_observed, s_observed); + assert_eq!( + "root=shirabe/e2e-command-provider\napp=Composer\\Console\\Application\nabout=0\n", + s_observed + ); + + assert_eq!(0, u_alias.exit_code); + assert_eq!(u_alias.exit_code, s_alias.exit_code); + assert_eq!(greet_lines(&u_alias.stdout), greet_lines(&s_alias.stdout)); + + assert_eq!(0, u_help.exit_code, "upstream help greet must succeed"); + assert_eq!(u_help.exit_code, s_help.exit_code); + assert_eq!(u_help.stdout, s_help.stdout, "help greet output differs"); + + assert_eq!(0, u_list.exit_code); + assert_eq!(u_list.exit_code, s_list.exit_code); + assert_eq!( + list_lines_mentioning_greet(&u_list.stdout), + list_lines_mentioning_greet(&s_list.stdout) + ); + + // A validation failure (missing required argument) must fail on both sides; the rendering + // of the error is implementation-owned and is not compared. + assert_ne!(0, u_missing.exit_code); + assert_ne!(0, s_missing.exit_code); +} diff --git a/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs b/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs index c78af3cd..9a95888c 100644 --- a/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs +++ b/crates/shirabe/tests/plugin/e2e_extension_installer_test.rs @@ -20,7 +20,7 @@ fn fixture_dir() -> PathBuf { /// The upstream Composer checkout used as the comparison oracle (and as the PHP runtime of /// the worker). Absent checkout means the oracle cannot run; the test returns early, /// following the convention of the non-mock tests in `shirabe-php-rpc`. -fn upstream_composer_bin() -> Option { +pub(crate) fn upstream_composer_bin() -> Option { let root = match std::env::var("SHIRABE_COMPOSER_PHP_DIR") { Ok(dir) => PathBuf::from(dir), Err(_) => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../composer"), @@ -33,7 +33,7 @@ fn upstream_composer_bin() -> Option { } } -fn copy_dir(from: &Path, to: &Path) { +pub(crate) fn copy_dir(from: &Path, to: &Path) { std::fs::create_dir_all(to).unwrap(); for entry in std::fs::read_dir(from).unwrap() { let entry = entry.unwrap(); diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/composer.json new file mode 100644 index 00000000..76cf19e9 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/composer.json @@ -0,0 +1,17 @@ +{ + "name": "shirabe-test/command-provider", + "version": "1.0.0", + "type": "composer-plugin", + "description": "Fixture plugin providing a command through the CommandProvider capability.", + "autoload": { + "psr-4": { + "ShirabeTest\\CommandProvider\\": "src/" + } + }, + "require": { + "composer-plugin-api": "^2.0" + }, + "extra": { + "class": "ShirabeTest\\CommandProvider\\Plugin" + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommand.php b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommand.php new file mode 100644 index 00000000..51b3e765 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommand.php @@ -0,0 +1,48 @@ +setName('greet') + ->setAliases(['hi']) + ->setDescription('Greets someone from a plugin-provided command.') + ->setHelp('The greet command exercises plugin-provided command execution.') + ->addArgument('who', InputArgument::REQUIRED, 'Who to greet') + ->addOption('shout', 's', InputOption::VALUE_NONE, 'Uppercase the greeting') + ->addOption('out', null, InputOption::VALUE_REQUIRED, 'File the command writes its observations to'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $message = 'Hello ' . $input->getArgument('who'); + if ($input->getOption('shout')) { + $message = strtoupper($message); + } + $this->getIO()->write('greet: ' . $message); + + $composer = $this->requireComposer(); + $lines = [ + 'root=' . $composer->getPackage()->getName(), + 'app=' . get_class($this->getApplication()), + ]; + $aboutExit = $this->getApplication()->find('about')->run(new ArrayInput(['command' => 'about']), $output); + $lines[] = 'about=' . $aboutExit; + $out = $input->getOption('out'); + if ($out !== null) { + file_put_contents($out, implode("\n", $lines) . "\n"); + } + + return 0; + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommandProvider.php b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommandProvider.php new file mode 100644 index 00000000..ce36e062 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommandProvider.php @@ -0,0 +1,13 @@ + GreetCommandProvider::class, + ]; + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-command/project/composer.json b/crates/shirabe/tests/plugin/fixtures/e2e-command/project/composer.json new file mode 100644 index 00000000..e68a53de --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/e2e-command/project/composer.json @@ -0,0 +1,24 @@ +{ + "name": "shirabe/e2e-command-provider", + "description": "E2E fixture project: run a plugin-provided command against upstream Composer and Shirabe.", + "repositories": [ + { + "type": "path", + "url": "../plugin", + "options": { + "symlink": false + } + }, + { + "packagist.org": false + } + ], + "require": { + "shirabe-test/command-provider": "1.0.0" + }, + "config": { + "allow-plugins": { + "shirabe-test/command-provider": true + } + } +} diff --git a/crates/shirabe/tests/plugin/main.rs b/crates/shirabe/tests/plugin/main.rs index b2631eb7..633ad790 100644 --- a/crates/shirabe/tests/plugin/main.rs +++ b/crates/shirabe/tests/plugin/main.rs @@ -3,6 +3,7 @@ mod async_runtime; #[path = "../common/config_stub.rs"] mod config_stub; +mod e2e_command_provider_test; mod e2e_extension_installer_test; mod plugin_installer_test; mod subscriber_test; -- cgit v1.3.1