Skip to content

Commit

Permalink
Change import format version and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrech committed Jun 26, 2023
1 parent c0ae522 commit d661b23
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
4 changes: 2 additions & 2 deletions doc/13-remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ To import functions from a GitHub repository, pass a path to the `import()`
function, formatted like this:

```
github://<user>/<repository>/<version>/<path of the php file to import>
github://<user>/<repository>/<path of the php file to import>@<version>
```

Here is an example:

```php
use function Castor\import;

import('github://pyrech/castor-setup-php/main/castor.php');
import('github://pyrech/castor-setup-php/castor.php@main');

#[AsTask()]
function hello(): void
Expand Down
2 changes: 1 addition & 1 deletion examples/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use function Castor\import;

import('github://pyrech/castor-setup-php/main');
import('github://pyrech/castor-setup-php/castor.php@main');

#[AsTask(description: 'Use a function imported from a remote repository')]
function hello(): void
Expand Down
13 changes: 7 additions & 6 deletions src/Remote/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
/** @internal */
class Import
{
public static function importFunctions(string $scheme, string $url): string
public static function importFunctions(string $scheme, string $url, bool $dryRun = false): string
{
if ('github' === $scheme) {
if (!preg_match('#^github://(?<organization>[^/]+)/(?<repository>[^/]+)/(?<version>[^/]+)(?<function_path>.*)$#', $url, $matches)) {
throw new InvalidImportUrl('The import path from GitHub repository must be formatted like this: "github://<organization>/<repository>/<version>/<function_path>".');
if (!preg_match('#^(?<organization>[^/]+)/(?<repository>[^/]+)(?<function_path>[^@]*)@(?<version>.+)$#', $url, $matches)) {
throw new InvalidImportUrl('The import path from GitHub repository must be formatted like this: "github://<organization>/<repository>/<function_path>@<version>".');
}

$path = self::importFunctionsFromGitRepository(
'github.com',
sprintf('%s/%s', $matches['organization'], $matches['repository']),
$matches['version'],
$matches['function_path'] ?? '/castor.php',
$dryRun,
);

log('Using functions from remote resource.', 'info', [
Expand All @@ -37,16 +38,16 @@ public static function importFunctions(string $scheme, string $url): string
return $path;
}

throw new InvalidImportUrl(sprintf('The import path scheme "%s" is not supported.', $scheme));
throw new InvalidImportUrl(sprintf('The import scheme "%s" is not supported.', $scheme));
}

private static function importFunctionsFromGitRepository(string $domain, string $repository, string $version, string $functionPath): string
private static function importFunctionsFromGitRepository(string $domain, string $repository, string $version, string $functionPath, bool $dryRun): string
{
self::ensureTrustedResource($domain . '/' . $repository);

$dir = GlobalHelper::getHomeDirectory() . '/remote/' . $domain . '/' . $repository . '/' . $version;

if (!is_dir($dir)) {
if (!is_dir($dir) && !$dryRun) {
log("Importing functions in path {$functionPath} from {$domain}/{$repository} (version {$version})");

fs()->mkdir($dir);
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ function import(string $path): void

if ($scheme) {
try {
$path = Import::importFunctions($scheme, $path);
$path = Import::importFunctions($scheme, mb_substr($path, mb_strlen($scheme) + 3));
} catch (InvalidImportUrl $e) {
throw fix_exception(new \InvalidArgumentException($e->getMessage(), 0, $e));
} catch (NotTrusted $e) {
Expand Down
55 changes: 55 additions & 0 deletions tests/Remote/ImportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Castor\Tests\Remote;

use Castor\GlobalHelper;
use Castor\Remote\Exception\InvalidImportUrl as InvalidImportUrlAlias;
use Castor\Remote\Exception\NotTrusted;
use Castor\Remote\Import;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\NullOutput;

class ImportTest extends TestCase
{
public function testInvalidScheme(): void
{
$this->expectException(InvalidImportUrlAlias::class);
$this->expectExceptionMessage('The import scheme "foobar" is not supported.');

Import::importFunctions('foobar', 'test-url', dryRun: true);
}

public function testInvalidGithub(): void
{
$this->expectException(InvalidImportUrlAlias::class);
$this->expectExceptionMessage('The import path from GitHub repository must be formatted like this: "github://<organization>/<repository>/<function_path>@<version>".');

Import::importFunctions('github', 'test-url', dryRun: true);
}

public function testValidGithubNotTrusted(): void
{
$this->expectException(NotTrusted::class);
$this->expectExceptionMessage('The remote resource github.com/pyrech/castor-setup-php is not trusted.');

GlobalHelper::setInput(new ArgvInput(['castor', '--no-trust']));
GlobalHelper::setOutput(new NullOutput());
GlobalHelper::setupDefaultCache();

Import::importFunctions('github', 'pyrech/castor-setup-php/castor.php@main', dryRun: true);
}

public function testValidGithubTrusted(): void
{
GlobalHelper::setInput(new ArgvInput(['castor', '--trust']));
GlobalHelper::setOutput(new NullOutput());
GlobalHelper::setupDefaultCache();
GlobalHelper::setLogger(new Logger('test'));

$path = Import::importFunctions('github', 'pyrech/castor-setup-php/castor.php@main', dryRun: true);

$this->assertStringContainsString('.castor/remote/github.com/pyrech/castor-setup-php/main/castor.php', $path);
}
}

0 comments on commit d661b23

Please sign in to comment.