From b6f2132565464e3acdfabc8cbf74b3de0cb8234c Mon Sep 17 00:00:00 2001 From: asciito Date: Wed, 11 Oct 2023 17:12:33 -0600 Subject: [PATCH 01/31] fix download helper --- app/helpers.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index 6a05e94..397e431 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -36,9 +36,7 @@ function download(string $url, string $file, bool $force = false): void File::delete($file); - $encoded_url = urlencode($url); - - $response = Http::get($encoded_url, ['stream' => true])->toPsrResponse(); + $response = Http::withOptions(['stream' => true])->get($url)->toPsrResponse(); $body = $response->getBody(); From 93ff097952aced0bac2081cbc6d84b181c5de3a2 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 10:41:39 -0600 Subject: [PATCH 02/31] remove unnecessary files --- app/Commands/.gitkeep | 0 tests/Feature/.gitkeep | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 app/Commands/.gitkeep delete mode 100644 tests/Feature/.gitkeep diff --git a/app/Commands/.gitkeep b/app/Commands/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep deleted file mode 100644 index e69de29..0000000 From 007971aa9ed2462e4d6b711dc4624e2562eb8eaf Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 10:44:12 -0600 Subject: [PATCH 03/31] build: add dependencies for testing and debugging --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dce68a6..27b2844 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,9 @@ "require-dev": { "laravel/pint": "^1.8", "mockery/mockery": "^1.5.1", - "pestphp/pest": "^2.5" + "pestphp/pest": "^2.5", + "pestphp/pest-plugin-laravel": "^2.2", + "spatie/laravel-ray": "^1.33", }, "autoload": { "psr-4": { From 5be96c86b7da14bd6e7a5fd645b924ceb419e6ec Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 10:46:58 -0600 Subject: [PATCH 04/31] chore: ignore log files --- .gitignore | 4 +++- storage/logs/.gitkeep | 0 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 storage/logs/.gitkeep diff --git a/.gitignore b/.gitignore index 6e49a00..827219d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ /.vagrant .phpunit.result.cache .DS_Store -composer.lock \ No newline at end of file +composer.lock +/storage/logs/* +!/storage/logs/.gitkeep diff --git a/storage/logs/.gitkeep b/storage/logs/.gitkeep new file mode 100644 index 0000000..e69de29 From ea8c15bdb9ebc15db0e5284e9e7439737a02b4ff Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 10:47:37 -0600 Subject: [PATCH 05/31] feat: add 'install:browser' command --- app/Commands/InstallBrowserCommand.php | 136 ++++++++++++++++++++ app/Facades/GoogleForTesting.php | 21 +++ app/GoogleDownloadable.php | 58 +++++++++ app/GoogleForTesting.php | 58 +++++++++ app/OperatingSystem.php | 47 +++++++ app/Providers/AppServiceProvider.php | 5 +- tests/Feature/InstallBrowserCommandTest.php | 13 +- 7 files changed, 335 insertions(+), 3 deletions(-) create mode 100644 app/Commands/InstallBrowserCommand.php create mode 100644 app/Facades/GoogleForTesting.php create mode 100644 app/GoogleDownloadable.php create mode 100644 app/GoogleForTesting.php create mode 100644 app/OperatingSystem.php diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php new file mode 100644 index 0000000..b788fb1 --- /dev/null +++ b/app/Commands/InstallBrowserCommand.php @@ -0,0 +1,136 @@ + 'linux64', + 'mac-arm' => 'mac-arm64', + 'mac-intel' => 'mac-x64', + 'win' => 'win64', + ]; + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle(): int + { + if (empty($downloadable = $this->version())) { + error("There' no versions available for [{$this->option('ver')}]"); + + return self::FAILURE; + } + + $os = OperatingSystem::id(); + + $version = $downloadable->getVersion(); + + spin( + callback: fn () => download($downloadable->getChromeBrowserURL($this->platforms[$os]), $this->filename($os)), + message: "Downloading Google Chrome Browser [$version]" + ); + + outro("Google Chrome Browser [$version] downloaded"); + + return self::SUCCESS; + } + + protected function version(): GoogleDownloadable|null + { + if ($this->option('latest')) { + return GoogleForTesting::getLatestVersion(); + } + + $version = $this->option('ver'); + + $downloadable = spin( + callback: fn () => GoogleForTesting::getVersion($version), + message: "Searching for version [$version]" + ); + + if (filled($downloadable)) { + return $downloadable; + } + + $versions = GoogleForTesting::getMilestone(Str::before($version, '.')); + + if (empty($versions)) { + return null; + } + + warning("There isn't an exact version [$version]"); + + $version = search( + label: "We found similar versions, please choose one", + options: fn () => $versions->mapWithKeys(fn ($d) => [$d->getVersion() => $d->getVersion()])->all(), + placeholder: 'Choose your prefer version' + ); + + return GoogleForTesting::getVersion($version); + } + + protected function filename(string $os): string + { + $folder = join_paths(getenv('HOME'), '.google-for-testing'); + + File::ensureDirectoryExists($folder); + + return $folder.DIRECTORY_SEPARATOR.'chrome-'.$this->platforms[$os].'.zip'; + } + + public function message(string $text, string $type = 'line'): void + { + $color = match ($type) { + 'success' => 'bg-green', + 'warning' => 'bg-yellow', + 'error' => 'bg-red', + 'info' => 'bg-blue', + default => 'bg-gray-600', + }; + + $type = str($type)->upper(); + + render(<< + $type + + $text +

+ HTML); + } +} diff --git a/app/Facades/GoogleForTesting.php b/app/Facades/GoogleForTesting.php new file mode 100644 index 0000000..cd6e2b5 --- /dev/null +++ b/app/Facades/GoogleForTesting.php @@ -0,0 +1,21 @@ + getMilestone(string $version) Get a collection with all the versions available for a Milestone + */ +class GoogleForTesting extends Facade +{ + public static function getFacadeAccessor(): string + { + return 'gft'; + } +} diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php new file mode 100644 index 0000000..365d730 --- /dev/null +++ b/app/GoogleDownloadable.php @@ -0,0 +1,58 @@ +version; + } + + public function getMilestone(): string + { + return Str::of($this->version)->before('.'); + } + + /** + * @throws \RuntimeException if the required platform doesn't exist + */ + public function getChromeBrowserURL(string $platform): string + { + $item = collect($this->browserDownloads)->first(fn (array $item) => $item['platform'] === $platform); + + if (empty($item)) { + throw new \RuntimeException("The URL for the platform [$platform] you requested, it's not available"); + } + + return $item['url']; + } + + public static function make(string $version, string $revision, array $driverDownloads, array $browserDownloads): static + { + return new static($version, $revision, $driverDownloads, $browserDownloads); + } + + public static function makeFromArray(array $data): static + { + $downloads = $data['downloads']; + + $version = $data['version']; + $revision = $data['revision']; + $driverDownloads = $downloads['chromedriver']; + $browserDownloads = $downloads['chrome']; + + return static::make($version, $revision, $driverDownloads, $browserDownloads); + } +} diff --git a/app/GoogleForTesting.php b/app/GoogleForTesting.php new file mode 100644 index 0000000..edb1878 --- /dev/null +++ b/app/GoogleForTesting.php @@ -0,0 +1,58 @@ +json('channels')['Stable']; + + $version = $channel['version']; + + return static::getVersion($version); + } + + public function getVersion(string $version): ?GoogleDownloadable + { + $response = Http::get(static::$downloads); + + $exact = collect($response->json('versions')) + ->first(fn(array $item) => $item['version'] == $version); + + if (empty($exact)) { + return null; + } + + return GoogleDownloadable::makeFromArray($exact); + } + + public function getMilestone(string $milestone): ?Collection + { + $response = Http::get(static::$downloads); + + $versions = collect($response->json('versions')) + ->filter(fn (array $item) => Str::before($item['version'], '.') == $milestone) + ->map(fn (array $version) => GoogleDownloadable::makeFromArray($version)); + + if ($versions->isEmpty()) { + return null; + } + + return $versions; + } +} diff --git a/app/OperatingSystem.php b/app/OperatingSystem.php new file mode 100644 index 0000000..05530b1 --- /dev/null +++ b/app/OperatingSystem.php @@ -0,0 +1,47 @@ + 'mac-arm', + default => 'mac-intel', + }; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4f1cfae..d43fe12 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\GoogleForTesting; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -11,7 +12,9 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + $this->app->bind('gft', function () { + return new GoogleForTesting; + }); } /** diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 5981820..5e5af89 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -1,8 +1,17 @@ todo(); +it('download the latest browser version', function () { + artisan('install:browser --latest') + ->expectsOutputToContain('Downloading Google Chrome Browser') + ->expectsOutputToContain('downloaded') + ->assertSuccessful(); + + expect(File::exists(join_paths(env('HOME'), '.google-for-testing', 'chrome-mac-arm64.zip'))) + ->toBeTrue(); +}); it('it download the browser version []', function () { From 38f8d4120678e6250abfde5030f88cb11bcf878e Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 10:48:57 -0600 Subject: [PATCH 06/31] refactor: remove trailing comma in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 27b2844..56d13bd 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "mockery/mockery": "^1.5.1", "pestphp/pest": "^2.5", "pestphp/pest-plugin-laravel": "^2.2", - "spatie/laravel-ray": "^1.33", + "spatie/laravel-ray": "^1.33" }, "autoload": { "psr-4": { From 7bf28b261aa0279b6d683491272a131d6523444e Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 11:26:44 -0600 Subject: [PATCH 07/31] fix: add permissions to run-pint.yml action --- .github/workflows/run-pint.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/run-pint.yml b/.github/workflows/run-pint.yml index 35157a0..212170d 100644 --- a/.github/workflows/run-pint.yml +++ b/.github/workflows/run-pint.yml @@ -5,6 +5,10 @@ on: paths: - "**.php" +permissions: + contents: write + pull-requests: write + jobs: pint-code-style: runs-on: ubuntu-latest From 7da59e552a524bb0c5dfb04df819d773561d0667 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:03:18 -0600 Subject: [PATCH 08/31] build: add log dependency --- composer.json | 1 + config/logging.php | 118 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 config/logging.php diff --git a/composer.json b/composer.json index 56d13bd..f250266 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "php": "^8.1", "guzzlehttp/guzzle": "^7.5", "illuminate/http": "^10.0", + "illuminate/log": "^10.0", "laravel-zero/framework": "^10.0.2", "nunomaduro/termwind": "^1.15.1" }, diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 0000000..6720dfa --- /dev/null +++ b/config/logging.php @@ -0,0 +1,118 @@ + env('LOG_CHANNEL', 'stack'), + + /* + |-------------------------------------------------------------------------- + | Deprecations Log Channel + |-------------------------------------------------------------------------- + | + | This option controls the log channel that should be used to log warnings + | regarding deprecated PHP and library features. This allows you to get + | your application ready for upcoming major versions of dependencies. + | + */ + + 'deprecations' => env('LOG_DEPRECATIONS_CHANNEL', 'null'), + + /* + |-------------------------------------------------------------------------- + | Log Channels + |-------------------------------------------------------------------------- + | + | Here you may configure the log channels for your application. Out of + | the box, Laravel uses the Monolog PHP logging library. This gives + | you a variety of powerful log handlers / formatters to utilize. + | + | Available Drivers: "single", "daily", "slack", "syslog", + | "errorlog", "monolog", + | "custom", "stack" + | + */ + + 'channels' => [ + 'stack' => [ + 'driver' => 'stack', + 'channels' => ['stderr'], + 'ignore_exceptions' => false, + ], + + 'single' => [ + 'driver' => 'single', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + ], + + 'daily' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + 'days' => 14, + ], + + 'slack' => [ + 'driver' => 'slack', + 'url' => env('LOG_SLACK_WEBHOOK_URL'), + 'username' => 'Laravel Log', + 'emoji' => ':boom:', + 'level' => env('LOG_LEVEL', 'critical'), + ], + + 'papertrail' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => SyslogUdpHandler::class, + 'handler_with' => [ + 'host' => env('PAPERTRAIL_URL'), + 'port' => env('PAPERTRAIL_PORT'), + ], + ], + + 'stderr' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => StreamHandler::class, + 'formatter' => env('LOG_STDERR_FORMATTER'), + 'with' => [ + 'stream' => 'php://stderr', + ], + ], + + 'syslog' => [ + 'driver' => 'syslog', + 'level' => env('LOG_LEVEL', 'debug'), + ], + + 'errorlog' => [ + 'driver' => 'errorlog', + 'level' => env('LOG_LEVEL', 'debug'), + ], + + 'null' => [ + 'driver' => 'monolog', + 'handler' => NullHandler::class, + ], + + 'emergency' => [ + 'path' => storage_path('logs/laravel.log'), + ], + ], + +]; From d87be4c076b2ace6e7354f105268a1bbeedf1ac2 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:04:49 -0600 Subject: [PATCH 09/31] refactor: add unzip function This commit also catch any exception and display and error message. --- app/Commands/InstallBrowserCommand.php | 30 ++++++++++++++++++--- tests/Feature/InstallBrowserCommandTest.php | 10 ++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index b788fb1..7be4836 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -7,6 +7,7 @@ use App\OperatingSystem; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use LaravelZero\Framework\Commands\Command; use function Laravel\Prompts\error; @@ -60,10 +61,31 @@ public function handle(): int $version = $downloadable->getVersion(); - spin( - callback: fn () => download($downloadable->getChromeBrowserURL($this->platforms[$os]), $this->filename($os)), - message: "Downloading Google Chrome Browser [$version]" - ); + $filename = $this->filename($os); + + try { + $result = true; + + spin( + callback: fn () => download($downloadable->getChromeBrowserURL($this->platforms[$os]), $filename), + message: "Downloading Google Chrome Browser [$version]" + ); + + spin( + callback: fn () => unzip($filename), + message: 'Unzipping Google Chrome Browser', + ); + } catch (\Exception $e) { + Log::error($e->getMessage()); + + $result = false; + } + + if (! $result) { + error("Unable to download/install Google Chrome Browser [$version]"); + + return self::FAILURE; + } outro("Google Chrome Browser [$version] downloaded"); diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 5e5af89..8dab1cf 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -1,6 +1,7 @@ expectsOutputToContain('downloaded') ->assertSuccessful(); - expect(File::exists(join_paths(env('HOME'), '.google-for-testing', 'chrome-mac-arm64.zip'))) + $finder = new Finder; + + $finder + ->directories() + ->in(join_paths(env('HOME'), '.google-for-testing')) + ->name('chrome*'); + + expect($finder->hasResults()) ->toBeTrue(); }); From 788c0ad0e6d5fffa4732443f55c8f3d140625235 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:14:45 -0600 Subject: [PATCH 10/31] refactor: change filename method for a generic one --- app/Commands/InstallBrowserCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 7be4836..c6bbe5f 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -61,7 +61,7 @@ public function handle(): int $version = $downloadable->getVersion(); - $filename = $this->filename($os); + $filename = $this->getBasePath('chrome-'.$this->platforms[$os].'.zip'); try { $result = true; @@ -126,13 +126,13 @@ protected function version(): GoogleDownloadable|null return GoogleForTesting::getVersion($version); } - protected function filename(string $os): string + protected function getBasePath(?string $path = null): string { $folder = join_paths(getenv('HOME'), '.google-for-testing'); File::ensureDirectoryExists($folder); - return $folder.DIRECTORY_SEPARATOR.'chrome-'.$this->platforms[$os].'.zip'; + return join_paths($folder, $path); } public function message(string $text, string $type = 'line'): void From a5a2fa6fc9c4f317ba934e2ccb6ba4048156b888 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:23:23 -0600 Subject: [PATCH 11/31] refactor: remove zip file at the end --- app/Commands/InstallBrowserCommand.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index c6bbe5f..cf1f8b3 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -79,6 +79,8 @@ public function handle(): int Log::error($e->getMessage()); $result = false; + } finally { + File::delete($filename); } if (! $result) { From f081b3abc7d414c21dffa8962e7bf8a95c8c5ce5 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:26:04 -0600 Subject: [PATCH 12/31] style: fix typo --- app/Commands/InstallBrowserCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index cf1f8b3..434e9a1 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -34,7 +34,7 @@ class InstallBrowserCommand extends Command * * @var string */ - protected $description = 'Install Google Browser from'; + protected $description = 'Install Google Browser'; protected array $platforms = [ From 480027fd72d268239dc43c9858026d868ef8fdef Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:33:13 -0600 Subject: [PATCH 13/31] refactor: re-order arguments This also makes the chromedriver array empty if is not present, because some version prior 115.0.5763.0 will not have the chromedriver array --- app/GoogleDownloadable.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php index 365d730..103dce9 100644 --- a/app/GoogleDownloadable.php +++ b/app/GoogleDownloadable.php @@ -9,8 +9,8 @@ class GoogleDownloadable protected function __construct( protected string $version, protected string $revision, - protected array $driverDownloads, - protected array $browserDownloads + protected array $browserDownloads, + protected array $driverDownloads ) { // } @@ -39,9 +39,9 @@ public function getChromeBrowserURL(string $platform): string return $item['url']; } - public static function make(string $version, string $revision, array $driverDownloads, array $browserDownloads): static + public static function make(string $version, string $revision, array $browserDownloads, array $driverDownloads): static { - return new static($version, $revision, $driverDownloads, $browserDownloads); + return new static($version, $revision, $browserDownloads, $driverDownloads); } public static function makeFromArray(array $data): static @@ -50,9 +50,9 @@ public static function makeFromArray(array $data): static $version = $data['version']; $revision = $data['revision']; - $driverDownloads = $downloads['chromedriver']; $browserDownloads = $downloads['chrome']; + $driverDownloads = $downloads['chromedriver'] ?? []; - return static::make($version, $revision, $driverDownloads, $browserDownloads); + return static::make($version, $revision, $browserDownloads, $driverDownloads); } } From 546ff8d8f22aa54678cc1361e9722416fc4de68c Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:48:56 -0600 Subject: [PATCH 14/31] feat: add 'install:driver' command --- app/Commands/InstallCommand.php | 80 +++++++++++++++++++++++ app/Commands/InstallDriverCommand.php | 91 +++++++++++++++++++++++++++ app/GoogleDownloadable.php | 15 +++++ 3 files changed, 186 insertions(+) create mode 100644 app/Commands/InstallCommand.php create mode 100644 app/Commands/InstallDriverCommand.php diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php new file mode 100644 index 0000000..60920d8 --- /dev/null +++ b/app/Commands/InstallCommand.php @@ -0,0 +1,80 @@ +option('latest')) { + return GoogleForTesting::getLatestVersion(); + } + + $version = $this->option('ver'); + + $downloadable = spin( + callback: fn () => GoogleForTesting::getVersion($version), + message: "Searching for version [$version]" + ); + + if (filled($downloadable)) { + return $downloadable; + } + + $versions = GoogleForTesting::getMilestone(Str::before($version, '.')); + + if (empty($versions)) { + return null; + } + + warning("There isn't an exact version [$version]"); + + $version = search( + label: "We found similar versions, please choose one", + options: fn () => $versions->mapWithKeys(fn ($d) => [$d->getVersion() => $d->getVersion()])->all(), + placeholder: 'Choose your prefer version' + ); + + return GoogleForTesting::getVersion($version); + } + + protected function getBasePath(?string $path = null): string + { + $folder = join_paths(getenv('HOME'), '.google-for-testing'); + + File::ensureDirectoryExists($folder); + + return join_paths($folder, $path); + } + + public function message(string $text, string $type = 'line'): void + { + $color = match ($type) { + 'success' => 'bg-green', + 'warning' => 'bg-yellow', + 'error' => 'bg-red', + 'info' => 'bg-blue', + default => 'bg-gray-600', + }; + + $type = str($type)->upper(); + + render(<< + $type + + $text +

+ HTML); + } +} diff --git a/app/Commands/InstallDriverCommand.php b/app/Commands/InstallDriverCommand.php new file mode 100644 index 0000000..22c2c67 --- /dev/null +++ b/app/Commands/InstallDriverCommand.php @@ -0,0 +1,91 @@ + 'linux64', + 'mac-arm' => 'mac-arm64', + 'mac-intel' => 'mac-x64', + 'win' => 'win64', + ]; + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle(): int + { + if (empty($downloadable = $this->version())) { + error("There' no versions available for [{$this->option('ver')}]"); + + return self::FAILURE; + } + + $os = OperatingSystem::id(); + + $version = $downloadable->getVersion(); + + $filename = $this->getBasePath('chromedriver-'.$this->platforms[$os].'.zip'); + + try { + $result = true; + + spin( + callback: fn () => download($downloadable->getChromeDriverURL($this->platforms[$os]), $filename), + message: "Downloading Google Chrome Driver [$version]" + ); + + spin( + callback: fn () => unzip($filename), + message: 'Unzipping Google Chrome Driver', + ); + } catch (\Exception $e) { + Log::error($e->getMessage()); + + error("Unable to download/install Google Chrome Driver [$version]"); + + return self::FAILURE; + } finally { + File::delete($filename); + } + + outro("Google Chrome Driver [$version] downloaded"); + + return self::SUCCESS; + } +} diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php index 103dce9..9deaca2 100644 --- a/app/GoogleDownloadable.php +++ b/app/GoogleDownloadable.php @@ -39,6 +39,21 @@ public function getChromeBrowserURL(string $platform): string return $item['url']; } + + /** + * @throws \RuntimeException if the required platform doesn't exist + */ + public function getChromeDriverURL(string $platform): string + { + $item = collect($this->driverDownloads)->first(fn (array $item) => $item['platform'] === $platform); + + if (empty($item)) { + throw new \RuntimeException("The URL for the platform [$platform] you requested, it's not available"); + } + + return $item['url']; + } + public static function make(string $version, string $revision, array $browserDownloads, array $driverDownloads): static { return new static($version, $revision, $browserDownloads, $driverDownloads); From 5b80fca5895514dc62bfacc43c5b824b4201d84b Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:49:26 -0600 Subject: [PATCH 15/31] refactor: change base class --- app/Commands/InstallBrowserCommand.php | 33 +------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 434e9a1..0e0ae09 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -9,7 +9,6 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; -use LaravelZero\Framework\Commands\Command; use function Laravel\Prompts\error; use function Laravel\Prompts\outro; use function Laravel\Prompts\search; @@ -18,7 +17,7 @@ use function Laravel\Prompts\info; use function Termwind\render; -class InstallBrowserCommand extends Command +class InstallBrowserCommand extends InstallCommand { /** * The signature of the command. @@ -127,34 +126,4 @@ protected function version(): GoogleDownloadable|null return GoogleForTesting::getVersion($version); } - - protected function getBasePath(?string $path = null): string - { - $folder = join_paths(getenv('HOME'), '.google-for-testing'); - - File::ensureDirectoryExists($folder); - - return join_paths($folder, $path); - } - - public function message(string $text, string $type = 'line'): void - { - $color = match ($type) { - 'success' => 'bg-green', - 'warning' => 'bg-yellow', - 'error' => 'bg-red', - 'info' => 'bg-blue', - default => 'bg-gray-600', - }; - - $type = str($type)->upper(); - - render(<< - $type - - $text -

- HTML); - } } From 33be908d6e657ba52c367c28dce5ee87e1588005 Mon Sep 17 00:00:00 2001 From: asciito Date: Thu, 19 Oct 2023 12:53:27 -0600 Subject: [PATCH 16/31] refactor: change exception message --- app/GoogleDownloadable.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php index 9deaca2..76fd9ad 100644 --- a/app/GoogleDownloadable.php +++ b/app/GoogleDownloadable.php @@ -33,13 +33,12 @@ public function getChromeBrowserURL(string $platform): string $item = collect($this->browserDownloads)->first(fn (array $item) => $item['platform'] === $platform); if (empty($item)) { - throw new \RuntimeException("The URL for the platform [$platform] you requested, it's not available"); + throw new \RuntimeException("The URL for Google Chrome Browser for platform [$platform], it's not available"); } return $item['url']; } - /** * @throws \RuntimeException if the required platform doesn't exist */ @@ -48,7 +47,7 @@ public function getChromeDriverURL(string $platform): string $item = collect($this->driverDownloads)->first(fn (array $item) => $item['platform'] === $platform); if (empty($item)) { - throw new \RuntimeException("The URL for the platform [$platform] you requested, it's not available"); + throw new \RuntimeException("The URL for Google Chrome Driver for platform [$platform], it's not available"); } return $item['url']; From f7e4606ae5934c984a90cfb0b2e718f0bef0be04 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 10:08:30 -0600 Subject: [PATCH 17/31] refactor: remove unnecessary code --- app/Commands/InstallBrowserCommand.php | 44 ++------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 0e0ae09..8abfa42 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -63,8 +63,6 @@ public function handle(): int $filename = $this->getBasePath('chrome-'.$this->platforms[$os].'.zip'); try { - $result = true; - spin( callback: fn () => download($downloadable->getChromeBrowserURL($this->platforms[$os]), $filename), message: "Downloading Google Chrome Browser [$version]" @@ -77,53 +75,15 @@ public function handle(): int } catch (\Exception $e) { Log::error($e->getMessage()); - $result = false; - } finally { - File::delete($filename); - } - - if (! $result) { error("Unable to download/install Google Chrome Browser [$version]"); return self::FAILURE; + } finally { + File::delete($filename); } outro("Google Chrome Browser [$version] downloaded"); return self::SUCCESS; } - - protected function version(): GoogleDownloadable|null - { - if ($this->option('latest')) { - return GoogleForTesting::getLatestVersion(); - } - - $version = $this->option('ver'); - - $downloadable = spin( - callback: fn () => GoogleForTesting::getVersion($version), - message: "Searching for version [$version]" - ); - - if (filled($downloadable)) { - return $downloadable; - } - - $versions = GoogleForTesting::getMilestone(Str::before($version, '.')); - - if (empty($versions)) { - return null; - } - - warning("There isn't an exact version [$version]"); - - $version = search( - label: "We found similar versions, please choose one", - options: fn () => $versions->mapWithKeys(fn ($d) => [$d->getVersion() => $d->getVersion()])->all(), - placeholder: 'Choose your prefer version' - ); - - return GoogleForTesting::getVersion($version); - } } From e170818959a55eaf8fd843fab1953e030fcb31d6 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 10:09:41 -0600 Subject: [PATCH 18/31] test: change first two test --- tests/Feature/InstallBrowserCommandTest.php | 51 +++++++++++++++------ 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 8dab1cf..1b4f1f1 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -1,29 +1,54 @@ shouldReceive('getVersion') + ->andReturn('200.0.0.0'); + + $downloadable->shouldReceive('getChromeBrowserURL') + ->andReturn('https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/200.0.0.0/linux64/chrome-linux64.zip'); + + $google->shouldReceive('getLatestVersion') + ->andReturn($downloadable); + artisan('install:browser --latest') - ->expectsOutputToContain('Downloading Google Chrome Browser') - ->expectsOutputToContain('downloaded') + ->doesntExpectOutputToContain("There' no versions available for [200.0.0.0]") + ->expectsOutputToContain('Downloading Google Chrome Browser [200.0.0.0]') + ->expectsOutputToContain('Google Chrome Browser [200.0.0.0] downloaded') ->assertSuccessful(); +}); - $finder = new Finder; +it('it download the browser version [113.0.5672.0]', function () { + Http::fake(); - $finder - ->directories() - ->in(join_paths(env('HOME'), '.google-for-testing')) - ->name('chrome*'); + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); - expect($finder->hasResults()) - ->toBeTrue(); -}); + $downloadable->shouldReceive('getVersion') + ->andReturn('113.0.5672.0'); -it('it download the browser version []', function () { + $downloadable->shouldReceive('getChromeBrowserURL') + ->andReturn('https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/linux64/chrome-linux64.zip'); -})->todo(); + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:browser --ver=113.0.5672.0') + ->doesntExpectOutputToContain("There' no versions available for [113.0.5672.0]") + ->expectsOutputToContain('Downloading Google Chrome Browser [113.0.5672.0]') + ->expectsOutputToContain('Google Chrome Browser [113.0.5672.0] downloaded') + ->assertSuccessful(); +}); it('download the browser on other path', function () { From f49ccefc2e32890cfdfbf44c31d195d60b2eba23 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 11:04:24 -0600 Subject: [PATCH 19/31] refactor: add download method on GoogleDownloadable class --- app/Commands/InstallBrowserCommand.php | 29 ++++++++++++++------- app/GoogleDownloadable.php | 19 ++++++++++++++ tests/Feature/InstallBrowserCommandTest.php | 5 ++-- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 8abfa42..e20c50c 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -26,7 +26,8 @@ class InstallBrowserCommand extends InstallCommand */ protected $signature = 'install:browser {--ver=115.0.5763.0 : Install specific version} - {--latest : Install the latest version}'; + {--latest : Install the latest version} + {--path= : Specify the path where to download the browser}'; /** * The description of the command. @@ -60,19 +61,18 @@ public function handle(): int $version = $downloadable->getVersion(); - $filename = $this->getBasePath('chrome-'.$this->platforms[$os].'.zip'); + $filename = $this->getFilename($os); try { spin( - callback: fn () => download($downloadable->getChromeBrowserURL($this->platforms[$os]), $filename), + callback: fn () => $downloadable->download(GoogleDownloadable::BROWSER, $this->platforms[$os], $filename), message: "Downloading Google Chrome Browser [$version]" ); - spin( - callback: fn () => unzip($filename), - message: 'Unzipping Google Chrome Browser', - ); - } catch (\Exception $e) { + $dir = dirname($filename); + + $this->message("Google Chrome Browser unzip on [$dir]", 'info'); + } catch (\Throwable $e) { Log::error($e->getMessage()); error("Unable to download/install Google Chrome Browser [$version]"); @@ -82,8 +82,19 @@ public function handle(): int File::delete($filename); } - outro("Google Chrome Browser [$version] downloaded"); + $this->message("Google Chrome Browser [$version] downloaded", 'success'); return self::SUCCESS; } + + protected function getFilename(string $os): string + { + $filename = 'chrome-'.$this->platforms[$os].'.zip'; + + if (! $this->option('path')) { + return $this->getBasePath($filename); + } + + return join_paths($this->option('path'), $filename); + } } diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php index 76fd9ad..bd08621 100644 --- a/app/GoogleDownloadable.php +++ b/app/GoogleDownloadable.php @@ -6,6 +6,10 @@ class GoogleDownloadable { + const BROWSER = 1; + + const DRIVER = 2; + protected function __construct( protected string $version, protected string $revision, @@ -53,6 +57,21 @@ public function getChromeDriverURL(string $platform): string return $item['url']; } + public function download(int $component, string $platform, string $filename, bool $unzip = false): void + { + if ($component & static::BROWSER) { + download($browser = $this->getChromeBrowserURL($platform), $filename); + + $unzip && unzip($browser); + } + + if ($component & static::DRIVER) { + download($driver = $this->getChromeDriverURL($platform), $filename); + + $unzip && unzip($driver); + } + } + public static function make(string $version, string $revision, array $browserDownloads, array $driverDownloads): static { return new static($version, $revision, $browserDownloads, $driverDownloads); diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 1b4f1f1..870ed65 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -15,6 +15,8 @@ $downloadable->shouldReceive('getVersion') ->andReturn('200.0.0.0'); + $downloadable->shouldReceive('download'); + $downloadable->shouldReceive('getChromeBrowserURL') ->andReturn('https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/200.0.0.0/linux64/chrome-linux64.zip'); @@ -37,8 +39,7 @@ $downloadable->shouldReceive('getVersion') ->andReturn('113.0.5672.0'); - $downloadable->shouldReceive('getChromeBrowserURL') - ->andReturn('https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/linux64/chrome-linux64.zip'); + $downloadable->shouldReceive('download'); $google->shouldReceive('getVersion') ->andReturn($downloadable); From de8e84fbcba763193c1004cb3b0cd984f2c39106 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 11:04:56 -0600 Subject: [PATCH 20/31] test: add test to download browser in other path --- app/Commands/InstallBrowserCommand.php | 2 +- tests/Feature/InstallBrowserCommandTest.php | 23 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index e20c50c..58a45bf 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -71,7 +71,7 @@ public function handle(): int $dir = dirname($filename); - $this->message("Google Chrome Browser unzip on [$dir]", 'info'); + $this->message("Google Chrome Browser unzip it on [$dir]", 'info'); } catch (\Throwable $e) { Log::error($e->getMessage()); diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 870ed65..fb9ca4d 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -52,8 +52,29 @@ }); it('download the browser on other path', function () { + Http::fake(); + File::partialMock() + ->shouldReceive('append') + ->andReturn(true); -})->todo(); + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('200.0.0.0'); + + $downloadable->shouldReceive('download'); + + $google->shouldReceive('getLatestVersion') + ->andReturn($downloadable); + + artisan('install:browser --latest --path=/some/dir/to/download') + ->doesntExpectOutputToContain("There' no versions available for [200.0.0.0]") + ->expectsOutputToContain('Downloading Google Chrome Browser [200.0.0.0]') + ->expectsOutputToContain('Google Chrome Browser [200.0.0.0] downloaded') + ->expectsOutputToContain("Google Chrome Browser unzip it on [/some/dir/to/download]") + ->assertSuccessful(); +}); it('try to download a pre-existing browser version', function () { From 60f1a5302953adf7a5e0859ae070663e084cd65f Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 11:07:13 -0600 Subject: [PATCH 21/31] chore: fix styling --- app/Commands/InstallBrowserCommand.php | 10 +---- app/Commands/InstallCommand.php | 13 ++++--- app/Commands/InstallDriverCommand.php | 10 +---- app/Facades/GoogleForTesting.php | 1 - app/GoogleForTesting.php | 4 +- app/helpers.php | 7 ++-- tests/Feature/InstallBrowserCommandTest.php | 3 +- tests/Unit/HelpersTest.php | 42 ++++++++++----------- 8 files changed, 35 insertions(+), 55 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 58a45bf..3b23e05 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -2,20 +2,13 @@ namespace App\Commands; -use App\Facades\GoogleForTesting; use App\GoogleDownloadable; use App\OperatingSystem; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Str; + use function Laravel\Prompts\error; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\search; use function Laravel\Prompts\spin; -use function Laravel\Prompts\warning; -use function Laravel\Prompts\info; -use function Termwind\render; class InstallBrowserCommand extends InstallCommand { @@ -36,7 +29,6 @@ class InstallBrowserCommand extends InstallCommand */ protected $description = 'Install Google Browser'; - protected array $platforms = [ 'linux' => 'linux64', 'mac-arm' => 'mac-arm64', diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index 60920d8..f2169c9 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -7,6 +7,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; + use function Laravel\Prompts\search; use function Laravel\Prompts\spin; use function Laravel\Prompts\warning; @@ -14,7 +15,7 @@ abstract class InstallCommand extends Command { - protected function version(): GoogleDownloadable|null + protected function version(): ?GoogleDownloadable { if ($this->option('latest')) { return GoogleForTesting::getLatestVersion(); @@ -40,7 +41,7 @@ protected function version(): GoogleDownloadable|null warning("There isn't an exact version [$version]"); $version = search( - label: "We found similar versions, please choose one", + label: 'We found similar versions, please choose one', options: fn () => $versions->mapWithKeys(fn ($d) => [$d->getVersion() => $d->getVersion()])->all(), placeholder: 'Choose your prefer version' ); @@ -48,7 +49,7 @@ protected function version(): GoogleDownloadable|null return GoogleForTesting::getVersion($version); } - protected function getBasePath(?string $path = null): string + protected function getBasePath(string $path = null): string { $folder = join_paths(getenv('HOME'), '.google-for-testing'); @@ -62,9 +63,9 @@ public function message(string $text, string $type = 'line'): void $color = match ($type) { 'success' => 'bg-green', 'warning' => 'bg-yellow', - 'error' => 'bg-red', - 'info' => 'bg-blue', - default => 'bg-gray-600', + 'error' => 'bg-red', + 'info' => 'bg-blue', + default => 'bg-gray-600', }; $type = str($type)->upper(); diff --git a/app/Commands/InstallDriverCommand.php b/app/Commands/InstallDriverCommand.php index 22c2c67..ce7a247 100644 --- a/app/Commands/InstallDriverCommand.php +++ b/app/Commands/InstallDriverCommand.php @@ -2,20 +2,13 @@ namespace App\Commands; -use App\Facades\GoogleForTesting; -use App\GoogleDownloadable; use App\OperatingSystem; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Str; + use function Laravel\Prompts\error; use function Laravel\Prompts\outro; -use function Laravel\Prompts\search; use function Laravel\Prompts\spin; -use function Laravel\Prompts\warning; -use function Laravel\Prompts\info; -use function Termwind\render; class InstallDriverCommand extends InstallCommand { @@ -35,7 +28,6 @@ class InstallDriverCommand extends InstallCommand */ protected $description = 'Install Google Driver'; - protected array $platforms = [ 'linux' => 'linux64', 'mac-arm' => 'mac-arm64', diff --git a/app/Facades/GoogleForTesting.php b/app/Facades/GoogleForTesting.php index cd6e2b5..0ba1f2d 100644 --- a/app/Facades/GoogleForTesting.php +++ b/app/Facades/GoogleForTesting.php @@ -5,7 +5,6 @@ use App\GoogleDownloadable; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Facade; -use Illuminate\Support\Facades\Http; /** * @method static null|GoogleDownloadable getLatestVersion() Get the latest version of Google Chrome Browser and Google Chrome Driver diff --git a/app/GoogleForTesting.php b/app/GoogleForTesting.php index edb1878..91ff6ef 100644 --- a/app/GoogleForTesting.php +++ b/app/GoogleForTesting.php @@ -2,9 +2,9 @@ namespace App; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Str; -use Illuminate\Support\Collection; class GoogleForTesting { @@ -32,7 +32,7 @@ public function getVersion(string $version): ?GoogleDownloadable $response = Http::get(static::$downloads); $exact = collect($response->json('versions')) - ->first(fn(array $item) => $item['version'] == $version); + ->first(fn (array $item) => $item['version'] == $version); if (empty($exact)) { return null; diff --git a/app/helpers.php b/app/helpers.php index 397e431..56fbb3c 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -16,7 +16,7 @@ function join_paths(string ...$paths): string array_map(fn (string $p) => trim($p, DIRECTORY_SEPARATOR), $paths) ); - return join(DIRECTORY_SEPARATOR, [rtrim($first, DIRECTORY_SEPARATOR), ...$paths]); + return implode(DIRECTORY_SEPARATOR, [rtrim($first, DIRECTORY_SEPARATOR), ...$paths]); } } @@ -61,9 +61,8 @@ function download(string $url, string $file, bool $force = false): void * This function will attempt to unzip the given zip file name into the given location, but if the location * is not provided, we'll use the file directory. * - * @param string $filename The file name of the ZIP file - * @param ?string $to The location where to extract the content - * @return void + * @param string $filename The file name of the ZIP file + * @param ?string $to The location where to extract the content */ function unzip(string $filename, string $to = null): void { diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index fb9ca4d..859d486 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -4,6 +4,7 @@ use App\GoogleDownloadable; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; + use function Pest\Laravel\artisan; it('download the latest browser version', function () { @@ -72,7 +73,7 @@ ->doesntExpectOutputToContain("There' no versions available for [200.0.0.0]") ->expectsOutputToContain('Downloading Google Chrome Browser [200.0.0.0]') ->expectsOutputToContain('Google Chrome Browser [200.0.0.0] downloaded') - ->expectsOutputToContain("Google Chrome Browser unzip it on [/some/dir/to/download]") + ->expectsOutputToContain('Google Chrome Browser unzip it on [/some/dir/to/download]') ->assertSuccessful(); }); diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index b6957d3..9443fac 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -6,16 +6,16 @@ dataset('paths', fn () => [ 'simple path' => [ 'paths' => ['/this', 'is', 'a', 'path/'], - 'result' => '/this/is/a/path' + 'result' => '/this/is/a/path', ], 'weird path' => [ 'paths' => ['/just/another/', '/path/to/', '/join//'], - 'result' => '/just/another/path/to/join' + 'result' => '/just/another/path/to/join', ], 'strange path' => [ 'paths' => ['what', '//is//this/', '//path//'], - 'result' => 'what/is//this/path' - ] + 'result' => 'what/is//this/path', + ], ]); afterAll(fn () => File::delete(join_paths(__DIR__, '..', 'files', 'file.txt'))); @@ -26,26 +26,26 @@ })->with('paths'); it('download a file', function () { - Http::fake(); + Http::fake(); - $fileMock = File::partialMock(); + $fileMock = File::partialMock(); - $fileMock - ->shouldReceive('exists') - ->andReturn(false, true); + $fileMock + ->shouldReceive('exists') + ->andReturn(false, true); - $fileMock - ->shouldReceive('delete') - ->andReturn(false); + $fileMock + ->shouldReceive('delete') + ->andReturn(false); - $fileMock - ->expects('append') - ->andReturn(); + $fileMock + ->expects('append') + ->andReturn(); - expect(fn () => download('https://fake-download.com', '/path/to/a/file.zip')) - ->not->toThrow(\Exception::class) - ->and(File::exists('/path/to/a/file.zip')) - ->toBeTrue(); + expect(fn () => download('https://fake-download.com', '/path/to/a/file.zip')) + ->not->toThrow(\Exception::class) + ->and(File::exists('/path/to/a/file.zip')) + ->toBeTrue(); }); it('try to download a file that already exists', function () { @@ -61,7 +61,6 @@ ->toThrow(\Exception::class); }); - it('delete a pre-existing file to download it again', function () { Http::fake(); @@ -95,6 +94,3 @@ ->and(File::exists($file)) ->toBeTrue(); }); - - - From 820b2c1e8fc2c74529c332a4c6f05922fe442534 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 14:41:27 -0600 Subject: [PATCH 22/31] refactor: download method --- app/GoogleDownloadable.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/GoogleDownloadable.php b/app/GoogleDownloadable.php index bd08621..95c7f6e 100644 --- a/app/GoogleDownloadable.php +++ b/app/GoogleDownloadable.php @@ -57,18 +57,24 @@ public function getChromeDriverURL(string $platform): string return $item['url']; } - public function download(int $component, string $platform, string $filename, bool $unzip = false): void + public function download(int $component, string $to, string $platform, bool $unzip = false): void { if ($component & static::BROWSER) { - download($browser = $this->getChromeBrowserURL($platform), $filename); + $url = $this->getChromeBrowserURL($platform); + $filename = join_paths($to, Str::afterLast($url, '/')); - $unzip && unzip($browser); + download($url, $filename); + + $unzip && unzip($filename); } if ($component & static::DRIVER) { - download($driver = $this->getChromeDriverURL($platform), $filename); + $url = $this->getChromeDriverURL($platform); + $filename = join_paths($to, Str::afterLast($url, '/')); + + download($url, $filename); - $unzip && unzip($driver); + $unzip && unzip($filename); } } From 8be42ba6f9b7e55ffa86d24ca0bd87b37efd0a56 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 14:42:16 -0600 Subject: [PATCH 23/31] fix: getBasePath to handle empty parameter --- app/Commands/InstallCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index f2169c9..60fc964 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -49,13 +49,13 @@ protected function version(): ?GoogleDownloadable return GoogleForTesting::getVersion($version); } - protected function getBasePath(string $path = null): string + protected function getBasePath(?string $path = null): string { $folder = join_paths(getenv('HOME'), '.google-for-testing'); File::ensureDirectoryExists($folder); - return join_paths($folder, $path); + return join_paths($folder, $path ?? ''); } public function message(string $text, string $type = 'line'): void From dc139b4337c2cbb1407c592a4b2b268728d1530f Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 14:43:27 -0600 Subject: [PATCH 24/31] refactor: change getFilename for getDownloadDirectory * the downloaded zip file is not deleted now --- app/Commands/InstallBrowserCommand.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 3b23e05..fb2d502 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; use function Laravel\Prompts\error; use function Laravel\Prompts\spin; @@ -53,25 +54,19 @@ public function handle(): int $version = $downloadable->getVersion(); - $filename = $this->getFilename($os); - try { spin( - callback: fn () => $downloadable->download(GoogleDownloadable::BROWSER, $this->platforms[$os], $filename), + callback: fn () => $downloadable->download(GoogleDownloadable::BROWSER, $this->getDownloadDirectory(), $this->platforms[$os], true), message: "Downloading Google Chrome Browser [$version]" ); - $dir = dirname($filename); - - $this->message("Google Chrome Browser unzip it on [$dir]", 'info'); + $this->message("Google Chrome Browser unzip it on [{$this->getDownloadDirectory()}]", 'info'); } catch (\Throwable $e) { Log::error($e->getMessage()); error("Unable to download/install Google Chrome Browser [$version]"); return self::FAILURE; - } finally { - File::delete($filename); } $this->message("Google Chrome Browser [$version] downloaded", 'success'); @@ -79,14 +74,12 @@ public function handle(): int return self::SUCCESS; } - protected function getFilename(string $os): string + protected function getDownloadDirectory(): string { - $filename = 'chrome-'.$this->platforms[$os].'.zip'; - if (! $this->option('path')) { - return $this->getBasePath($filename); + return $this->getBasePath(); } - return join_paths($this->option('path'), $filename); + return $this->option('path'); } } From dfc1835c19cca5ed03d8ad869ce53ade75c2edff Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 15:12:31 -0600 Subject: [PATCH 25/31] test: add final test for 'install:browser' command --- tests/Feature/InstallBrowserCommandTest.php | 50 +++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 859d486..f30897c 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -5,8 +5,34 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use function Pest\Laravel\artisan; +it('download default browser version', function () { + Http::fake(); + File::partialMock() + ->shouldReceive('append') + ->andReturn(true); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('115.0.5763.0'); + + $downloadable->shouldReceive('download'); + + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:browser') + ->doesntExpectOutputToContain("There' no versions available for [115.0.5763.0]") + ->expectsOutputToContain('Downloading Google Chrome Browser [115.0.5763.0]') + ->expectsOutputToContain('Google Chrome Browser [115.0.5763.0] downloaded') + ->expectsOutputToContain('Google Chrome Browser unzip it on') + ->assertSuccessful(); +}); + it('download the latest browser version', function () { Http::fake(); @@ -78,9 +104,27 @@ }); it('try to download a pre-existing browser version', function () { + Http::fake(); -})->todo(); + Log::partialMock() + ->shouldReceive('error') + ->with('The file [chrome-linux.zip] already exists'); -it('try to download a non existing browser version', function () { + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); -})->todo(); + $downloadable + ->shouldReceive('getVersion') + ->andReturn('115.0.5763.0'); + + $downloadable + ->shouldReceive('download') + ->andThrow(\Exception::class, 'The file [chrome-linux.zip] already exists'); + + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:browser') + ->doesntExpectOutputToContain('Google Chrome Browser [115.0.5763.0] downloaded') + ->assertFailed(); +}); From 5f1c5251b5b7109b9e9a85e1a54aaa231231be41 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 15:12:52 -0600 Subject: [PATCH 26/31] chore: fix styling --- app/Commands/InstallBrowserCommand.php | 2 -- app/Commands/InstallCommand.php | 2 +- tests/Feature/InstallBrowserCommandTest.php | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index fb2d502..9814a79 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -4,10 +4,8 @@ use App\GoogleDownloadable; use App\OperatingSystem; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Str; use function Laravel\Prompts\error; use function Laravel\Prompts\spin; diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index 60fc964..6efba9b 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -49,7 +49,7 @@ protected function version(): ?GoogleDownloadable return GoogleForTesting::getVersion($version); } - protected function getBasePath(?string $path = null): string + protected function getBasePath(string $path = null): string { $folder = join_paths(getenv('HOME'), '.google-for-testing'); diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index f30897c..71f970e 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -4,8 +4,8 @@ use App\GoogleDownloadable; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; - use Illuminate\Support\Facades\Log; + use function Pest\Laravel\artisan; it('download default browser version', function () { From aa00ff2d382c742114e00edfa3d3544ce6d55cdf Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 15:45:31 -0600 Subject: [PATCH 27/31] test: add chrome driver test --- app/Commands/InstallCommand.php | 9 ++ app/Commands/InstallDriverCommand.php | 19 +-- tests/Feature/InstallDriverCommandTest.php | 130 +++++++++++++++++++++ 3 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 tests/Feature/InstallDriverCommandTest.php diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index 6efba9b..8768e99 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -78,4 +78,13 @@ public function message(string $text, string $type = 'line'): void

HTML); } + + protected function getDownloadDirectory(): string + { + if (! $this->option('path')) { + return $this->getBasePath(); + } + + return $this->option('path'); + } } diff --git a/app/Commands/InstallDriverCommand.php b/app/Commands/InstallDriverCommand.php index ce7a247..0b0bffc 100644 --- a/app/Commands/InstallDriverCommand.php +++ b/app/Commands/InstallDriverCommand.php @@ -2,6 +2,7 @@ namespace App\Commands; +use App\GoogleDownloadable; use App\OperatingSystem; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; @@ -19,7 +20,8 @@ class InstallDriverCommand extends InstallCommand */ protected $signature = 'install:driver {--ver=115.0.5763.0 : Install specific version} - {--latest : Install the latest version}'; + {--latest : Install the latest version} + {--path= : Specify the path where to download the driver}'; /** * The description of the command. @@ -52,28 +54,19 @@ public function handle(): int $version = $downloadable->getVersion(); - $filename = $this->getBasePath('chromedriver-'.$this->platforms[$os].'.zip'); - try { - $result = true; - spin( - callback: fn () => download($downloadable->getChromeDriverURL($this->platforms[$os]), $filename), + callback: fn () => $downloadable->download(GoogleDownloadable::DRIVER, $this->getDownloadDirectory(), $this->platforms[$os], true), message: "Downloading Google Chrome Driver [$version]" ); - spin( - callback: fn () => unzip($filename), - message: 'Unzipping Google Chrome Driver', - ); - } catch (\Exception $e) { + $this->message("Google Chrome Driver unzip it on [{$this->getDownloadDirectory()}]", 'info'); + } catch (\Throwable $e) { Log::error($e->getMessage()); error("Unable to download/install Google Chrome Driver [$version]"); return self::FAILURE; - } finally { - File::delete($filename); } outro("Google Chrome Driver [$version] downloaded"); diff --git a/tests/Feature/InstallDriverCommandTest.php b/tests/Feature/InstallDriverCommandTest.php new file mode 100644 index 0000000..aba3022 --- /dev/null +++ b/tests/Feature/InstallDriverCommandTest.php @@ -0,0 +1,130 @@ +shouldReceive('append') + ->andReturn(true); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('115.0.5763.0'); + + $downloadable->shouldReceive('download'); + + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:driver') + ->doesntExpectOutputToContain("There' no versions available for [115.0.5763.0]") + ->expectsOutputToContain('Downloading Google Chrome Driver [115.0.5763.0]') + ->expectsOutputToContain('Google Chrome Driver [115.0.5763.0] downloaded') + ->expectsOutputToContain('Google Chrome Driver unzip it on') + ->assertSuccessful(); +}); + +it('download the latest driver version', function () { + Http::fake(); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('200.0.0.0'); + + $downloadable->shouldReceive('download'); + + $downloadable->shouldReceive('getChromeBrowserURL') + ->andReturn('https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/200.0.0.0/linux64/chrome-linux64.zip'); + + $google->shouldReceive('getLatestVersion') + ->andReturn($downloadable); + + artisan('install:driver --latest') + ->doesntExpectOutputToContain("There' no versions available for [200.0.0.0]") + ->expectsOutputToContain('Downloading Google Chrome Driver [200.0.0.0]') + ->expectsOutputToContain('Google Chrome Driver [200.0.0.0] downloaded') + ->assertSuccessful(); +}); + +it('it download the driver version [118.0.5672.0]', function () { + Http::fake(); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('118.0.5672.0'); + + $downloadable->shouldReceive('download'); + + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:driver --ver=118.0.5672.0') + ->doesntExpectOutputToContain("There' no versions available for [118.0.5672.0]") + ->expectsOutputToContain('Downloading Google Chrome Driver [118.0.5672.0]') + ->expectsOutputToContain('Google Chrome Driver [118.0.5672.0] downloaded') + ->assertSuccessful(); +}); + +it('download the driver on other path', function () { + Http::fake(); + File::partialMock() + ->shouldReceive('append') + ->andReturn(true); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable->shouldReceive('getVersion') + ->andReturn('200.0.0.0'); + + $downloadable->shouldReceive('download'); + + $google->shouldReceive('getLatestVersion') + ->andReturn($downloadable); + + artisan('install:driver --latest --path=/some/dir/to/download') + ->doesntExpectOutputToContain("There' no versions available for [200.0.0.0]") + ->expectsOutputToContain('Downloading Google Chrome Driver [200.0.0.0]') + ->expectsOutputToContain('Google Chrome Driver [200.0.0.0] downloaded') + ->expectsOutputToContain('Google Chrome Driver unzip it on [/some/dir/to/download]') + ->assertSuccessful(); +}); + +it('try to download a pre-existing driver version', function () { + Http::fake(); + + Log::partialMock() + ->shouldReceive('error') + ->with('The file [chromedriver-linux.zip] already exists'); + + $google = GoogleForTesting::partialMock(); + $downloadable = Mockery::mock(GoogleDownloadable::class); + + $downloadable + ->shouldReceive('getVersion') + ->andReturn('115.0.5763.0'); + + $downloadable + ->shouldReceive('download') + ->andThrow(\Exception::class, 'The file [chromedriver-linux.zip] already exists'); + + $google->shouldReceive('getVersion') + ->andReturn($downloadable); + + artisan('install:driver') + ->doesntExpectOutputToContain('Google Chrome Driver [115.0.5763.0] downloaded') + ->assertFailed(); +}); From 02aff2c2af5ba32ef9c958874056fe62e4ca5480 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 15:45:55 -0600 Subject: [PATCH 28/31] refactor: remove unnecessary code --- app/Commands/InstallBrowserCommand.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 9814a79..178f5c7 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -71,13 +71,4 @@ public function handle(): int return self::SUCCESS; } - - protected function getDownloadDirectory(): string - { - if (! $this->option('path')) { - return $this->getBasePath(); - } - - return $this->option('path'); - } } From 15d1128018e76903376a8de9c2a701b0b0372393 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 16:47:10 -0600 Subject: [PATCH 29/31] refactor: remove repeated code --- app/Commands/InstallBrowserCommand.php | 50 +-------------- app/Commands/InstallCommand.php | 84 ++++++++++++++++++++++++-- app/Commands/InstallDriverCommand.php | 56 +---------------- 3 files changed, 86 insertions(+), 104 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 178f5c7..057bfb5 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -11,15 +11,9 @@ class InstallBrowserCommand extends InstallCommand { - /** - * The signature of the command. - * - * @var string - */ - protected $signature = 'install:browser - {--ver=115.0.5763.0 : Install specific version} - {--latest : Install the latest version} - {--path= : Specify the path where to download the browser}'; + protected int $component = GoogleDownloadable::BROWSER; + + protected $name = 'install:browser'; /** * The description of the command. @@ -28,47 +22,9 @@ class InstallBrowserCommand extends InstallCommand */ protected $description = 'Install Google Browser'; - protected array $platforms = [ - 'linux' => 'linux64', - 'mac-arm' => 'mac-arm64', - 'mac-intel' => 'mac-x64', - 'win' => 'win64', - ]; - /** * Execute the console command. * * @return mixed */ - public function handle(): int - { - if (empty($downloadable = $this->version())) { - error("There' no versions available for [{$this->option('ver')}]"); - - return self::FAILURE; - } - - $os = OperatingSystem::id(); - - $version = $downloadable->getVersion(); - - try { - spin( - callback: fn () => $downloadable->download(GoogleDownloadable::BROWSER, $this->getDownloadDirectory(), $this->platforms[$os], true), - message: "Downloading Google Chrome Browser [$version]" - ); - - $this->message("Google Chrome Browser unzip it on [{$this->getDownloadDirectory()}]", 'info'); - } catch (\Throwable $e) { - Log::error($e->getMessage()); - - error("Unable to download/install Google Chrome Browser [$version]"); - - return self::FAILURE; - } - - $this->message("Google Chrome Browser [$version] downloaded", 'success'); - - return self::SUCCESS; - } } diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index 8768e99..d786635 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -4,10 +4,14 @@ use App\Facades\GoogleForTesting; use App\GoogleDownloadable; +use App\OperatingSystem; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; +use Symfony\Component\Console\Input\InputOption; +use function Laravel\Prompts\error; use function Laravel\Prompts\search; use function Laravel\Prompts\spin; use function Laravel\Prompts\warning; @@ -15,6 +19,72 @@ abstract class InstallCommand extends Command { + protected int $component; + + protected array $platforms = [ + 'linux' => 'linux64', + 'mac-arm' => 'mac-arm64', + 'mac-intel' => 'mac-x64', + 'win' => 'win64', + ]; + + protected function configure(): void + { + $this->addOption( + 'ver', + null, + InputOption::VALUE_OPTIONAL, + 'Install a specific version', + '115.0.5763.0', + ); + + $this->addOption( + 'latest', + null, + InputOption::VALUE_NONE, + 'Install the latest version', + ); + + $this->addOption( + 'path', + null, + InputOption::VALUE_OPTIONAL, + 'Specify the path where to download it', + ); + } + + public function handle(): int + { + if (empty($downloadable = $this->version())) { + error("There' no versions available for [{$this->option('ver')}]"); + + return self::FAILURE; + } + + $os = OperatingSystem::id(); + + $version = $downloadable->getVersion(); + + try { + spin( + callback: fn () => $downloadable->download(GoogleDownloadable::BROWSER, $this->getDownloadDirectory(), $this->platforms[$os], true), + message: "Downloading Google Chrome {$this->getComponentName()} [$version]" + ); + + $this->message("Google Chrome {$this->getComponentName()} unzip it on [{$this->getDownloadDirectory()}]", 'info'); + } catch (\Throwable $e) { + Log::error($e->getMessage()); + + error("Unable to download/install Google Chrome {$this->getComponentName()} [$version]"); + + return self::FAILURE; + } + + $this->message("Google Chrome {$this->getComponentName()} [$version] downloaded", 'success'); + + return self::SUCCESS; + } + protected function version(): ?GoogleDownloadable { if ($this->option('latest')) { @@ -81,10 +151,16 @@ public function message(string $text, string $type = 'line'): void protected function getDownloadDirectory(): string { - if (! $this->option('path')) { - return $this->getBasePath(); - } + return $this->option('path') ?? $this->getBasePath(); + } - return $this->option('path'); + protected function getComponent(): int + { + return $this->component; + } + + protected function getComponentName(): string + { + return $this->getComponent() === GoogleDownloadable::BROWSER ? 'Browser' : 'Driver'; } } diff --git a/app/Commands/InstallDriverCommand.php b/app/Commands/InstallDriverCommand.php index 0b0bffc..3bf675f 100644 --- a/app/Commands/InstallDriverCommand.php +++ b/app/Commands/InstallDriverCommand.php @@ -13,15 +13,9 @@ class InstallDriverCommand extends InstallCommand { - /** - * The signature of the command. - * - * @var string - */ - protected $signature = 'install:driver - {--ver=115.0.5763.0 : Install specific version} - {--latest : Install the latest version} - {--path= : Specify the path where to download the driver}'; + protected int $component = GoogleDownloadable::DRIVER; + + protected $name = 'install:driver'; /** * The description of the command. @@ -29,48 +23,4 @@ class InstallDriverCommand extends InstallCommand * @var string */ protected $description = 'Install Google Driver'; - - protected array $platforms = [ - 'linux' => 'linux64', - 'mac-arm' => 'mac-arm64', - 'mac-intel' => 'mac-x64', - 'win' => 'win64', - ]; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle(): int - { - if (empty($downloadable = $this->version())) { - error("There' no versions available for [{$this->option('ver')}]"); - - return self::FAILURE; - } - - $os = OperatingSystem::id(); - - $version = $downloadable->getVersion(); - - try { - spin( - callback: fn () => $downloadable->download(GoogleDownloadable::DRIVER, $this->getDownloadDirectory(), $this->platforms[$os], true), - message: "Downloading Google Chrome Driver [$version]" - ); - - $this->message("Google Chrome Driver unzip it on [{$this->getDownloadDirectory()}]", 'info'); - } catch (\Throwable $e) { - Log::error($e->getMessage()); - - error("Unable to download/install Google Chrome Driver [$version]"); - - return self::FAILURE; - } - - outro("Google Chrome Driver [$version] downloaded"); - - return self::SUCCESS; - } } From 47d2f200ce19e76f15d98cf34bdeeddf56fec765 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 16:47:23 -0600 Subject: [PATCH 30/31] test: remove unnecessary code --- tests/Feature/InstallBrowserCommandTest.php | 6 ------ tests/Feature/InstallDriverCommandTest.php | 6 ------ 2 files changed, 12 deletions(-) diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 71f970e..5fb71ef 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -10,9 +10,6 @@ it('download default browser version', function () { Http::fake(); - File::partialMock() - ->shouldReceive('append') - ->andReturn(true); $google = GoogleForTesting::partialMock(); $downloadable = Mockery::mock(GoogleDownloadable::class); @@ -80,9 +77,6 @@ it('download the browser on other path', function () { Http::fake(); - File::partialMock() - ->shouldReceive('append') - ->andReturn(true); $google = GoogleForTesting::partialMock(); $downloadable = Mockery::mock(GoogleDownloadable::class); diff --git a/tests/Feature/InstallDriverCommandTest.php b/tests/Feature/InstallDriverCommandTest.php index aba3022..be35c7f 100644 --- a/tests/Feature/InstallDriverCommandTest.php +++ b/tests/Feature/InstallDriverCommandTest.php @@ -10,9 +10,6 @@ it('download default driver version', function () { Http::fake(); - File::partialMock() - ->shouldReceive('append') - ->andReturn(true); $google = GoogleForTesting::partialMock(); $downloadable = Mockery::mock(GoogleDownloadable::class); @@ -80,9 +77,6 @@ it('download the driver on other path', function () { Http::fake(); - File::partialMock() - ->shouldReceive('append') - ->andReturn(true); $google = GoogleForTesting::partialMock(); $downloadable = Mockery::mock(GoogleDownloadable::class); From d2128f62ded061ef8e3703568eac4213351446af Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 20 Oct 2023 16:47:41 -0600 Subject: [PATCH 31/31] chore: fix styling --- app/Commands/InstallBrowserCommand.php | 5 ----- app/Commands/InstallCommand.php | 2 +- app/Commands/InstallDriverCommand.php | 7 ------- tests/Feature/InstallBrowserCommandTest.php | 1 - tests/Feature/InstallDriverCommandTest.php | 1 - 5 files changed, 1 insertion(+), 15 deletions(-) diff --git a/app/Commands/InstallBrowserCommand.php b/app/Commands/InstallBrowserCommand.php index 057bfb5..6ad46ce 100644 --- a/app/Commands/InstallBrowserCommand.php +++ b/app/Commands/InstallBrowserCommand.php @@ -3,11 +3,6 @@ namespace App\Commands; use App\GoogleDownloadable; -use App\OperatingSystem; -use Illuminate\Support\Facades\Log; - -use function Laravel\Prompts\error; -use function Laravel\Prompts\spin; class InstallBrowserCommand extends InstallCommand { diff --git a/app/Commands/InstallCommand.php b/app/Commands/InstallCommand.php index d786635..51bc266 100644 --- a/app/Commands/InstallCommand.php +++ b/app/Commands/InstallCommand.php @@ -9,8 +9,8 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; - use Symfony\Component\Console\Input\InputOption; + use function Laravel\Prompts\error; use function Laravel\Prompts\search; use function Laravel\Prompts\spin; diff --git a/app/Commands/InstallDriverCommand.php b/app/Commands/InstallDriverCommand.php index 3bf675f..e4204e3 100644 --- a/app/Commands/InstallDriverCommand.php +++ b/app/Commands/InstallDriverCommand.php @@ -3,13 +3,6 @@ namespace App\Commands; use App\GoogleDownloadable; -use App\OperatingSystem; -use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Log; - -use function Laravel\Prompts\error; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\spin; class InstallDriverCommand extends InstallCommand { diff --git a/tests/Feature/InstallBrowserCommandTest.php b/tests/Feature/InstallBrowserCommandTest.php index 5fb71ef..1d4c412 100644 --- a/tests/Feature/InstallBrowserCommandTest.php +++ b/tests/Feature/InstallBrowserCommandTest.php @@ -2,7 +2,6 @@ use App\Facades\GoogleForTesting; use App\GoogleDownloadable; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; diff --git a/tests/Feature/InstallDriverCommandTest.php b/tests/Feature/InstallDriverCommandTest.php index be35c7f..11bcca7 100644 --- a/tests/Feature/InstallDriverCommandTest.php +++ b/tests/Feature/InstallDriverCommandTest.php @@ -2,7 +2,6 @@ use App\Facades\GoogleForTesting; use App\GoogleDownloadable; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log;