Skip to content

Commit

Permalink
Merge pull request #386 from jolicode/remote-import-default-version
Browse files Browse the repository at this point in the history
Fix multiple remote imports with default version
  • Loading branch information
lyrixx committed Apr 5, 2024
2 parents 67fdaf1 + 4227b51 commit 33e0882
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
43 changes: 35 additions & 8 deletions bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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';
Expand Down Expand Up @@ -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();
<?php

Expand Down
8 changes: 5 additions & 3 deletions src/Import/Remote/PackageImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('#^(?<organization>[^/]+)/(?<repository>[^/]+)$#', $package)) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Castor\Tests\Examples\Generated;

use Castor\Tests\TaskTestCase;

class ImportSamePackageWithDefaultVersionTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->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());
}
}
Original file line number Diff line number Diff line change
@@ -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!
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use function Castor\import;

import('composer://pyrech/castor-example');
import('composer://pyrech/castor-example', file: 'castor.php');

0 comments on commit 33e0882

Please sign in to comment.