aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/tests')
-rw-r--r--crates/shirabe/tests/plugin/e2e_command_provider_test.rs145
-rw-r--r--crates/shirabe/tests/plugin/e2e_extension_installer_test.rs4
-rw-r--r--crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/composer.json17
-rw-r--r--crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommand.php48
-rw-r--r--crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/GreetCommandProvider.php13
-rw-r--r--crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/Plugin.php30
-rw-r--r--crates/shirabe/tests/plugin/fixtures/e2e-command/project/composer.json24
-rw-r--r--crates/shirabe/tests/plugin/main.rs1
8 files changed, 280 insertions, 2 deletions
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<PathBuf> {
+pub(crate) fn upstream_composer_bin() -> Option<PathBuf> {
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<PathBuf> {
}
}
-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 @@
+<?php
+
+namespace ShirabeTest\CommandProvider;
+
+use Composer\Command\BaseCommand;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class GreetCommand extends BaseCommand
+{
+ protected function configure(): void
+ {
+ $this
+ ->setName('greet')
+ ->setAliases(['hi'])
+ ->setDescription('Greets someone from a plugin-provided command.')
+ ->setHelp('The <info>greet</info> 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 @@
+<?php
+
+namespace ShirabeTest\CommandProvider;
+
+use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
+
+class GreetCommandProvider implements CommandProviderCapability
+{
+ public function getCommands(): array
+ {
+ return [new GreetCommand()];
+ }
+}
diff --git a/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/Plugin.php b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/Plugin.php
new file mode 100644
index 00000000..fc708b1f
--- /dev/null
+++ b/crates/shirabe/tests/plugin/fixtures/e2e-command/plugin/src/Plugin.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace ShirabeTest\CommandProvider;
+
+use Composer\Composer;
+use Composer\IO\IOInterface;
+use Composer\Plugin\Capable;
+use Composer\Plugin\PluginInterface;
+
+class Plugin implements PluginInterface, Capable
+{
+ 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 function getCapabilities(): array
+ {
+ return [
+ 'Composer\Plugin\Capability\CommandProvider' => 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;