chore: fix package manager tests (#3407)

This commit is contained in:
Sami Mazouz 2022-04-25 13:31:16 +01:00 committed by GitHub
parent d465fd27bc
commit b14c0780d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 27 deletions

View File

@ -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

View File

@ -23,7 +23,7 @@
}, },
"require": { "require": {
"flarum/core": "^1.0.0", "flarum/core": "^1.0.0",
"composer/composer": "^2.0" "composer/composer": "^2.3"
}, },
"require-dev": { "require-dev": {
"flarum/testing": "^1.0.0", "flarum/testing": "^1.0.0",
@ -86,7 +86,7 @@
"test:integration": "phpunit -c tests/phpunit.integration.xml", "test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": [ "test:setup": [
"@php tests/integration/setup.php", "@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": { "scripts-descriptions": {

View File

@ -58,7 +58,7 @@ class CheckForUpdatesHandler
$actor->assertAdmin(); $actor->assertAdmin();
$firstOutput = $this->runComposerCommand(false); $firstOutput = $this->runComposerCommand(false);
$firstOutput = json_decode($firstOutput, true); $firstOutput = json_decode($this->cleanJson($firstOutput), true);
$majorUpdates = false; $majorUpdates = false;
@ -71,7 +71,7 @@ class CheckForUpdatesHandler
if ($majorUpdates) { if ($majorUpdates) {
$secondOutput = $this->runComposerCommand(true); $secondOutput = $this->runComposerCommand(true);
$secondOutput = json_decode($secondOutput, true); $secondOutput = json_decode($this->cleanJson($secondOutput), true);
} }
if (! isset($secondOutput)) { if (! isset($secondOutput)) {
@ -101,6 +101,15 @@ class CheckForUpdatesHandler
->save(); ->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 * @throws ComposerCommandFailedException
*/ */

View File

@ -15,27 +15,11 @@ class SetupComposer
{ {
use UsesTmpDir; use UsesTmpDir;
private $config = [ 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/*',
]
]
];
public function __construct(array $config = null) public function __construct(array $config = null)
{ {
$this->config = array_merge($this->config, $config ?? []); $this->config = $config;
} }
public function run() public function run()
@ -44,7 +28,7 @@ class SetupComposer
$composerLock = $this->tmpDir().'/composer.lock'; $composerLock = $this->tmpDir().'/composer.lock';
$packages = $this->tmpDir().'/packages'; $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)) { if (! file_exists($packages)) {
mkdir($packages); mkdir($packages);
@ -53,5 +37,28 @@ class SetupComposer
if (file_exists($composerLock)) { if (file_exists($composerLock)) {
unlink($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 ?? []);
} }
} }

View File

@ -19,10 +19,7 @@
<testsuites> <testsuites>
<testsuite name="Flarum Integration Tests"> <testsuite name="Flarum Integration Tests">
<directory suffix="Test.php">./integration</directory> <directory suffix="Test.php">./integration</directory>
<exclude>./integration/tmp</exclude> <exclude>./integration/tmp</exclude>
</testsuite> </testsuite>
</testsuites> </testsuites>
<php>
<env name="FLARUM_TEST_TMP_DIR_LOCAL" value="tests/integration/tmp" force="true" />
</php>
</phpunit> </phpunit>