From 87b6ebaab229a4901ad5d44dbfc24dc988fdd454 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 1 Dec 2023 11:54:42 -0600 Subject: [PATCH 1/5] Add code to delete the Google testing directory --- app/Commands/DriverManagerCommand.php | 8 ++++++-- tests/Feature/ManageDriverCommandTest.php | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index af6c142..fa4b185 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -7,6 +7,7 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Process\ProcessResult; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; @@ -307,10 +308,13 @@ protected function getBinary(): string protected function getChromeDriverDirectory(): string { + $directory = join_paths(getenv('HOME'), '.google-for-testing'); + + File::ensureDirectoryExists($directory); + return $this->option('path') ?? join_paths( - getenv('HOME'), - '.google-for-testing', + $directory, 'chromedriver-'.$this->platforms[OperatingSystem::id()], ); } diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 3e1db3b..283cb64 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -5,10 +5,13 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; +use Illuminate\Support\Facades\File; use Laravel\Prompts\Prompt; use function Pest\Laravel\artisan; +afterAll(fn () => File::deleteDirectory(join_paths(getenv('HOME'), '.google-for-testing'))); + it('start a Chrome Driver server', function () { Process::fake(); From 1092dc706d879bc2d89e8e2d41e2e831afcac06d Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 1 Dec 2023 18:08:58 +0000 Subject: [PATCH 2/5] chore: fix styling --- tests/Feature/ManageDriverCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 283cb64..5c5727c 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -2,10 +2,10 @@ use Illuminate\Process\PendingProcess; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; -use Illuminate\Support\Facades\File; use Laravel\Prompts\Prompt; use function Pest\Laravel\artisan; From 162b7eae0856df35ec7600d375c64baeece01449 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 1 Dec 2023 12:31:17 -0600 Subject: [PATCH 3/5] chore: refactor methods for windows --- app/Commands/DriverManagerCommand.php | 44 ++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index af6c142..4cb44a4 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -93,8 +93,14 @@ public function handle(): int protected function start(string $port): int { - if ($pid = $this->getProcessID($port)) { - warning("[PID: $pid]: There's a server running already on port [$port]"); + try { + if ($pid = $this->getProcessID($port)) { + warning("[PID: $pid]: There's a server running already on port [$port]"); + + return self::FAILURE; + } + } catch (\Exception) { + warning("We are running on windows and we cannot start the server"); return self::FAILURE; } @@ -115,7 +121,13 @@ public function stop(string $port): int { intro("Stopping Google Chrome Driver on port [$port]"); - $pid = $this->getProcessID($port); + try { + $pid = $this->getProcessID($port); + } catch (\Exception) { + warning("We are running on windows and we cannot stop the server"); + + return self::FAILURE; + } if (empty($pid)) { warning("There's no server to stop on port [$port]"); @@ -134,7 +146,13 @@ protected function restart(string $port): int { intro("Restarting Google Chrome Driver on port [$port]"); - $pid = $this->getProcessID($port); + try { + $pid = $this->getProcessID($port); + } catch (\Exception) { + warning("We are running on windows and we cannot restart the server"); + + return self::FAILURE; + } if (empty($pid)) { warning("There's no server to restart on port [$port]"); @@ -161,7 +179,7 @@ protected function status(string $port): int warning('We are running on windows and we cannot be sure if the server is running, but we will try to check'); } - if (empty($pid)) { + if (! $this->onWindows() && empty($pid)) { warning("There's no server available on port [$port]"); return self::FAILURE; @@ -189,7 +207,13 @@ protected function list(): int { info('Listing all the servers available'); - $result = $this->getProcessIDs(); + try { + $result = $this->getProcessIDs(); + } catch (\Exception) { + warning("We are running on windows and we cannot list the servers"); + + return self::FAILURE; + } if (empty($result)) { warning("There' no servers available to list"); @@ -207,7 +231,13 @@ protected function list(): int */ protected function kill(): int { - $pids = $this->getProcessIDs(); + try { + $pids = $this->getProcessIDs(); + } catch (\Exception) { + warning("We are running on windows and we cannot stop the servers"); + + return self::FAILURE; + } if (empty($pids)) { warning("There' no servers to kill"); From 9edc8c049db2d8976f5983b3a70a56f5f14619c4 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 1 Dec 2023 12:45:46 -0600 Subject: [PATCH 4/5] fix: add matrix to test on windows also --- .github/workflows/run-tests.yml | 7 ++++++- tests/Feature/ManageDriverCommandTest.php | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index a5419b6..c2b4514 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,7 +8,12 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + steps: - name: Checkout Code uses: actions/checkout@v3 diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 3e1db3b..610e7d1 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -18,7 +18,7 @@ ->assertSuccessful(); Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); -}); +})->skipOnWindows(); it('stop a Chrome Driver server', function () { Process::fake([ @@ -35,7 +35,7 @@ Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2,$13}'"); Process::assertRan('kill -9 10101'); -}); +})->skipOnWindows(); it('restart a Chrome Driver server', function () { Process::fake([ @@ -54,7 +54,7 @@ Process::assertRan('kill -9 10101'); Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); -}); +})->skipOnWindows(); test('status of Chrome Driver server', function () { Process::fake([ @@ -72,7 +72,7 @@ ->expectsOutputToContain('Google Chrome server status: [OK]') ->doesntExpectOutputToContain("There's no server available on port [9515]") ->assertSuccessful(); -}); +})->skipOnWindows(); it('can\'t start a new Chrome Driver server if there\'s one already started', function () { Process::fake([ @@ -83,7 +83,7 @@ ->expectsOutputToContain("[PID: 10101]: There's a server running already on port [9515]") ->doesntExpectOutput('Stating Google Chrome Driver on port [9515]') ->assertFailed(); -}); +})->skipOnWindows(); it('can\'t stop a Chrome Driver server if there\'s no server already started', function () { Process::fake(); @@ -91,7 +91,7 @@ artisan('manage:driver', ['action' => 'stop']) ->expectsOutputToContain("There's no server to stop") ->assertFailed(); -}); +})->skipOnWindows(); it('can\'t restart a Chrome Driver server if there\'s no server already started', function () { Process::fake(); @@ -99,7 +99,7 @@ artisan('manage:driver', ['action' => 'stop']) ->expectsOutputToContain("There's no server to stop on port [9515]") ->assertFailed(); -}); +})->skipOnWindows(); it('can\'t get the status of Chrome Driver server if there\'s no server already started', function () { Process::fake(); @@ -107,7 +107,7 @@ artisan('manage:driver', ['action' => 'restart']) ->expectsOutputToContain("There's no server to restart on port [9515]") ->assertFailed(); -}); +})->skipOnWindows(); it('start 4 Chrome Driver servers', function () { Process::fake(); @@ -116,7 +116,7 @@ ->assertSuccessful(); Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port=\d+ &$/', $process->command), 4); -}); +})->skipOnWindows(); it('stop all the available Chrome Driver servers', function () { $data = ['9991 1111', '9992 1112', '9993 1113', '9994 1114']; @@ -143,7 +143,7 @@ Process::assertRan(fn (PendingProcess $process) => Str::match('/^ps aux .*/', $process->command)); Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^kill -9 \d+/', $process->command), 4); -}); +})->skipOnWindows(); it('list all the available Chrome Driver servers', function () { $data = collect([ @@ -166,4 +166,4 @@ ->doesntExpectOutputToContain("There' no servers available to list") ->expectsTable(['PID', 'PORT'], $data->map(fn ($port, $pid) => [$pid, $port])->values()) ->assertSuccessful(); -}); +})->skipOnWindows(); From fc8d236ddeca2406f27e2e0204d04751da366ba8 Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 1 Dec 2023 18:46:20 +0000 Subject: [PATCH 5/5] chore: fix styling --- app/Commands/DriverManagerCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 4cb44a4..e48b886 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -100,7 +100,7 @@ protected function start(string $port): int return self::FAILURE; } } catch (\Exception) { - warning("We are running on windows and we cannot start the server"); + warning('We are running on windows and we cannot start the server'); return self::FAILURE; } @@ -124,7 +124,7 @@ public function stop(string $port): int try { $pid = $this->getProcessID($port); } catch (\Exception) { - warning("We are running on windows and we cannot stop the server"); + warning('We are running on windows and we cannot stop the server'); return self::FAILURE; } @@ -149,7 +149,7 @@ protected function restart(string $port): int try { $pid = $this->getProcessID($port); } catch (\Exception) { - warning("We are running on windows and we cannot restart the server"); + warning('We are running on windows and we cannot restart the server'); return self::FAILURE; } @@ -210,7 +210,7 @@ protected function list(): int try { $result = $this->getProcessIDs(); } catch (\Exception) { - warning("We are running on windows and we cannot list the servers"); + warning('We are running on windows and we cannot list the servers'); return self::FAILURE; } @@ -234,7 +234,7 @@ protected function kill(): int try { $pids = $this->getProcessIDs(); } catch (\Exception) { - warning("We are running on windows and we cannot stop the servers"); + warning('We are running on windows and we cannot stop the servers'); return self::FAILURE; }