From 93c4568578690c66ef0ed6e219407833aa715eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFck=20Piera?= Date: Fri, 5 Apr 2024 10:12:09 +0200 Subject: [PATCH 1/2] Fix multiple remote imports with default version --- CHANGELOG.md | 1 + src/Import/Remote/PackageImporter.php | 8 ++++--- ...mportSamePackageWithDefaultVersionTest.php | 18 ++++++++++++++ ...ckageWithDefaultVersionTest.php.output.txt | 24 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php create mode 100644 tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php.output.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index dd79d983..ecfc3379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Deprecate `Castor\GlobalHelper` class. There are no replacements. Use raw functions instead. * Import and load task from remote import automatically +* Fix multiple remote imports of the same package with default version ## 0.15.0 (2024-04-03) diff --git a/src/Import/Remote/PackageImporter.php b/src/Import/Remote/PackageImporter.php index 4599c148..d9276d33 100644 --- a/src/Import/Remote/PackageImporter.php +++ b/src/Import/Remote/PackageImporter.php @@ -32,8 +32,10 @@ public function importPackage(string $scheme, string $package, ?string $file = n throw new RemoteNotAllowed('Remote imports are disabled.'); } - if (isset($this->imports[$package]) && $this->imports[$package]->version !== $version) { - throw new ImportError(sprintf('The package "%s" is already required in version "%s", could not require it in version "%s"', $package, $this->imports[$package]->version, $version)); + $requiredVersion = $version ?? '*'; + + if (isset($this->imports[$package]) && $this->imports[$package]->version !== $requiredVersion) { + throw new ImportError(sprintf('The package "%s" is already required in version "%s", could not require it in version "%s"', $package, $this->imports[$package]->version, $requiredVersion)); } if (!preg_match('#^(?[^/]+)/(?[^/]+)$#', $package)) { @@ -45,7 +47,7 @@ public function importPackage(string $scheme, string $package, ?string $file = n throw new InvalidImportFormat('The "source" argument is not supported for Composer/Packagist packages.'); } - $this->importPackageWithComposer($package, version: $version ?? '*', repositoryUrl: $vcs, file: $file); + $this->importPackageWithComposer($package, version: $requiredVersion, repositoryUrl: $vcs, file: $file); return; } diff --git a/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php b/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php new file mode 100644 index 00000000..4b1a7d8c --- /dev/null +++ b/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php @@ -0,0 +1,18 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/valid/import-same-package-with-default-version', needRemote: true); + + $this->assertSame(0, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + $this->assertSame('', $process->getErrorOutput()); + } +} diff --git a/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php.output.txt b/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php.output.txt new file mode 100644 index 00000000..cebf6622 --- /dev/null +++ b/tests/Examples/Generated/ImportSamePackageWithDefaultVersionTest.php.output.txt @@ -0,0 +1,24 @@ + Downloading remote packages + Remote packages imported + +castor v0.15.0 + +Usage: + command [options] [arguments] + +Options: + -h, --help Display help for the given command. When no command is given display help for the list command + -q, --quiet Do not output any message + -V, --version Display this application version + --ansi|--no-ansi Force (or disable --no-ansi) ANSI output + -n, --no-interaction Do not ask any interactive question + --no-remote Skip the import of all remote remote packages + --update-remotes Force the update of remote packages + -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug + +Available commands: + completion Dump the shell completion script + help Display help for a command + list List commands + pyrech + pyrech:hello-example Hello from example! From 4227b51e53f2073be31b73d3d5cdbaf3e7557937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFck=20Piera?= Date: Fri, 5 Apr 2024 10:49:28 +0200 Subject: [PATCH 2/2] Beautify tests generation and fix list test --- bin/generate-tests.php | 43 +++++++++++++++---- .../castor.php | 6 +++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 tests/Examples/fixtures/valid/import-same-package-with-default-version/castor.php diff --git a/bin/generate-tests.php b/bin/generate-tests.php index 36e0dfda..ca388f27 100755 --- a/bin/generate-tests.php +++ b/bin/generate-tests.php @@ -23,6 +23,8 @@ $fs->remove(__DIR__ . '/../tests/Examples/Generated'); $fs->mkdir(__DIR__ . '/../tests/Examples/Generated'); +displayTitle('Retrieving example tasks'); + $application = ApplicationFactory::create(); $application->setAutoExit(false); $application @@ -36,6 +38,10 @@ throw new RuntimeException('Could not get the list of commands. You probably break something:' . $json, previous: $e); } +echo "\nDone.\n"; + +displayTitle('Generating tests for example tasks'); + $taskFilterList = [ '_complete', 'completion', @@ -89,6 +95,7 @@ 'pyrech:foobar', ]; $optionFilterList = array_flip(['help', 'quiet', 'verbose', 'version', 'ansi', 'no-ansi', 'no-interaction', 'context', 'no-remote', 'update-remotes']); + foreach ($applicationDescription['commands'] as $task) { if (in_array($task['name'], $taskFilterList, true)) { continue; @@ -123,15 +130,28 @@ add_test($args, $class); } -$dirs = (new Finder()) - ->in($basePath = __DIR__ . '/../tests/Examples/fixtures/broken') - ->depth(1) -; -foreach ($dirs as $dir) { - $class = u($dir->getRelativePath())->camel()->title()->toString(); - add_test([], $class, '{{ base }}/tests/Examples/fixtures/broken/' . $dir->getRelativePath(), true); +echo "\nDone.\n"; + +displayTitle('Generating tests for fixtures'); + +foreach (['broken', 'valid'] as $type) { + $dirs = (new Finder()) + ->in($basePath = __DIR__ . '/../tests/Examples/fixtures/' . $type) + ->depth(1) + ; + + foreach ($dirs as $dir) { + echo "Generating test for {$type} fixture " . basename(dirname($dir)) . "\n"; + + $class = u($dir->getRelativePath())->camel()->title()->toString(); + add_test([], $class, '{{ base }}/tests/Examples/fixtures/' . $type . '/' . $dir->getRelativePath(), true); + } } +echo "\nDone.\n"; + +displayTitle('Generating additional tests'); + add_test(['args:passthru', 'a', 'b', '--no', '--foo', 'bar', '-x'], 'ArgPassthruExpanded'); add_test(['context:context', '--context', 'dynamic'], 'ContextContextDynamic'); add_test(['context:context', '--context', 'my_default', '-v'], 'ContextContextMyDefault'); @@ -140,7 +160,7 @@ add_test(['context:context', '--context', 'production'], 'ContextContextProduction'); add_test(['context:context', '--context', 'run'], 'ContextContextRun'); add_test(['enabled:hello', '--context', 'production'], 'EnabledInProduction'); -add_test(['list', '--raw', '--format', 'txt', '--short'], 'List', skipOnBinary: true); +add_test(['list', '--raw', '--format', 'txt', '--short'], 'List', needRemote: true, skipOnBinary: true); // Transient test, disabled for now // add_test(['parallel:sleep', '--sleep5', '0', '--sleep7', '0', '--sleep10', '0'], 'ParallelSleep'); add_test(['symfony:greet', 'World', '--french', 'COUCOU', '--punctuation', '!'], 'SymfonyGreet', skipOnBinary: true); @@ -154,6 +174,8 @@ add_test(['list'], 'LayoutWithFolder', '{{ base }}/tests/Examples/fixtures/layout/with-folder'); add_test(['list'], 'LayoutWithOldFolder', '{{ base }}/tests/Examples/fixtures/layout/with-old-folder'); +echo "\nDone.\n"; + function add_test(array $args, string $class, ?string $cwd = null, bool $needRemote = false, bool $skipOnBinary = false) { $class .= 'Test'; @@ -209,6 +231,11 @@ function add_test(array $args, string $class, ?string $cwd = null, bool $needRem } } +function displayTitle(string $title) +{ + echo "\n-- {$title} --\n\n"; +} + __halt_compiler();