From add8ba15fcdf7aed60a299f6fb9e337e0b9ec191 Mon Sep 17 00:00:00 2001 From: lguichard Date: Tue, 7 Jan 2025 09:42:24 +0100 Subject: [PATCH 1/2] Feat - Handle fake over ssh --- src/PendingProcess.php | 19 +++++++++++++++++ tests/ProcessSshTest.php | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/PendingProcess.php b/src/PendingProcess.php index 5a43226..acb886b 100644 --- a/src/PendingProcess.php +++ b/src/PendingProcess.php @@ -170,6 +170,25 @@ public function start(array|string|null $command = null, ?callable $output = nul return parent::start($command, $output); } + /** + * Specify the fake process result handlers for the pending process. + */ + public function withFakeHandlers(array $fakeHandlers) + { + if (! $this->handleSsh) { + $this->fakeHandlers = $fakeHandlers; + } else { + $updatedArray = []; + foreach ($fakeHandlers as $key => $value) { + $newKey = $this->buildCommand($key); + $updatedArray[$newKey] = $value; + } + $this->fakeHandlers = $updatedArray; + } + + return $this; + } + /** * Convert the command to a Symfony Process object. */ diff --git a/tests/ProcessSshTest.php b/tests/ProcessSshTest.php index ebe5e26..507fe80 100644 --- a/tests/ProcessSshTest.php +++ b/tests/ProcessSshTest.php @@ -294,3 +294,48 @@ 'whoami', ]); })->throws(InvalidArgumentException::class, 'Cannot pipe processes with SSH enabled.'); + +it('fake result Process::run', function (): void { + Process::fake([ + 'ls -al' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->run('ls -al'); + + expect($process->output())->toBe("test\n"); +}); + +it('fake result Process::start', function (): void { + Process::fake([ + 'ls -al' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->start('ls -al'); + + expect($process->wait()->output())->toBe("test\n"); +}); + +it('fake result Process::concurrently', function (): void { + Process::fake([ + '*' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->concurrently(function (Pool $pool): void { + $pool->command('ls -al'); + $pool->command('whoami'); + }); + + expect($process[0]->output())->toBe("test\n"); +}); From 1f2c0e6517b1263cd9703609906c86af43afbe59 Mon Sep 17 00:00:00 2001 From: lguichard Date: Tue, 7 Jan 2025 10:00:06 +0100 Subject: [PATCH 2/2] Wip --- src/PendingProcess.php | 20 ++++++-------------- tests/ProcessSshTest.php | 17 ----------------- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/src/PendingProcess.php b/src/PendingProcess.php index acb886b..0b7d342 100644 --- a/src/PendingProcess.php +++ b/src/PendingProcess.php @@ -3,6 +3,8 @@ namespace Bagel\ProcessSsh; use Illuminate\Process\PendingProcess as BasePendingProcess; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** * PendingProcess handles the construction and execution of SSH commands. @@ -171,22 +173,12 @@ public function start(array|string|null $command = null, ?callable $output = nul } /** - * Specify the fake process result handlers for the pending process. + * Get the fake handler for the given command, if applicable. */ - public function withFakeHandlers(array $fakeHandlers) + protected function fakeFor(string $command) { - if (! $this->handleSsh) { - $this->fakeHandlers = $fakeHandlers; - } else { - $updatedArray = []; - foreach ($fakeHandlers as $key => $value) { - $newKey = $this->buildCommand($key); - $updatedArray[$newKey] = $value; - } - $this->fakeHandlers = $updatedArray; - } - - return $this; + return (new Collection($this->fakeHandlers)) + ->first(fn ($handler, $pattern): bool => $pattern === '*' || Str::contains($command, $pattern)); } /** diff --git a/tests/ProcessSshTest.php b/tests/ProcessSshTest.php index 507fe80..7c3762a 100644 --- a/tests/ProcessSshTest.php +++ b/tests/ProcessSshTest.php @@ -322,20 +322,3 @@ expect($process->wait()->output())->toBe("test\n"); }); - -it('fake result Process::concurrently', function (): void { - Process::fake([ - '*' => Process::result( - output: 'test', - errorOutput: '', - exitCode: 1, - ), - ]); - - $process = Process::ssh($this->basicSshConfig)->concurrently(function (Pool $pool): void { - $pool->command('ls -al'); - $pool->command('whoami'); - }); - - expect($process[0]->output())->toBe("test\n"); -});