Skip to content

Commit

Permalink
[MERGE]: develop to main
Browse files Browse the repository at this point in the history
merge: develop to main
  • Loading branch information
asciito authored Dec 1, 2023
2 parents 7a74205 + 3156d74 commit db2c7b7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 43 additions & 9 deletions app/Commands/DriverManagerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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]");
Expand All @@ -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]");
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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()],
);
}
Expand Down
25 changes: 14 additions & 11 deletions tests/Feature/ManageDriverCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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 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();

Expand All @@ -18,7 +21,7 @@
->assertSuccessful();

Process::assertRan('./chromedriver --log-level=ALL --port=9515 &');
});
})->skipOnWindows();

it('stop a Chrome Driver server', function () {
Process::fake([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand All @@ -83,31 +86,31 @@
->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();

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();

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();

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();
Expand All @@ -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'];
Expand All @@ -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([
Expand All @@ -166,4 +169,4 @@
->doesntExpectOutputToContain("There' no servers available to list")
->expectsTable(['PID', 'PORT'], $data->map(fn ($port, $pid) => [$pid, $port])->values())
->assertSuccessful();
});
})->skipOnWindows();

0 comments on commit db2c7b7

Please sign in to comment.