From b14c0780d2c36f8f225024f1d92e4992d7011f30 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 25 Apr 2022 13:31:16 +0100 Subject: [PATCH] chore: fix package manager tests (#3407) --- .../flarum-package-manager-backend.yml | 15 +++++++ extensions/package-manager/composer.json | 4 +- .../src/Command/CheckForUpdatesHandler.php | 13 +++++- .../tests/integration/SetupComposer.php | 45 +++++++++++-------- .../tests/phpunit.integration.xml | 5 +-- 5 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/flarum-package-manager-backend.yml diff --git a/.github/workflows/flarum-package-manager-backend.yml b/.github/workflows/flarum-package-manager-backend.yml new file mode 100644 index 000000000..bb82c74d3 --- /dev/null +++ b/.github/workflows/flarum-package-manager-backend.yml @@ -0,0 +1,15 @@ +name: Package Manager PHP + +on: [workflow_dispatch, push, pull_request] + +# The reusable workflow definitions will be moved to the `flarum/framework` repo soon. +# This will break your current script. +# When this happens, run `flarum-cli audit infra --fix` to update your infrastructure. + +jobs: + run: + uses: flarum/.github/.github/workflows/REUSABLE_backend.yml@main + with: + enable_backend_testing: true + + backend_directory: ./extensions/package-manager diff --git a/extensions/package-manager/composer.json b/extensions/package-manager/composer.json index d8e7493d0..449c0a4e7 100755 --- a/extensions/package-manager/composer.json +++ b/extensions/package-manager/composer.json @@ -23,7 +23,7 @@ }, "require": { "flarum/core": "^1.0.0", - "composer/composer": "^2.0" + "composer/composer": "^2.3" }, "require-dev": { "flarum/testing": "^1.0.0", @@ -86,7 +86,7 @@ "test:integration": "phpunit -c tests/phpunit.integration.xml", "test:setup": [ "@php tests/integration/setup.php", - "cd $FLARUM_TEST_TMP_DIR_LOCAL && composer install" + "cd ${FLARUM_TEST_TMP_DIR_LOCAL:-${FLARUM_TEST_TMP_DIR:-./tests/integration/tmp}} && composer install" ] }, "scripts-descriptions": { diff --git a/extensions/package-manager/src/Command/CheckForUpdatesHandler.php b/extensions/package-manager/src/Command/CheckForUpdatesHandler.php index 2b396a0c8..8bc9a1acd 100755 --- a/extensions/package-manager/src/Command/CheckForUpdatesHandler.php +++ b/extensions/package-manager/src/Command/CheckForUpdatesHandler.php @@ -58,7 +58,7 @@ class CheckForUpdatesHandler $actor->assertAdmin(); $firstOutput = $this->runComposerCommand(false); - $firstOutput = json_decode($firstOutput, true); + $firstOutput = json_decode($this->cleanJson($firstOutput), true); $majorUpdates = false; @@ -71,7 +71,7 @@ class CheckForUpdatesHandler if ($majorUpdates) { $secondOutput = $this->runComposerCommand(true); - $secondOutput = json_decode($secondOutput, true); + $secondOutput = json_decode($this->cleanJson($secondOutput), true); } if (! isset($secondOutput)) { @@ -101,6 +101,15 @@ class CheckForUpdatesHandler ->save(); } + /** + * Composer can sometimes return text above the JSON. + * This method tries to remove such occurences. + */ + protected function cleanJson(string $composerOutput): string + { + return preg_replace('/^[^{]+\n({.*)/ms', '$1', $composerOutput); + } + /** * @throws ComposerCommandFailedException */ diff --git a/extensions/package-manager/tests/integration/SetupComposer.php b/extensions/package-manager/tests/integration/SetupComposer.php index 9a1080ce4..fb021faf9 100644 --- a/extensions/package-manager/tests/integration/SetupComposer.php +++ b/extensions/package-manager/tests/integration/SetupComposer.php @@ -15,27 +15,11 @@ class SetupComposer { use UsesTmpDir; - private $config = [ - 'require' => [ - 'flarum/core' => '1.0.0', - 'flarum/tags' => '1.0.3', - 'flarum/lang-english' => '*', - ], - 'config' => [ - 'preferred-install' => 'dist', - 'sort-packages' => true, - ], - 'repositories' => [ - [ - 'type' => 'path', - 'url' => __DIR__.'/tmp/packages/*', - ] - ] - ]; + private $config; public function __construct(array $config = null) { - $this->config = array_merge($this->config, $config ?? []); + $this->config = $config; } public function run() @@ -44,7 +28,7 @@ class SetupComposer $composerLock = $this->tmpDir().'/composer.lock'; $packages = $this->tmpDir().'/packages'; - file_put_contents($composerJson, json_encode($this->config, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + file_put_contents($composerJson, json_encode($this->getConfig(), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); if (! file_exists($packages)) { mkdir($packages); @@ -53,5 +37,28 @@ class SetupComposer if (file_exists($composerLock)) { unlink($composerLock); } + + echo 'composer.json created with testing packages directory.'; + } + + private function getConfig(): array + { + return array_merge([ + 'require' => [ + 'flarum/core' => '1.0.0', + 'flarum/tags' => '1.0.3', + 'flarum/lang-english' => '*', + ], + 'config' => [ + 'preferred-install' => 'dist', + 'sort-packages' => true, + ], + 'repositories' => [ + [ + 'type' => 'path', + 'url' => realpath($this->tmpDir()).'/packages/*', + ] + ] + ], $this->config ?? []); } } diff --git a/extensions/package-manager/tests/phpunit.integration.xml b/extensions/package-manager/tests/phpunit.integration.xml index 2d2b64c39..89f91a4d1 100644 --- a/extensions/package-manager/tests/phpunit.integration.xml +++ b/extensions/package-manager/tests/phpunit.integration.xml @@ -19,10 +19,7 @@ ./integration - ./integration/tmp + ./integration/tmp - - -