From 7bb8a99fe64aec2c12525adbb9886a498dac1156 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:04:33 -0600 Subject: [PATCH 01/24] add basic actions and tests --- app/Commands/ManagerDriverCommand.php | 161 ++++++++++++++++++++++ tests/Feature/ManageDriverCommandTest.php | 133 ++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 app/Commands/ManagerDriverCommand.php create mode 100644 tests/Feature/ManageDriverCommandTest.php diff --git a/app/Commands/ManagerDriverCommand.php b/app/Commands/ManagerDriverCommand.php new file mode 100644 index 0000000..7d6472f --- /dev/null +++ b/app/Commands/ManagerDriverCommand.php @@ -0,0 +1,161 @@ + 'linux64', + 'mac-arm' => 'mac-arm64', + 'mac-intel' => 'mac-x64', + 'win' => 'win64', + ]; + + public function handle(): int + { + $action = $this->argument('action') ?? select('Select an action to perform', [ + 'start' => 'Start a new server', + 'stop' => 'Stop a server', + 'restart' => 'Restart a server', + 'status' => 'Status of a server', + ]); + + $result = match ($action) { + 'start' => $this->start(), + 'stop' => $this->stop(), + 'restart' => $this->restart(), + 'status' => $this->status(), + }; + + return $result; + } + + protected function start(): int + { + $process = Process::path($this->getChromeDriverDirectory()) + ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + if (filled($process->output())) { + warning("[PID: ".trim($process->output())."]: There's a server running already on port [9515]"); + + return self::FAILURE; + } + + intro('Stating Google Chrome Driver on port [9515]'); + + Process::path($this->getChromeDriverDirectory()) + ->run('./chromedriver --log-level=ALL --port=9515 &'); + + info('Google Chrome Driver server is up and running'); + + return self::SUCCESS; + } + + public function stop(): int + { + intro('Stopping Google Chrome Driver on port [9515]'); + + $process = Process::path($this->getChromeDriverDirectory()) + ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + $pid = trim($process->output()); + + if (empty($pid)) { + warning("There's no server to stop on port [9515]"); + + return self::FAILURE; + } + + Process::path($this->getChromeDriverDirectory()) + ->run("kill -9 $pid"); + + info('Google Chrome Driver server stopped on port [9515]'); + + return self::SUCCESS; + } + + protected function restart(): int + { + intro('Restarting Google Chrome Driver on port [9515]'); + + $process = Process::path($this->getChromeDriverDirectory()) + ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + $pid = trim($process->output()); + + if (empty($pid)) { + info("There's no server to restart on port [9515]"); + + return self::FAILURE; + } + + Process::path($this->getChromeDriverDirectory()) + ->run("kill -9 $pid"); + + Process::path($this->getChromeDriverDirectory()) + ->run('./chromedriver --log-level=ALL --port=9515 &'); + + info('Google Chrome Driver server restarted on port [9515]'); + + return self::SUCCESS; + } + + protected function status(): int + { + intro("Getting Google Chrome Driver status on port [9515]"); + + $process = Process::path($this->getChromeDriverDirectory()) + ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + $pid = trim($process->output()); + + if (empty($pid)) { + info("There's no server available on port [9515]"); + + return self::FAILURE; + } + + $response = Http::get('http://localhost:9515/status'); + + $data = $response->json('value'); + + if (array_key_exists('error', $data) || ! $data['ready']) { + error("There was a problem, we cannot establish connection with the server"); + + return self::FAILURE; + } + + info("Google Chrome server status: [OK]"); + + return self::SUCCESS; + } + + protected function getChromeDriverDirectory(): string + { + return $this->option('path') + ?? join_paths( + getenv('HOME'), + '.google-for-testing', + 'chromedriver-' . $this->platforms[OperatingSystem::id()], + ); + } +} diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php new file mode 100644 index 0000000..73091a7 --- /dev/null +++ b/tests/Feature/ManageDriverCommandTest.php @@ -0,0 +1,133 @@ + 'start']) + ->expectsOutputToContain('Stating Google Chrome Driver on port [9515]') + ->expectsOutputToContain('Google Chrome Driver server is up and running') + ->assertSuccessful(); + + Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); +}); + +it('stop a Chrome Driver server', function () { + Process::fake([ + 'ps aux *' => "10101", + '*' => Process::result(), + ]); + + artisan('manage:driver', ['action' => 'stop']) + ->expectsOutputToContain('Stopping Google Chrome Driver on port [9515]') + ->expectsOutputToContain('Google Chrome Driver server stopped') + ->doesntExpectOutputToContain("There's no server to stop on port [9515]") + ->assertSuccessful(); + + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + Process::assertRan("kill -9 10101"); +}); + +it('restart a Chrome Driver server', function () { + Process::fake([ + 'ps aux *' => "10101", + '*' => Process::result(), + ]); + + artisan('manage:driver', ['action' => 'restart']) + ->expectsOutputToContain('Restarting Google Chrome Driver on port [9515]') + ->expectsOutputToContain('Google Chrome Driver server restarted') + ->doesntExpectOutputToContain("There's no server to restart on port [9515]") + ->assertSuccessful(); + + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + + Process::assertRan("kill -9 10101"); + + Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); +}); + +test('status of Chrome Driver server', function () { + Process::fake([ + '*' => Process::result('10101') + ]); + + Http::fake([ + '*' => Http::response(["value" => [ + "ready" => true, + ]], headers: ['Content-Type' => 'application/json']) + ]); + + artisan('manage:driver', ['action' => 'status']) + ->expectsOutputToContain('Getting Google Chrome Driver status on port [9515]') + ->expectsOutputToContain('Google Chrome server status: [OK]') + ->doesntExpectOutputToContain("There's no server available on port [9515]") + ->assertSuccessful(); +}); + +it('can\'t start a new Chrome Driver server if there\'s one already started', function () { + Process::fake([ + "*" => Process::result('10101'), + ]); + + artisan('manage:driver', ['action' => 'start']) + ->expectsOutputToContain("[PID: 10101]: There's a server running already on port [9515]") + ->doesntExpectOutput('Stating Google Chrome Driver on port [9515]') + ->assertFailed(); +}); + +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(); +}); + +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(); +})->todo(); + +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(); +}); + +it('start 4 Chrome Driver servers', function () { + +})->todo(); + +it('stop all the available Chrome Driver servers', function () { + +})->todo(); + +it('restart all the available Chrome Driver servers', function () { + +})->todo(); + +it('get the status of all the available Chrome Driver servers', function () { + +})->todo(); + +it('list all the available Chrome Driver servers', function () { + +})->todo(); From 6f5788764e9ecd29537e8fb1f53323ac449f1595 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:05:21 -0600 Subject: [PATCH 02/24] rename command class --- .../{ManagerDriverCommand.php => DriverManagerCommand.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename app/Commands/{ManagerDriverCommand.php => DriverManagerCommand.php} (99%) diff --git a/app/Commands/ManagerDriverCommand.php b/app/Commands/DriverManagerCommand.php similarity index 99% rename from app/Commands/ManagerDriverCommand.php rename to app/Commands/DriverManagerCommand.php index 7d6472f..a5d01ec 100644 --- a/app/Commands/ManagerDriverCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -15,7 +15,7 @@ use function Laravel\Prompts\warning; -class ManagerDriverCommand extends Command +class DriverManagerCommand extends Command { protected $signature = 'manage:driver {action? : The action you want to perform on Google\'s Chrome Driver} From 8e473d7271617a542c4a6ddb1857c269e4bf73de Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:07:58 -0600 Subject: [PATCH 03/24] fix code style issues --- app/Commands/DriverManagerCommand.php | 16 +++++------ tests/Feature/ManageDriverCommandTest.php | 33 +++++++++-------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index a5d01ec..54f22eb 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -6,15 +6,13 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; -use Illuminate\Support\Facades\Response; -use Illuminate\Support\Str; + use function Laravel\Prompts\error; use function Laravel\Prompts\info; use function Laravel\Prompts\intro; use function Laravel\Prompts\select; use function Laravel\Prompts\warning; - class DriverManagerCommand extends Command { protected $signature = 'manage:driver @@ -44,7 +42,7 @@ public function handle(): int 'stop' => $this->stop(), 'restart' => $this->restart(), 'status' => $this->status(), - }; + }; return $result; } @@ -55,7 +53,7 @@ protected function start(): int ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); if (filled($process->output())) { - warning("[PID: ".trim($process->output())."]: There's a server running already on port [9515]"); + warning('[PID: '.trim($process->output())."]: There's a server running already on port [9515]"); return self::FAILURE; } @@ -121,7 +119,7 @@ protected function restart(): int protected function status(): int { - intro("Getting Google Chrome Driver status on port [9515]"); + intro('Getting Google Chrome Driver status on port [9515]'); $process = Process::path($this->getChromeDriverDirectory()) ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); @@ -139,12 +137,12 @@ protected function status(): int $data = $response->json('value'); if (array_key_exists('error', $data) || ! $data['ready']) { - error("There was a problem, we cannot establish connection with the server"); + error('There was a problem, we cannot establish connection with the server'); return self::FAILURE; } - info("Google Chrome server status: [OK]"); + info('Google Chrome server status: [OK]'); return self::SUCCESS; } @@ -155,7 +153,7 @@ protected function getChromeDriverDirectory(): string ?? join_paths( getenv('HOME'), '.google-for-testing', - 'chromedriver-' . $this->platforms[OperatingSystem::id()], + 'chromedriver-'.$this->platforms[OperatingSystem::id()], ); } } diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 73091a7..41dad49 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -1,15 +1,8 @@ "10101", + 'ps aux *' => '10101', '*' => Process::result(), ]); @@ -37,12 +30,12 @@ Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - Process::assertRan("kill -9 10101"); + Process::assertRan('kill -9 10101'); }); it('restart a Chrome Driver server', function () { Process::fake([ - 'ps aux *' => "10101", + 'ps aux *' => '10101', '*' => Process::result(), ]); @@ -54,20 +47,20 @@ Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - Process::assertRan("kill -9 10101"); + Process::assertRan('kill -9 10101'); Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); }); test('status of Chrome Driver server', function () { Process::fake([ - '*' => Process::result('10101') + '*' => Process::result('10101'), ]); Http::fake([ - '*' => Http::response(["value" => [ - "ready" => true, - ]], headers: ['Content-Type' => 'application/json']) + '*' => Http::response(['value' => [ + 'ready' => true, + ]], headers: ['Content-Type' => 'application/json']), ]); artisan('manage:driver', ['action' => 'status']) @@ -79,13 +72,13 @@ it('can\'t start a new Chrome Driver server if there\'s one already started', function () { Process::fake([ - "*" => Process::result('10101'), + '*' => Process::result('10101'), ]); artisan('manage:driver', ['action' => 'start']) - ->expectsOutputToContain("[PID: 10101]: There's a server running already on port [9515]") - ->doesntExpectOutput('Stating Google Chrome Driver on port [9515]') - ->assertFailed(); + ->expectsOutputToContain("[PID: 10101]: There's a server running already on port [9515]") + ->doesntExpectOutput('Stating Google Chrome Driver on port [9515]') + ->assertFailed(); }); it('can\'t stop a Chrome Driver server if there\'s no server already started', function () { From 780f9b3920405e787339b4927d3cad3f5b0a6a79 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:30:45 -0600 Subject: [PATCH 04/24] remove todo from test --- 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 41dad49..b38f8ca 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -95,7 +95,7 @@ artisan('manage:driver', ['action' => 'stop']) ->expectsOutputToContain("There's no server to stop on port [9515]") ->assertFailed(); -})->todo(); +}); it('can\'t get the status of Chrome Driver server if there\'s no server already started', function () { Process::fake([]); From dd880d8a878ff5ccad66cc121b2676fd868b93b4 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:49:41 -0600 Subject: [PATCH 05/24] refactor code --- app/Commands/DriverManagerCommand.php | 56 ++++++++++++++------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 54f22eb..5cfa26e 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -4,9 +4,11 @@ use App\OperatingSystem; use Illuminate\Console\Command; +use Illuminate\Process\PendingProcess; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; +use Illuminate\Support\Str; use function Laravel\Prompts\error; use function Laravel\Prompts\info; use function Laravel\Prompts\intro; @@ -28,6 +30,12 @@ class DriverManagerCommand extends Command 'win' => 'win64', ]; + protected array $commands = [ + 'start' => "./chromedriver --log-level=ALL --port=9515 &", + 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'", + 'stop' => 'kill -9 {pid}' + ]; + public function handle(): int { $action = $this->argument('action') ?? select('Select an action to perform', [ @@ -49,19 +57,15 @@ public function handle(): int protected function start(): int { - $process = Process::path($this->getChromeDriverDirectory()) - ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - - if (filled($process->output())) { - warning('[PID: '.trim($process->output())."]: There's a server running already on port [9515]"); + if ($pid = $this->getProcessID()) { + warning("[PID: $pid]: There's a server running already on port [9515]"); return self::FAILURE; } intro('Stating Google Chrome Driver on port [9515]'); - Process::path($this->getChromeDriverDirectory()) - ->run('./chromedriver --log-level=ALL --port=9515 &'); + $this->command($this->commands['start'])->run(); info('Google Chrome Driver server is up and running'); @@ -72,10 +76,7 @@ public function stop(): int { intro('Stopping Google Chrome Driver on port [9515]'); - $process = Process::path($this->getChromeDriverDirectory()) - ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - - $pid = trim($process->output()); + $pid = $this->getProcessID(); if (empty($pid)) { warning("There's no server to stop on port [9515]"); @@ -83,8 +84,7 @@ public function stop(): int return self::FAILURE; } - Process::path($this->getChromeDriverDirectory()) - ->run("kill -9 $pid"); + $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); info('Google Chrome Driver server stopped on port [9515]'); @@ -95,10 +95,7 @@ protected function restart(): int { intro('Restarting Google Chrome Driver on port [9515]'); - $process = Process::path($this->getChromeDriverDirectory()) - ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - - $pid = trim($process->output()); + $pid = $this->getProcessID(); if (empty($pid)) { info("There's no server to restart on port [9515]"); @@ -106,11 +103,9 @@ protected function restart(): int return self::FAILURE; } - Process::path($this->getChromeDriverDirectory()) - ->run("kill -9 $pid"); + $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); - Process::path($this->getChromeDriverDirectory()) - ->run('./chromedriver --log-level=ALL --port=9515 &'); + $this->command($this->commands['start'])->run(); info('Google Chrome Driver server restarted on port [9515]'); @@ -121,10 +116,7 @@ protected function status(): int { intro('Getting Google Chrome Driver status on port [9515]'); - $process = Process::path($this->getChromeDriverDirectory()) - ->run("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); - - $pid = trim($process->output()); + $pid = $this->getProcessID(); if (empty($pid)) { info("There's no server available on port [9515]"); @@ -137,7 +129,7 @@ protected function status(): int $data = $response->json('value'); if (array_key_exists('error', $data) || ! $data['ready']) { - error('There was a problem, we cannot establish connection with the server'); + error('There was a problem, we cannot estAblish connection with the server'); return self::FAILURE; } @@ -147,6 +139,18 @@ protected function status(): int return self::SUCCESS; } + protected function command(string $cmd): PendingProcess + { + return Process::command($cmd)->path($this->getChromeDriverDirectory()); + } + + protected function getProcessID(): int|null + { + $process = $this->command($this->commands['pid'])->run(); + + return (int) trim($process->output()) ?: null; + } + protected function getChromeDriverDirectory(): string { return $this->option('path') From 32c1454dbf42cb7742198a753c5945b1364342c4 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 15:50:06 -0600 Subject: [PATCH 06/24] fix typo --- app/Commands/DriverManagerCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 5cfa26e..6f166c7 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -129,7 +129,7 @@ protected function status(): int $data = $response->json('value'); if (array_key_exists('error', $data) || ! $data['ready']) { - error('There was a problem, we cannot estAblish connection with the server'); + error('There was a problem, we cannot establish connection with the server'); return self::FAILURE; } From 23cbe48ca1cfb593d1ae83ce7fb1adbcb7da82e9 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 16:34:17 -0600 Subject: [PATCH 07/24] fix tests Add missing `Process::result` call, and remove unnecessary empty array from `Process::fake` call --- tests/Feature/ManageDriverCommandTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index b38f8ca..6b4cc01 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -35,7 +35,7 @@ it('restart a Chrome Driver server', function () { Process::fake([ - 'ps aux *' => '10101', + 'ps aux *' => Process::result('10101'), '*' => Process::result(), ]); @@ -82,7 +82,7 @@ }); it('can\'t stop a Chrome Driver server if there\'s no server already started', function () { - Process::fake([]); + Process::fake(); artisan('manage:driver', ['action' => 'stop']) ->expectsOutputToContain("There's no server to stop") @@ -90,7 +90,7 @@ }); it('can\'t restart a Chrome Driver server if there\'s no server already started', function () { - Process::fake([]); + Process::fake(); artisan('manage:driver', ['action' => 'stop']) ->expectsOutputToContain("There's no server to stop on port [9515]") @@ -98,7 +98,7 @@ }); it('can\'t get the status of Chrome Driver server if there\'s no server already started', function () { - Process::fake([]); + Process::fake(); artisan('manage:driver', ['action' => 'restart']) ->expectsOutputToContain("There's no server to restart on port [9515]") From 6b2cb86901e3a8f4d385d65a4897326f78894e16 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 22:35:04 +0000 Subject: [PATCH 08/24] chore: fix styling --- app/Commands/DriverManagerCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 6f166c7..7e34c27 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -7,8 +7,8 @@ use Illuminate\Process\PendingProcess; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; - use Illuminate\Support\Str; + use function Laravel\Prompts\error; use function Laravel\Prompts\info; use function Laravel\Prompts\intro; @@ -31,9 +31,9 @@ class DriverManagerCommand extends Command ]; protected array $commands = [ - 'start' => "./chromedriver --log-level=ALL --port=9515 &", + 'start' => './chromedriver --log-level=ALL --port=9515 &', 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'", - 'stop' => 'kill -9 {pid}' + 'stop' => 'kill -9 {pid}', ]; public function handle(): int @@ -144,7 +144,7 @@ protected function command(string $cmd): PendingProcess return Process::command($cmd)->path($this->getChromeDriverDirectory()); } - protected function getProcessID(): int|null + protected function getProcessID(): ?int { $process = $this->command($this->commands['pid'])->run(); From 1f0fc79a3779d9127b56c4dba11370d9ddebeced Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 18:04:14 -0600 Subject: [PATCH 09/24] add port option to manage more than one server --- app/Commands/DriverManagerCommand.php | 66 +++++++++++++++------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 7e34c27..c489a6c 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -19,6 +19,7 @@ class DriverManagerCommand extends Command { protected $signature = 'manage:driver {action? : The action you want to perform on Google\'s Chrome Driver} + {--p|port=* : The port from where to start a new server} {--path= : The absolute path of where to find Google Chrome Driver binary}'; protected $description = 'Manage Google Chrome Driver'; @@ -31,8 +32,8 @@ class DriverManagerCommand extends Command ]; protected array $commands = [ - 'start' => './chromedriver --log-level=ALL --port=9515 &', - 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'", + 'start' => './chromedriver --log-level=ALL --port={port} &', + 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port={port}' | awk '{print $2}'", 'stop' => 'kill -9 {pid}', ]; @@ -45,81 +46,86 @@ public function handle(): int 'status' => 'Status of a server', ]); - $result = match ($action) { - 'start' => $this->start(), - 'stop' => $this->stop(), - 'restart' => $this->restart(), - 'status' => $this->status(), + $cb = match ($action) { + 'start' => $this->start(...), + 'stop' => $this->stop(...), + 'restart' => $this->restart(...), + 'status' => $this->status(...), }; - return $result; + + $results = collect(['9515', ...$this->option('port')]) + ->unique() + ->map($cb(...)); + + return $results->reduce(fn (int $results, int $result) => $results && $result, self::FAILURE); } - protected function start(): int + protected function start(string $port): int { - if ($pid = $this->getProcessID()) { - warning("[PID: $pid]: There's a server running already on port [9515]"); + if ($pid = $this->getProcessID($port)) { + warning("[PID: $pid]: There's a server running already on port [$port]"); return self::FAILURE; } - intro('Stating Google Chrome Driver on port [9515]'); + intro("Stating Google Chrome Driver on port [$port]"); - $this->command($this->commands['start'])->run(); + $this->command(Str::replace('{port}', $port, $this->commands['start']))->run(); info('Google Chrome Driver server is up and running'); return self::SUCCESS; } - public function stop(): int + public function stop(string $port): int { - intro('Stopping Google Chrome Driver on port [9515]'); + intro("Stopping Google Chrome Driver on port [$port]"); - $pid = $this->getProcessID(); + $pid = $this->getProcessID($port); if (empty($pid)) { - warning("There's no server to stop on port [9515]"); + warning("There's no server to stop on port [$port]"); return self::FAILURE; } $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); - info('Google Chrome Driver server stopped on port [9515]'); + info("Google Chrome Driver server stopped on port [$port]"); return self::SUCCESS; } - protected function restart(): int + protected function restart(string $port): int { - intro('Restarting Google Chrome Driver on port [9515]'); + intro("Restarting Google Chrome Driver on port [$port]"); - $pid = $this->getProcessID(); + $pid = $this->getProcessID($port); if (empty($pid)) { - info("There's no server to restart on port [9515]"); + info("There's no server to restart on port [$port]"); return self::FAILURE; } $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); - $this->command($this->commands['start'])->run(); + $this->command(Str::replace('{port}', $port, $this->commands['start']))->run(); - info('Google Chrome Driver server restarted on port [9515]'); + info("Google Chrome Driver server restarted on port [$port]"); return self::SUCCESS; } - protected function status(): int + protected function status(string $port): int { - intro('Getting Google Chrome Driver status on port [9515]'); + intro("Getting Google Chrome Driver status on port [$port]"); - $pid = $this->getProcessID(); + $pid = $this->getProcessID($port); if (empty($pid)) { - info("There's no server available on port [9515]"); + info("There's no server available on port [$port]"); return self::FAILURE; } @@ -144,9 +150,9 @@ protected function command(string $cmd): PendingProcess return Process::command($cmd)->path($this->getChromeDriverDirectory()); } - protected function getProcessID(): ?int + protected function getProcessID(string $port): ?int { - $process = $this->command($this->commands['pid'])->run(); + $process = $this->command(Str::replace('{port}', $port, $this->commands['pid']))->run(); return (int) trim($process->output()) ?: null; } From 8cc26b532bf31a49a0824371f3dc38a4c98e802c Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 18:12:32 -0600 Subject: [PATCH 10/24] refactor code --- app/Commands/DriverManagerCommand.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index c489a6c..006f613 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -5,6 +5,7 @@ use App\OperatingSystem; use Illuminate\Console\Command; use Illuminate\Process\PendingProcess; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; @@ -24,6 +25,8 @@ class DriverManagerCommand extends Command protected $description = 'Manage Google Chrome Driver'; + protected string $port = '9515'; + protected array $platforms = [ 'linux' => 'linux64', 'mac-arm' => 'mac-arm64', @@ -46,19 +49,16 @@ public function handle(): int 'status' => 'Status of a server', ]); - $cb = match ($action) { + $callable = match ($action) { 'start' => $this->start(...), 'stop' => $this->stop(...), 'restart' => $this->restart(...), 'status' => $this->status(...), }; - - $results = collect(['9515', ...$this->option('port')]) - ->unique() - ->map($cb(...)); - - return $results->reduce(fn (int $results, int $result) => $results && $result, self::FAILURE); + return $this->getPorts()->map(fn (string $port) => $callable(port: $port)) + // Reduce the result of every callable to a single SUCCESS or FAILURE value + ->reduce(fn (int $results, int $result) => $results && $result, self::FAILURE); } protected function start(string $port): int @@ -157,6 +157,11 @@ protected function getProcessID(string $port): ?int return (int) trim($process->output()) ?: null; } + protected function getPorts(): Collection + { + return collect([$this->port, ...$this->option('port')])->unique()->filter(); + } + protected function getChromeDriverDirectory(): string { return $this->option('path') From 02d7d1154cf71806b8ed4f21ca94ad40b6742cd8 Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 18:54:23 -0600 Subject: [PATCH 11/24] add start more than one server at the time and kill all servers --- app/Commands/DriverManagerCommand.php | 32 ++++++++++++++++++++++- tests/Feature/ManageDriverCommandTest.php | 28 ++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 006f613..06ab5ac 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -37,6 +37,7 @@ class DriverManagerCommand extends Command protected array $commands = [ 'start' => './chromedriver --log-level=ALL --port={port} &', 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port={port}' | awk '{print $2}'", + 'pids' => "ps aux | grep '[c]hromedriver --log-level=ALL' | awk '{print $2}'", 'stop' => 'kill -9 {pid}', ]; @@ -47,6 +48,7 @@ public function handle(): int 'stop' => 'Stop a server', 'restart' => 'Restart a server', 'status' => 'Status of a server', + 'kill' => 'Kill all the servers available in the system', ]); $callable = match ($action) { @@ -54,9 +56,10 @@ public function handle(): int 'stop' => $this->stop(...), 'restart' => $this->restart(...), 'status' => $this->status(...), + 'kill' => $this->kill(...), }; - return $this->getPorts()->map(fn (string $port) => $callable(port: $port)) + return $this->getPorts()->map(fn (string $port) => $callable($port)) // Reduce the result of every callable to a single SUCCESS or FAILURE value ->reduce(fn (int $results, int $result) => $results && $result, self::FAILURE); } @@ -145,6 +148,33 @@ protected function status(string $port): int return self::SUCCESS; } + public function kill(): int + { + $result = $this->command($this->commands['pids'])->run(); + + if (empty($result->output())) { + warning("There' no servers to kill"); + + return self::FAILURE; + } + + if (! $this->confirm("Are you sure you want to do this?")) { + return self::SUCCESS; + } + + info('Stopping all the Google Chrome Driver servers that are available in the system'); + + collect(explode("\n", trim($result->output()))) + ->each(function (string $pid) { + info("Stopping Google Chrome Driver [PID: $pid]"); + + $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); + }); + + + return self::SUCCESS; + } + protected function command(string $cmd): PendingProcess { return Process::command($cmd)->path($this->getChromeDriverDirectory()); diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 6b4cc01..4b75807 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -1,8 +1,11 @@ todo(); + artisan('manage:driver', ['action' => 'start', '-p' => [9515, 9516, 9517, 9518]]) + ->assertSuccessful(); + + Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port=\d+ &$/', $process->command), 4); +}); it('stop all the available Chrome Driver servers', function () { + Process::fake([ + 'ps aux *' => Process::result(Arr::join([9991, 9992, 9993, 9994], "\n")), + '*' => Process::result(), + ]); -})->todo(); + artisan('manage:driver', ['action' => 'kill']) + ->expectsConfirmation('Are you sure you want to do this?', 'yes') + ->expectsOutputToContain('Stopping all the Google Chrome Driver servers that are available in the system') + ->expectsOutputToContain('Stopping Google Chrome Driver [PID: 9991]') + ->expectsOutputToContain('Stopping Google Chrome Driver [PID: 9992]') + ->expectsOutputToContain('Stopping Google Chrome Driver [PID: 9993]') + ->expectsOutputToContain('Stopping Google Chrome Driver [PID: 9994]') + ->assertSuccessful(); + + Process::assertRan(fn (PendingProcess $process) => Str::match('/^ps aux .*/', $process->command)); + + Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^kill -9 \d+/', $process->command), 4); +}); it('restart all the available Chrome Driver servers', function () { From e9c9784404da3fc8f46301939450c35d1f402cea Mon Sep 17 00:00:00 2001 From: asciito Date: Sun, 22 Oct 2023 18:58:48 -0600 Subject: [PATCH 12/24] refactor pid command --- app/Commands/DriverManagerCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 06ab5ac..6fb6d3b 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -36,8 +36,7 @@ class DriverManagerCommand extends Command protected array $commands = [ 'start' => './chromedriver --log-level=ALL --port={port} &', - 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL --port={port}' | awk '{print $2}'", - 'pids' => "ps aux | grep '[c]hromedriver --log-level=ALL' | awk '{print $2}'", + 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {port}' | awk '{print $2}'", 'stop' => 'kill -9 {pid}', ]; @@ -150,7 +149,8 @@ protected function status(string $port): int public function kill(): int { - $result = $this->command($this->commands['pids'])->run(); + $result = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); +; if (empty($result->output())) { warning("There' no servers to kill"); @@ -182,7 +182,7 @@ protected function command(string $cmd): PendingProcess protected function getProcessID(string $port): ?int { - $process = $this->command(Str::replace('{port}', $port, $this->commands['pid']))->run(); + $process = $this->command(Str::replace('{port}', '--port='.$port, $this->commands['pid']))->run(); return (int) trim($process->output()) ?: null; } From f62246a4f4b602c210d40a31a8fe832635d19ac2 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 00:59:14 +0000 Subject: [PATCH 13/24] chore: fix styling --- app/Commands/DriverManagerCommand.php | 4 +--- tests/Feature/ManageDriverCommandTest.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 6fb6d3b..5c5214c 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -150,7 +150,6 @@ protected function status(string $port): int public function kill(): int { $result = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); -; if (empty($result->output())) { warning("There' no servers to kill"); @@ -158,7 +157,7 @@ public function kill(): int return self::FAILURE; } - if (! $this->confirm("Are you sure you want to do this?")) { + if (! $this->confirm('Are you sure you want to do this?')) { return self::SUCCESS; } @@ -171,7 +170,6 @@ public function kill(): int $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); }); - return self::SUCCESS; } diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 4b75807..1326ee3 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -4,8 +4,8 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; - use Illuminate\Support\Str; + use function Pest\Laravel\artisan; it('start a Chrome Driver server', function () { From 1947d0bdf737100ce2e6c62ce803e8a5455c7b48 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 10:44:31 -0600 Subject: [PATCH 14/24] add missing commands and change method visibility --- app/Commands/DriverManagerCommand.php | 46 ++++++++++++++++++++++- tests/Feature/ManageDriverCommandTest.php | 28 ++++++++++---- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 5c5214c..539c367 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -38,6 +38,7 @@ class DriverManagerCommand extends Command 'start' => './chromedriver --log-level=ALL --port={port} &', 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {port}' | awk '{print $2}'", 'stop' => 'kill -9 {pid}', + 'list' => "ps aux | grep '[c]hromedriver --log-level=ALL' | awk '{print $2,$13}'", ]; public function handle(): int @@ -55,10 +56,15 @@ public function handle(): int 'stop' => $this->stop(...), 'restart' => $this->restart(...), 'status' => $this->status(...), + 'list' => $this->list(...), 'kill' => $this->kill(...), }; - return $this->getPorts()->map(fn (string $port) => $callable($port)) + if ($action === 'kill' || $action === 'list') { + return $callable(); + } + + return $this->getPorts()->map(fn (string $port) => $callable(port: $port)) // Reduce the result of every callable to a single SUCCESS or FAILURE value ->reduce(fn (int $results, int $result) => $results && $result, self::FAILURE); } @@ -147,7 +153,32 @@ protected function status(string $port): int return self::SUCCESS; } - public function kill(): int + protected function list(): int + { + info('Listing all the servers available'); + + $result = $this->command($this->commands['list'])->run(); + + if (empty($result->output())) { + warning("There' no servers available to list"); + + return self::FAILURE; + } + + $rows = collect(explode("\n", trim($result->output()))) + ->map(function (string $value) { + $values = explode(" ", trim($value)); + + // PID => PORT + return [$values[0], Str::remove('--port=', $values[1])]; + }); + + $this->table(['PID', 'PORT'], $rows); + + return self::SUCCESS; + } + + protected function kill(): int { $result = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); @@ -185,6 +216,17 @@ protected function getProcessID(string $port): ?int return (int) trim($process->output()) ?: null; } + protected function getProcessIDs(): ?Collection + { + $process = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); + + if (empty($process->output())) { + return null; + } + + return collect(explode("\n", trim($process->output()))); + } + protected function getPorts(): Collection { return collect([$this->port, ...$this->option('port')])->unique()->filter(); diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 1326ee3..3bab459 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; +use Laravel\Prompts\Prompt; use function Pest\Laravel\artisan; it('start a Chrome Driver server', function () { @@ -137,14 +138,27 @@ Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^kill -9 \d+/', $process->command), 4); }); -it('restart all the available Chrome Driver servers', function () { - -})->todo(); +it('list all the available Chrome Driver servers', function () { + $data = collect([ + // PID => PORT + '1111' => 9515, + '1112' => 9516, + '1113' => 9517, + '1114' => 9518, + '1115' => 9519, + ]); -it('get the status of all the available Chrome Driver servers', function () { + Process::fake([ + 'ps aux | grep *' => Process::result($data->map(fn ($port, $pid) => "$pid $port")->join("\n")) + ]); -})->todo(); + Prompt::fallbackWhen($this->app->runningUnitTests()); -it('list all the available Chrome Driver servers', function () { + ray($data->map(fn ($port, $pid) => [$pid, $port])->values()); -})->todo(); + artisan('manage:driver', ['action' => 'list']) + ->expectsOutputToContain('Listing all the servers available') + ->doesntExpectOutputToContain("There' no servers available to list") + ->expectsTable(['PID', 'PORT'], $data->map(fn ($port, $pid) => [$pid, $port])->values()) + ->assertSuccessful(); +}); From a3d791ed7795450d395d53c646a96c16400b7227 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 16:45:25 +0000 Subject: [PATCH 15/24] chore: fix styling --- app/Commands/DriverManagerCommand.php | 2 +- tests/Feature/ManageDriverCommandTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 539c367..0ae687d 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -167,7 +167,7 @@ protected function list(): int $rows = collect(explode("\n", trim($result->output()))) ->map(function (string $value) { - $values = explode(" ", trim($value)); + $values = explode(' ', trim($value)); // PID => PORT return [$values[0], Str::remove('--port=', $values[1])]; diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 3bab459..8502a15 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -5,8 +5,8 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; use Illuminate\Support\Str; - use Laravel\Prompts\Prompt; + use function Pest\Laravel\artisan; it('start a Chrome Driver server', function () { @@ -149,7 +149,7 @@ ]); Process::fake([ - 'ps aux | grep *' => Process::result($data->map(fn ($port, $pid) => "$pid $port")->join("\n")) + 'ps aux | grep *' => Process::result($data->map(fn ($port, $pid) => "$pid $port")->join("\n")), ]); Prompt::fallbackWhen($this->app->runningUnitTests()); From 2c9918cc5f568577bf1a5227a24b59b67a9b5e4f Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 11:10:30 -0600 Subject: [PATCH 16/24] refactor code and tests --- app/Commands/DriverManagerCommand.php | 37 ++++++++++++----------- tests/Feature/ManageDriverCommandTest.php | 12 +++----- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 0ae687d..8ba24a4 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -35,10 +35,9 @@ class DriverManagerCommand extends Command ]; protected array $commands = [ - 'start' => './chromedriver --log-level=ALL --port={port} &', - 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {port}' | awk '{print $2}'", + 'start' => './chromedriver --log-level=ALL --port {port} &', + 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {options}' | awk '{print $2,$14}'", 'stop' => 'kill -9 {pid}', - 'list' => "ps aux | grep '[c]hromedriver --log-level=ALL' | awk '{print $2,$13}'", ]; public function handle(): int @@ -157,30 +156,24 @@ protected function list(): int { info('Listing all the servers available'); - $result = $this->command($this->commands['list'])->run(); + $result = $this->getProcessIDs(); - if (empty($result->output())) { + if (empty($result)) { warning("There' no servers available to list"); return self::FAILURE; } - $rows = collect(explode("\n", trim($result->output()))) - ->map(function (string $value) { - $values = explode(' ', trim($value)); - - // PID => PORT - return [$values[0], Str::remove('--port=', $values[1])]; - }); + ray($result); - $this->table(['PID', 'PORT'], $rows); + $this->table(['PID', 'PORT'], $result); return self::SUCCESS; } protected function kill(): int { - $result = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); + $result = $this->command(Str::replace('{options}', '', $this->commands['pid']))->run(); if (empty($result->output())) { warning("There' no servers to kill"); @@ -211,20 +204,28 @@ protected function command(string $cmd): PendingProcess protected function getProcessID(string $port): ?int { - $process = $this->command(Str::replace('{port}', '--port='.$port, $this->commands['pid']))->run(); + $process = $this->command(Str::replace('{options}', '--port '.$port, $this->commands['pid']))->run(); - return (int) trim($process->output()) ?: null; + $output = explode(" ", trim($process->output())); + + return (int) $output[0] ?: null; } protected function getProcessIDs(): ?Collection { - $process = $this->command(Str::replace('{port}', '', $this->commands['pid']))->run(); + $process = $this->command(Str::replace('{options}', '', $this->commands['pid']))->run(); if (empty($process->output())) { return null; } - return collect(explode("\n", trim($process->output()))); + $raw = explode("\n", trim($process->output())); + + return collect($raw)->map(function (string $data) { + $data = explode(" ", $data); + + return ['pid' => $data[0], 'port' => $data[1]]; + }); } protected function getPorts(): Collection diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 8502a15..1bb0da4 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -17,7 +17,7 @@ ->expectsOutputToContain('Google Chrome Driver server is up and running') ->assertSuccessful(); - Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); + Process::assertRan('./chromedriver --log-level=ALL --port 9515 &'); }); it('stop a Chrome Driver server', function () { @@ -32,7 +32,7 @@ ->doesntExpectOutputToContain("There's no server to stop on port [9515]") ->assertSuccessful(); - Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port 9515' | awk '{print $2,$14}'"); Process::assertRan('kill -9 10101'); }); @@ -49,11 +49,11 @@ ->doesntExpectOutputToContain("There's no server to restart on port [9515]") ->assertSuccessful(); - Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2}'"); + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port 9515' | awk '{print $2,$14}'"); Process::assertRan('kill -9 10101'); - Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); + Process::assertRan('./chromedriver --log-level=ALL --port 9515 &'); }); test('status of Chrome Driver server', function () { @@ -115,7 +115,7 @@ artisan('manage:driver', ['action' => 'start', '-p' => [9515, 9516, 9517, 9518]]) ->assertSuccessful(); - Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port=\d+ &$/', $process->command), 4); + Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port \d+ &$/', $process->command), 4); }); it('stop all the available Chrome Driver servers', function () { @@ -154,8 +154,6 @@ Prompt::fallbackWhen($this->app->runningUnitTests()); - ray($data->map(fn ($port, $pid) => [$pid, $port])->values()); - artisan('manage:driver', ['action' => 'list']) ->expectsOutputToContain('Listing all the servers available') ->doesntExpectOutputToContain("There' no servers available to list") From 27a1f94aa32b3e83f31fe985f47fecc5868f3550 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 17:10:59 +0000 Subject: [PATCH 17/24] chore: fix styling --- app/Commands/DriverManagerCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 8ba24a4..8d9325e 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -206,7 +206,7 @@ protected function getProcessID(string $port): ?int { $process = $this->command(Str::replace('{options}', '--port '.$port, $this->commands['pid']))->run(); - $output = explode(" ", trim($process->output())); + $output = explode(' ', trim($process->output())); return (int) $output[0] ?: null; } @@ -222,7 +222,7 @@ protected function getProcessIDs(): ?Collection $raw = explode("\n", trim($process->output())); return collect($raw)->map(function (string $data) { - $data = explode(" ", $data); + $data = explode(' ', $data); return ['pid' => $data[0], 'port' => $data[1]]; }); From 758cf0d522e4ce96efa5921b6a634ecc10e9e25d Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 12:03:34 -0600 Subject: [PATCH 18/24] refactor code and update tests --- app/Commands/DriverManagerCommand.php | 38 +++++++++++++---------- tests/Feature/ManageDriverCommandTest.php | 4 ++- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 8d9325e..f776836 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -4,7 +4,9 @@ use App\OperatingSystem; use Illuminate\Console\Command; +use Illuminate\Process\FakeProcessResult; use Illuminate\Process\PendingProcess; +use Illuminate\Process\ProcessResult; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; @@ -78,7 +80,7 @@ protected function start(string $port): int intro("Stating Google Chrome Driver on port [$port]"); - $this->command(Str::replace('{port}', $port, $this->commands['start']))->run(); + $this->command('start', ['{port}' => $port]); info('Google Chrome Driver server is up and running'); @@ -97,7 +99,7 @@ public function stop(string $port): int return self::FAILURE; } - $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); + $this->command('stop', ['{pid}' => $pid]); info("Google Chrome Driver server stopped on port [$port]"); @@ -116,9 +118,9 @@ protected function restart(string $port): int return self::FAILURE; } - $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); + $this->command('stop', ['{pid}' => $pid]); - $this->command(Str::replace('{port}', $port, $this->commands['start']))->run(); + $this->command('start', ['{port}' => $port]); info("Google Chrome Driver server restarted on port [$port]"); @@ -164,8 +166,6 @@ protected function list(): int return self::FAILURE; } - ray($result); - $this->table(['PID', 'PORT'], $result); return self::SUCCESS; @@ -173,9 +173,9 @@ protected function list(): int protected function kill(): int { - $result = $this->command(Str::replace('{options}', '', $this->commands['pid']))->run(); + $pids = $this->getProcessIDs(); - if (empty($result->output())) { + if (empty($pids)) { warning("There' no servers to kill"); return self::FAILURE; @@ -187,24 +187,30 @@ protected function kill(): int info('Stopping all the Google Chrome Driver servers that are available in the system'); - collect(explode("\n", trim($result->output()))) - ->each(function (string $pid) { - info("Stopping Google Chrome Driver [PID: $pid]"); + $pids + ->each(function (array $data) { + info("Stopping Google Chrome Driver [PID: {$data['pid']}]"); - $this->command(Str::replace('{pid}', $pid, $this->commands['stop']))->run(); + $this->command('stop', ['{pid}' => $data['pid']]); }); return self::SUCCESS; } - protected function command(string $cmd): PendingProcess + protected function command(string $cmd, array $with) { - return Process::command($cmd)->path($this->getChromeDriverDirectory()); + return Process::command( + Str::replace( + collect($with)->keys(), + collect($with)->values(), + $this->commands[$cmd] + ) + )->path($this->getChromeDriverDirectory())->run(); } protected function getProcessID(string $port): ?int { - $process = $this->command(Str::replace('{options}', '--port '.$port, $this->commands['pid']))->run(); + $process = $this->command('pid', ['{options}' => '--port '.$port]); $output = explode(' ', trim($process->output())); @@ -213,7 +219,7 @@ protected function getProcessID(string $port): ?int protected function getProcessIDs(): ?Collection { - $process = $this->command(Str::replace('{options}', '', $this->commands['pid']))->run(); + $process = $this->command('pid', ['{pid}' => '']); if (empty($process->output())) { return null; diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 1bb0da4..60b4d79 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -119,8 +119,10 @@ }); it('stop all the available Chrome Driver servers', function () { + $data = ['9991 1111', '9992 1112', '9993 1113', '9994 1114']; + Process::fake([ - 'ps aux *' => Process::result(Arr::join([9991, 9992, 9993, 9994], "\n")), + 'ps aux *' => Process::result(Arr::join($data, "\n")), '*' => Process::result(), ]); From 502a4d62e8c4aced7c726a8e3b53801496b0e588 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 18:03:56 +0000 Subject: [PATCH 19/24] chore: fix styling --- app/Commands/DriverManagerCommand.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index f776836..4db642a 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -4,9 +4,6 @@ use App\OperatingSystem; use Illuminate\Console\Command; -use Illuminate\Process\FakeProcessResult; -use Illuminate\Process\PendingProcess; -use Illuminate\Process\ProcessResult; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; From e18c20465944a24f2859e0c1378730e522c5835f Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 12:09:10 -0600 Subject: [PATCH 20/24] change info for a warning --- app/Commands/DriverManagerCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 4db642a..d9f5f36 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -110,7 +110,7 @@ protected function restart(string $port): int $pid = $this->getProcessID($port); if (empty($pid)) { - info("There's no server to restart on port [$port]"); + warning("There's no server to restart on port [$port]"); return self::FAILURE; } @@ -131,7 +131,7 @@ protected function status(string $port): int $pid = $this->getProcessID($port); if (empty($pid)) { - info("There's no server available on port [$port]"); + warning("There's no server available on port [$port]"); return self::FAILURE; } From c19c9586ca9deb762b5fc7c70c057488a03b6f03 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 12:24:35 -0600 Subject: [PATCH 21/24] fix option on command --- app/Commands/DriverManagerCommand.php | 11 ++++++----- tests/Feature/ManageDriverCommandTest.php | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index d9f5f36..8f2d9bd 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -34,8 +34,8 @@ class DriverManagerCommand extends Command ]; protected array $commands = [ - 'start' => './chromedriver --log-level=ALL --port {port} &', - 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {options}' | awk '{print $2,$14}'", + 'start' => './chromedriver --log-level=ALL --port={port} &', + 'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {options}' | awk '{print $2,$13}'", 'stop' => 'kill -9 {pid}', ]; @@ -46,6 +46,7 @@ public function handle(): int 'stop' => 'Stop a server', 'restart' => 'Restart a server', 'status' => 'Status of a server', + 'list' => 'List all the server available in the system', 'kill' => 'Kill all the servers available in the system', ]); @@ -207,7 +208,7 @@ protected function command(string $cmd, array $with) protected function getProcessID(string $port): ?int { - $process = $this->command('pid', ['{options}' => '--port '.$port]); + $process = $this->command('pid', ['{options}' => '--port='.$port]); $output = explode(' ', trim($process->output())); @@ -216,7 +217,7 @@ protected function getProcessID(string $port): ?int protected function getProcessIDs(): ?Collection { - $process = $this->command('pid', ['{pid}' => '']); + $process = $this->command('pid', ['{options}' => '']); if (empty($process->output())) { return null; @@ -227,7 +228,7 @@ protected function getProcessIDs(): ?Collection return collect($raw)->map(function (string $data) { $data = explode(' ', $data); - return ['pid' => $data[0], 'port' => $data[1]]; + return ['pid' => $data[0], 'port' => Str::remove('--port=', $data[1])]; }); } diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 60b4d79..7ad3ed6 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -17,7 +17,7 @@ ->expectsOutputToContain('Google Chrome Driver server is up and running') ->assertSuccessful(); - Process::assertRan('./chromedriver --log-level=ALL --port 9515 &'); + Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); }); it('stop a Chrome Driver server', function () { @@ -32,7 +32,7 @@ ->doesntExpectOutputToContain("There's no server to stop on port [9515]") ->assertSuccessful(); - Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port 9515' | awk '{print $2,$14}'"); + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2,$13}'"); Process::assertRan('kill -9 10101'); }); @@ -49,11 +49,11 @@ ->doesntExpectOutputToContain("There's no server to restart on port [9515]") ->assertSuccessful(); - Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port 9515' | awk '{print $2,$14}'"); + Process::assertRan("ps aux | grep '[c]hromedriver --log-level=ALL --port=9515' | awk '{print $2,$13}'"); Process::assertRan('kill -9 10101'); - Process::assertRan('./chromedriver --log-level=ALL --port 9515 &'); + Process::assertRan('./chromedriver --log-level=ALL --port=9515 &'); }); test('status of Chrome Driver server', function () { @@ -115,7 +115,7 @@ artisan('manage:driver', ['action' => 'start', '-p' => [9515, 9516, 9517, 9518]]) ->assertSuccessful(); - Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port \d+ &$/', $process->command), 4); + Process::assertRanTimes(fn (PendingProcess $process) => Str::match('/^\.\/chromedriver --log-level=ALL --port=\d+ &$/', $process->command), 4); }); it('stop all the available Chrome Driver servers', function () { From 73e1baf5b83c59ac3c8aaf9b0cd9c2d0eb3a53be Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 12:28:15 -0600 Subject: [PATCH 22/24] fix default port --- app/Commands/DriverManagerCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 8f2d9bd..0e43001 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -234,7 +234,7 @@ protected function getProcessIDs(): ?Collection protected function getPorts(): Collection { - return collect([$this->port, ...$this->option('port')])->unique()->filter(); + return collect($this->option('port') ? [...$this->option('port')] : $this->port)->unique()->filter(); } protected function getChromeDriverDirectory(): string From 12a42a1c8ba853598fa46e7c539c252a12d802b6 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 12:37:02 -0600 Subject: [PATCH 23/24] add preview of server to kill action Also, and change the text for `kill` and `list` action --- app/Commands/DriverManagerCommand.php | 6 ++++-- tests/Feature/ManageDriverCommandTest.php | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Commands/DriverManagerCommand.php b/app/Commands/DriverManagerCommand.php index 0e43001..6a2d9da 100644 --- a/app/Commands/DriverManagerCommand.php +++ b/app/Commands/DriverManagerCommand.php @@ -46,8 +46,8 @@ public function handle(): int 'stop' => 'Stop a server', 'restart' => 'Restart a server', 'status' => 'Status of a server', - 'list' => 'List all the server available in the system', - 'kill' => 'Kill all the servers available in the system', + 'list' => 'List all the server', + 'kill' => 'Kill all the servers', ]); $callable = match ($action) { @@ -179,6 +179,8 @@ protected function kill(): int return self::FAILURE; } + $this->table(['PID', 'PORT'], $pids); + if (! $this->confirm('Are you sure you want to do this?')) { return self::SUCCESS; } diff --git a/tests/Feature/ManageDriverCommandTest.php b/tests/Feature/ManageDriverCommandTest.php index 7ad3ed6..28384c8 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -127,6 +127,11 @@ ]); artisan('manage:driver', ['action' => 'kill']) + ->expectsTable(['PID', 'PORT'], collect($data)->map(function (string $value) { + $values = explode(" ", $value); + + return ['pid' => $values[0], 'port' => $values[1]]; + })) ->expectsConfirmation('Are you sure you want to do this?', 'yes') ->expectsOutputToContain('Stopping all the Google Chrome Driver servers that are available in the system') ->expectsOutputToContain('Stopping Google Chrome Driver [PID: 9991]') From 34d47c8a70dfc4034b33a69249ffb8e77d3c47f6 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 23 Oct 2023 18:37:27 +0000 Subject: [PATCH 24/24] 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 28384c8..3e1db3b 100644 --- a/tests/Feature/ManageDriverCommandTest.php +++ b/tests/Feature/ManageDriverCommandTest.php @@ -128,7 +128,7 @@ artisan('manage:driver', ['action' => 'kill']) ->expectsTable(['PID', 'PORT'], collect($data)->map(function (string $value) { - $values = explode(" ", $value); + $values = explode(' ', $value); return ['pid' => $values[0], 'port' => $values[1]]; }))