From 3aa06cf92890f88e90e99fecf041dc54f26c1a57 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 27 Jul 2021 12:51:03 +0100 Subject: [PATCH 1/3] Allow tests to be run against system Composer --- tests/integration/IntegrationTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/integration/IntegrationTest.php b/tests/integration/IntegrationTest.php index 39bde77..999b6a5 100644 --- a/tests/integration/IntegrationTest.php +++ b/tests/integration/IntegrationTest.php @@ -93,8 +93,17 @@ public function testRun(int $expectedExit, string $expectedOutput, string $condi } $workingDir = temporaryDir(); - $composer = function (...$args) use ($workingDir) { - array_unshift($args, getcwd().'/vendor/bin/composer'); + + // Environment variable instructs tests to use another Composer binary, + // this allows testing with the systems Composer installation. + $composerBin = getenv('COMPOSER_AUDIT_TEST_COMPOSER_BINARY'); + + if (!is_string($composerBin) || is_dir($composerBin) || !is_executable($composerBin)) { + $composerBin = getcwd().'/vendor/bin/composer'; + } + + $composer = function (...$args) use ($composerBin, $workingDir) { + array_unshift($args, $composerBin); return new Process($args, $workingDir, [ 'COMPOSER_HOME' => $workingDir.'/.composer', From 60bcc4eea2b2af679df5bbc527221bdf15eb2bb5 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 27 Jul 2021 13:25:09 +0100 Subject: [PATCH 2/3] Test against local and system Composer --- .github/workflows/main.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 979e887..9275967 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -52,7 +52,13 @@ jobs: run: | vendor/bin/simple-phpunit --version | head -n1 >&2 - vendor/bin/simple-phpunit --verbose $args + echo "::group::Tests (w/ source composer/composer)" + vendor/bin/simple-phpunit --verbose + echo "::endgroup::" + + echo "::group::Tests (w/ phar composer/composer)" + COMPOSER_AUDIT_TEST_COMPOSER_BINARY=$(which composer) vendor/bin/simple-phpunit --verbose + echo "::endgroup::" # Verifies that the plugin entry point still parses on PHP 5.3, users of 5.3 # cannot use this Composer plugin but at least it won't break their Composer. legacy: From 305eb3c21735fa56c56c4732845b21ff509f14b2 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 27 Jul 2021 14:17:06 +0100 Subject: [PATCH 3/3] Load Symfony polyfill packages when running the plugin --- src/ComposerPlugin.php | 2 +- src/PolyfillLoader.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/PolyfillLoader.php diff --git a/src/ComposerPlugin.php b/src/ComposerPlugin.php index 5530670..a4dfd7a 100644 --- a/src/ComposerPlugin.php +++ b/src/ComposerPlugin.php @@ -15,7 +15,7 @@ final class ComposerPlugin implements PluginInterface, Capable { public function activate(Composer $composer, IOInterface $io) { - + PolyfillLoader::load($composer, $io); } public function deactivate(Composer $composer, IOInterface $io) diff --git a/src/PolyfillLoader.php b/src/PolyfillLoader.php new file mode 100644 index 0000000..c5c0a1a --- /dev/null +++ b/src/PolyfillLoader.php @@ -0,0 +1,49 @@ +getRepositoryManager()->getLocalRepository()->getPackages(); + $includeFiles = []; + + foreach ($packages as $package) { + if (strpos($package->getName(), 'symfony/polyfill-') === 0) { + $io->debug(sprintf('PolyfillLoader finding files in: %s', $package->getName())); + $autoload = $package->getAutoload(); + + if (isset($autoload['files'])) { + $installPath = $composer->getInstallationManager()->getInstallPath($package); + + foreach ($autoload['files'] as $file) { + $io->debug(sprintf('PolyfillLoader found: %s %s', $package->getName(), $file)); + $includeFiles[] = $installPath.\DIRECTORY_SEPARATOR.$file; + } + } + } + } + + foreach ($includeFiles as $includeFile) { + if (in_array($includeFile, \get_included_files(), true)) { + $io->debug(sprintf('PolyfillLoader %s is already loaded', $includeFile)); + return; + } + + $io->debug(sprintf('PolyfillLoader loading: %s', $includeFile)); + self::incldueFile($includeFile); + } + } + + private static function incldueFile($path): void + { + require $path; + } +}