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/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index af6c142..2f2ecbb 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; @@ -93,8 +94,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 +122,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 +147,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 +180,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 +208,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 +232,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"); @@ -307,10 +338,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..2afb480 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -2,6 +2,7 @@ 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; @@ -9,6 +10,8 @@ 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(); @@ -18,7 +21,7 @@ ->assertSuccessful(); Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); -}); +})->skipOnWindows(); it('stop a Chrome Driver server', function () { Process::fake([ @@ -35,7 +38,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 +57,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 +75,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 +86,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 +94,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 +102,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 +110,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 +119,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 +146,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 +169,4 @@ ->doesntExpectOutputToContain("There' no servers available to list") ->expectsTable(['PID', 'PORT'], $data->map(fn ($port, $pid) => [$pid, $port])->values()) ->assertSuccessful(); -}); +})->skipOnWindows();