diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-06 20:04:25 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-06 20:04:25 +0900 |
| commit | 3f525f228052ad04e6f9da6ff160bef803d079bd (patch) | |
| tree | 3fe2d078e6a30b12ce041b1b3f1e589bc47cf8f3 /crates/shirabe/tests | |
| parent | d9dca94603766712b5989ea8169c9e286d27a60c (diff) | |
| download | php-shirabe-3f525f228052ad04e6f9da6ff160bef803d079bd.tar.gz php-shirabe-3f525f228052ad04e6f9da6ff160bef803d079bd.tar.zst php-shirabe-3f525f228052ad04e6f9da6ff160bef803d079bd.zip | |
feat(plugin): let links and release dates cross the RPC boundary
The link getters and setters and the release date accessors were explicit
errors for every non-empty value, because an immutable value has no entity
to point a handle at. They now cross as materialized values: the descriptor
names the real class and the constructor arguments, and each side builds a
genuine instance of its own.
The semver constraint a link holds is encoded structurally rather than
re-parsed from its string form, so the pretty strings and the conjunctive
flag survive the crossing.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/tests')
4 files changed, 266 insertions, 0 deletions
diff --git a/crates/shirabe/tests/plugin/fixtures/values-v1/Values/Plugin.php b/crates/shirabe/tests/plugin/fixtures/values-v1/Values/Plugin.php new file mode 100644 index 00000000..685aa903 --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/values-v1/Values/Plugin.php @@ -0,0 +1,85 @@ +<?php + +namespace Values; + +use Composer\Composer; +use Composer\IO\IOInterface; +use Composer\Package\Link; +use Composer\Plugin\PluginInterface; +use Composer\Semver\Constraint\Constraint; +use Composer\Semver\Constraint\ConstraintInterface; +use Composer\Semver\Constraint\MatchAllConstraint; +use Composer\Semver\Constraint\MatchNoneConstraint; +use Composer\Semver\Constraint\MultiConstraint; + +class Plugin implements PluginInterface +{ + public function activate(Composer $composer, IOInterface $io) + { + $package = $composer->getPackage(); + + foreach ($package->getRequires() as $name => $link) { + if (!$link instanceof Link) { + throw new \RuntimeException('not a Link: ' . get_class($link)); + } + $io->write(sprintf( + '%s | %s | %s | %s | %s | %s', + $name, + $link->getSource(), + $link->getTarget(), + $link->getDescription(), + $link->getPrettyConstraint(), + self::render($link->getConstraint()) + )); + } + + $package->setRequires([ + 'bar/plain' => new Link('dummy/root', 'bar/plain', self::pretty(new Constraint('==', '2.0.0.0'), '2.0'), Link::TYPE_REQUIRE, '2.0'), + 'bar/multi' => new Link('dummy/root', 'bar/multi', new MultiConstraint([ + new Constraint('<', '1.0.0.0'), + new Constraint('>=', '3.0.0.0'), + ], false), Link::TYPE_DEV_REQUIRE, '<1.0 || >=3.0'), + 'bar/all' => new Link('dummy/root', 'bar/all', new MatchAllConstraint(), Link::TYPE_REQUIRE, '*'), + 'bar/none' => new Link('dummy/root', 'bar/none', self::pretty(new MatchNoneConstraint(), 'nothing'), Link::TYPE_REQUIRE, ''), + ]); + + $package->setReleaseDate(new \DateTimeImmutable('2024-03-04 05:06:07.123456', new \DateTimeZone('Asia/Tokyo'))); + $io->write('release date: ' . $package->getReleaseDate()->format('Y-m-d\TH:i:s.uP')); + } + + public function deactivate(Composer $composer, IOInterface $io) + { + } + + public function uninstall(Composer $composer, IOInterface $io) + { + } + + private static function pretty(ConstraintInterface $constraint, string $prettyString): ConstraintInterface + { + $constraint->setPrettyString($prettyString); + + return $constraint; + } + + private static function render(ConstraintInterface $constraint): string + { + if ($constraint instanceof Constraint) { + $shape = sprintf('Constraint(%s %s)', $constraint->getOperator(), $constraint->getVersion()); + } elseif ($constraint instanceof MultiConstraint) { + $shape = sprintf( + 'MultiConstraint(%s: %s)', + $constraint->isConjunctive() ? 'and' : 'or', + implode(', ', array_map([self::class, 'render'], $constraint->getConstraints())) + ); + } elseif ($constraint instanceof MatchAllConstraint) { + $shape = 'MatchAllConstraint()'; + } elseif ($constraint instanceof MatchNoneConstraint) { + $shape = 'MatchNoneConstraint()'; + } else { + throw new \RuntimeException('unexpected constraint: ' . get_class($constraint)); + } + + return $shape . ' pretty=' . $constraint->getPrettyString(); + } +} diff --git a/crates/shirabe/tests/plugin/fixtures/values-v1/composer.json b/crates/shirabe/tests/plugin/fixtures/values-v1/composer.json new file mode 100644 index 00000000..d4e5861e --- /dev/null +++ b/crates/shirabe/tests/plugin/fixtures/values-v1/composer.json @@ -0,0 +1,12 @@ +{ + "name": "values-v1", + "version": "1.0.0", + "type": "composer-plugin", + "autoload": { "psr-0": { "Values": "" } }, + "extra": { + "class": "Values\\Plugin" + }, + "require": { + "composer-plugin-api": "^2.0" + } +} diff --git a/crates/shirabe/tests/plugin/main.rs b/crates/shirabe/tests/plugin/main.rs index 0b404ee9..f5a29ba6 100644 --- a/crates/shirabe/tests/plugin/main.rs +++ b/crates/shirabe/tests/plugin/main.rs @@ -10,3 +10,4 @@ mod e2e_installers_test; mod e2e_normalize_test; mod plugin_installer_test; mod subscriber_test; +mod value_round_trip_test; diff --git a/crates/shirabe/tests/plugin/value_round_trip_test.rs b/crates/shirabe/tests/plugin/value_round_trip_test.rs new file mode 100644 index 00000000..3e01422a --- /dev/null +++ b/crates/shirabe/tests/plugin/value_round_trip_test.rs @@ -0,0 +1,168 @@ +//! Shirabe-specific integration tests for the immutable values that cross the plugin boundary +//! as real PHP objects: `Composer\Package\Link` with its `composer/semver` constraint, and the +//! `\DateTimeInterface` release date. Upstream Composer has no test for this (its plugins run +//! in-process), so the fixture `fixtures/values-v1` is Shirabe-owned. + +use crate::async_runtime::run; +use crate::plugin_installer_test::{lock_php_worker, new_installer, php_runtime_available, set_up}; +use indexmap::IndexMap; +use shirabe::installer::InstallerInterface; +use shirabe::package::Link; +use shirabe::package::PackageInterfaceHandle; +use shirabe::package::loader::{ArrayLoader, JsonLoader, JsonLoaderInput}; +use shirabe_semver::constraint::{ + AnyConstraint, MatchAllConstraint, MatchNoneConstraint, MultiConstraint, SimpleConstraint, +}; + +fn values_fixture_package() -> PackageInterfaceHandle { + let loader = JsonLoader::new(Box::new(ArrayLoader::new(None, false))); + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests/plugin/fixtures/values-v1/composer.json"); + loader + .load(JsonLoaderInput::String( + path.canonicalize().unwrap().to_str().unwrap().to_string(), + )) + .unwrap() +} + +fn simple(operator: &str, version: &str, pretty_string: Option<&str>) -> AnyConstraint { + AnyConstraint::Simple(SimpleConstraint::new( + operator.to_string(), + version.to_string(), + pretty_string.map(str::to_string), + )) +} + +fn link(target: &str, constraint: AnyConstraint, pretty_constraint: &str) -> Link { + Link::new( + "dummy/root".to_string(), + target.to_string(), + constraint, + Some(Link::TYPE_REQUIRE.to_string()), + pretty_constraint.to_string(), + ) +} + +/// Every constraint shape the codec knows, so the rendering the plugin writes covers all four +/// classes and both the set and the unset pretty string. +fn seeded_requires() -> IndexMap<String, Link> { + let mut requires = IndexMap::new(); + requires.insert( + "foo/simple".to_string(), + link("foo/simple", simple(">=", "1.0.0.0", None), ">=1.0"), + ); + requires.insert( + "foo/multi".to_string(), + link( + "foo/multi", + AnyConstraint::Multi(MultiConstraint::new( + vec![ + simple(">=", "1.0.0.0", None), + simple("<", "2.0.0.0", Some("<2.0")), + ], + true, + Some("^1.0".to_string()), + )), + "^1.0", + ), + ); + requires.insert( + "foo/all".to_string(), + link( + "foo/all", + AnyConstraint::MatchAll(MatchAllConstraint::new(None)), + "*", + ), + ); + requires.insert( + "foo/none".to_string(), + link( + "foo/none", + AnyConstraint::MatchNone(MatchNoneConstraint::new(Some("nothing".to_string()))), + "", + ), + ); + requires +} + +fn describe(constraint: &AnyConstraint) -> String { + let shape = match constraint { + AnyConstraint::Simple(c) => { + format!("Constraint({} {})", c.get_operator(), c.get_version()) + } + AnyConstraint::Multi(c) => format!( + "MultiConstraint({}: {})", + if c.is_conjunctive() { "and" } else { "or" }, + c.get_constraints() + .iter() + .map(describe) + .collect::<Vec<_>>() + .join(", ") + ), + AnyConstraint::MatchAll(_) => "MatchAllConstraint()".to_string(), + AnyConstraint::MatchNone(_) => "MatchNoneConstraint()".to_string(), + }; + format!("{shape} pretty={}", constraint.get_pretty_string()) +} + +fn describe_links(links: &IndexMap<String, Link>) -> Vec<String> { + links + .iter() + .map(|(name, link)| { + format!( + "{name} | {} | {} | {} | {} | {}", + link.get_source(), + link.get_target(), + link.get_description(), + link.get_pretty_constraint(), + describe(link.get_constraint()) + ) + }) + .collect() +} + +#[test] +fn test_links_and_release_date_round_trip_through_the_plugin() { + if !php_runtime_available() { + return; + } + let _worker = lock_php_worker(); + let set_up = set_up(); + let package = set_up.composer.borrow().get_package().clone(); + package.set_requires(seeded_requires()); + + let installer = new_installer(&set_up); + set_up.pm.borrow_mut().load_installed_plugins().unwrap(); + run(installer.install(&set_up.repository, values_fixture_package())).unwrap(); + + // What the plugin saw: each seeded link rebuilt in the child as a real Link over a real + // composer/semver constraint. + assert_eq!( + "foo/simple | dummy/root | foo/simple | requires | >=1.0 | Constraint(>= 1.0.0.0) pretty=>= 1.0.0.0\n\ + foo/multi | dummy/root | foo/multi | requires | ^1.0 | MultiConstraint(and: Constraint(>= 1.0.0.0) pretty=>= 1.0.0.0, Constraint(< 2.0.0.0) pretty=<2.0) pretty=^1.0\n\ + foo/all | dummy/root | foo/all | requires | * | MatchAllConstraint() pretty=*\n\ + foo/none | dummy/root | foo/none | requires | | MatchNoneConstraint() pretty=nothing\n\ + release date: 2024-03-03T20:06:07.123456+00:00\n", + set_up.io.borrow().get_output() + ); + + // What the plugin wrote back: links it built PHP-side, decoded into Rust values. + assert_eq!( + vec![ + "bar/plain | dummy/root | bar/plain | requires | 2.0 | Constraint(== 2.0.0.0) pretty=2.0", + "bar/multi | dummy/root | bar/multi | requires (for development) | <1.0 || >=3.0 | MultiConstraint(or: Constraint(< 1.0.0.0) pretty=< 1.0.0.0, Constraint(>= 3.0.0.0) pretty=>= 3.0.0.0) pretty=[< 1.0.0.0 || >= 3.0.0.0]", + "bar/all | dummy/root | bar/all | requires | * | MatchAllConstraint() pretty=*", + "bar/none | dummy/root | bar/none | requires | | MatchNoneConstraint() pretty=nothing", + ], + describe_links(&package.get_requires()) + ); + // The plugin wrote an Asia/Tokyo instant with microseconds; both survive the crossing, + // and the child sees the same instant back in UTC. + assert_eq!( + "2024-03-03T20:06:07.123456Z", + package + .get_release_date() + .unwrap() + .to_rfc3339_opts(chrono::SecondsFormat::Micros, true) + ); +} |
