From 357dfc53c99be01330a32861f0597865bd7e19e5 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 5 Oct 2024 10:52:53 +0800 Subject: [PATCH] Add full Downloader tests --- src/SPC/store/Downloader.php | 4 ++ tests/SPC/store/DownloaderTest.php | 90 ++++++++++++++++++++++++++---- tests/mock/SPC_store.php | 47 +++++++++++++++- 3 files changed, 127 insertions(+), 14 deletions(-) diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index 363fe344..f01519bf 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -182,6 +182,7 @@ class Downloader * * @throws FileSystemException * @throws RuntimeException + * @throws WrongUsageException */ public static function downloadFile(string $name, string $url, string $filename, ?string $move_path = null, int $lock_as = SPC_LOCK_SOURCE): void { @@ -220,6 +221,7 @@ class Downloader * * @throws FileSystemException * @throws RuntimeException + * @throws WrongUsageException */ public static function downloadGit(string $name, string $url, string $branch, ?string $move_path = null, int $retry = 0, int $lock_as = SPC_LOCK_SOURCE): void { @@ -381,6 +383,7 @@ class Downloader * @param int $lock_as Lock source type (default: SPC_LOCK_SOURCE) * @throws DownloaderException * @throws FileSystemException + * @throws WrongUsageException */ public static function downloadSource(string $name, ?array $source = null, bool $force = false, int $lock_as = SPC_LOCK_SOURCE): void { @@ -539,6 +542,7 @@ class Downloader * Use curl to download sources from url * * @throws RuntimeException + * @throws WrongUsageException */ public static function curlDown(string $url, string $path, string $method = 'GET', array $headers = [], array $hooks = [], int $retry = 0): void { diff --git a/tests/SPC/store/DownloaderTest.php b/tests/SPC/store/DownloaderTest.php index ff6af17e..6fc11cf1 100644 --- a/tests/SPC/store/DownloaderTest.php +++ b/tests/SPC/store/DownloaderTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace SPC\Tests\store; use PHPUnit\Framework\TestCase; +use SPC\exception\WrongUsageException; use SPC\store\Downloader; /** @@ -24,23 +25,88 @@ class DownloaderTest extends TestCase ); } - public function testDownloadGit() {} + public function testDownloadGit() + { + Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'main'); + $this->assertTrue(true); - public function testDownloadFile() {} + // test keyboard interrupt + try { + Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'SIGINT'); + } catch (WrongUsageException $e) { + $this->assertStringContainsString('interrupted', $e->getMessage()); + return; + } + $this->fail('Expected exception not thrown'); + } - public function testLockSource() {} + public function testDownloadFile() + { + Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'curlDown.exe'); + $this->assertTrue(true); - public function testGetLatestBitbucketTag() {} + // test keyboard interrupt + try { + Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'SIGINT'); + } catch (WrongUsageException $e) { + $this->assertStringContainsString('interrupted', $e->getMessage()); + return; + } + $this->fail('Expected exception not thrown'); + } - public function testGetLatestGithubRelease() {} + public function testLockSource() + { + Downloader::lockSource('fake-file', ['source_type' => 'archive', 'filename' => 'fake-file-name', 'move_path' => 'fake-path', 'lock_as' => 'fake-lock-as']); + $this->assertFileExists(DOWNLOAD_PATH . '/.lock.json'); + $json = json_decode(file_get_contents(DOWNLOAD_PATH . '/.lock.json'), true); + $this->assertIsArray($json); + $this->assertArrayHasKey('fake-file', $json); + $this->assertArrayHasKey('source_type', $json['fake-file']); + $this->assertArrayHasKey('filename', $json['fake-file']); + $this->assertArrayHasKey('move_path', $json['fake-file']); + $this->assertArrayHasKey('lock_as', $json['fake-file']); + $this->assertEquals('archive', $json['fake-file']['source_type']); + $this->assertEquals('fake-file-name', $json['fake-file']['filename']); + $this->assertEquals('fake-path', $json['fake-file']['move_path']); + $this->assertEquals('fake-lock-as', $json['fake-file']['lock_as']); + } - public function testCurlExec() {} + public function testGetLatestBitbucketTag() + { + $this->assertEquals( + 'abc.tar.gz', + Downloader::getLatestBitbucketTag('abc', [ + 'repo' => 'MATCHED/def', + ])[1] + ); + $this->assertEquals( + 'abc-1.0.0.tar.gz', + Downloader::getLatestBitbucketTag('abc', [ + 'repo' => 'abc/def', + ])[1] + ); + } - public function testCurlDown() {} + public function testGetLatestGithubRelease() + { + $this->assertEquals( + 'ghreltest.tar.gz', + Downloader::getLatestGithubRelease('ghrel', [ + 'type' => 'ghrel', + 'repo' => 'ghreltest/ghrel', + 'match' => 'ghreltest.tar.gz', + ])[1] + ); + } - public function testDownloadSource() {} - - public function testGetFromFileList() {} - - public function testDownloadPackage() {} + public function testGetFromFileList() + { + $filelist = Downloader::getFromFileList('fake-filelist', [ + 'url' => 'https://fakecmd.com/filelist', + 'regex' => '/href="(?filelist-(?[^"]+)\\.tar\\.xz)"/', + ]); + $this->assertIsArray($filelist); + $this->assertEquals('filelist-4.7.0.tar.xz', $filelist[1]); + } } diff --git a/tests/mock/SPC_store.php b/tests/mock/SPC_store.php index 18d495c3..6969e128 100644 --- a/tests/mock/SPC_store.php +++ b/tests/mock/SPC_store.php @@ -6,19 +6,62 @@ declare(strict_types=1); namespace SPC\store; +use SPC\exception\RuntimeException; + function f_exec(string $command, mixed &$output, mixed &$result_code): bool { + $result_code = 0; if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/releases')) { $output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/github_api_AOMediaCodec_libavif_releases.json.gz'))); - $result_code = 0; return true; } if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/tarball/v1.1.1')) { $output = explode("\n", "HTTP/1.1 200 OK\r\nContent-Disposition: attachment; filename=AOMediaCodec-libavif-v1.1.1-0-gbb24db0.tar.gz\r\n\r\n"); - $result_code = 0; + return true; + } + if (str_contains($command, 'https://api.bitbucket.org/2.0/repositories/')) { + $output = explode("\n", json_encode(['values' => [['name' => '1.0.0']], 'tag_name' => '1.0.0'])); + return true; + } + if (str_contains($command, 'https://bitbucket.org/')) { + $output = explode("\n", str_contains($command, 'MATCHED') ? "HTTP/2 200 OK\r\ncontent-disposition: attachment; filename=abc.tar.gz\r\n\r\n" : "HTTP/2 200 OK\r\n\r\n"); + return true; + } + if (str_contains($command, 'ghreltest/ghrel')) { + $output = explode("\n", json_encode([[ + 'prerelease' => false, + 'assets' => [ + [ + 'name' => 'ghreltest.tar.gz', + 'browser_download_url' => 'https://fakecmd.com/ghreltest.tar.gz', + ], + ], + ]])); + return true; + } + if (str_contains($command, 'filelist')) { + $output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/filelist.gz'))); return true; } $result_code = -2; $output = null; return false; } + +function f_passthru(string $cmd): bool +{ + if (str_starts_with($cmd, 'git')) { + if (str_contains($cmd, '--branch "SIGINT"')) { + throw new RuntimeException('Interrupt', 2); + } + return true; + } + if (str_contains($cmd, 'https://fakecmd.com/curlDown')) { + if (str_contains($cmd, 'SIGINT')) { + throw new RuntimeException('Interrupt', 2); + } + return true; + } + + throw new RuntimeException('Invalid tests'); +}