8000 [Messenger][Process] add `fromShellCommandline` to `RunProcessMessage` by Staormin · Pull Request #59768 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger][Process] add fromShellCommandline to RunProcessMessage #59768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Process/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========


7.3
---

* Add `RunProcessMessage::fromShellCommandline()` to instantiate a Process via the fromShellCommandline method

7.1
---

Expand Down
17 changes: 16 additions & 1 deletion src/Symfony/Component/Process/Messenger/RunProcessMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class RunProcessMessage implements \Stringable
{
public ?string $commandLine = null;

public function __construct(
public readonly array $command,
public readonly ?string $cwd = null,
Expand All @@ -27,6 +29,19 @@ public function __construct(

public function __toString(): string
{
return implode(' ', $this->command);
return $this->commandLine ?? implode(' ', $this->command);
}

/**
* Create a process message instance that will instantiate a Process using the fromShellCommandline method.
*
* @see Process::fromShellCommandline
*/
public static function fromShellCommandline(string $command, ?string $cwd = null, ?array $env = null, mixed $input = null, ?float $timeout = 60): self
{
$message = new self([], $cwd, $env, $input, $timeout);
$message->commandLine = $command;

return $message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ final class RunProcessMessageHandler
{
public function __invoke(RunProcessMessage $message): RunProcessContext
{
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
$process = match ($message->commandLine) {
null => new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout),
default => Process::fromShellCommandline($message->commandLine, $message->cwd, $message->env, $message->input, $message->timeout),
};

try {
return new RunProcessContext($message, $process->mustRun());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,28 @@ public function testRunFailedProcess()

$this->fail('Exception not thrown');
}

public function testRunSuccessfulProcessFromShellCommandline()
{
$context = (new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('ls | grep Test', cwd: __DIR__));

$this->assertSame('ls | grep Test', $context->message->commandLine);
$this->assertSame(0, $context->exitCode);
$this->assertStringContainsString(basename(__FILE__), $context->output);
}

public function testRunFailedProcessFromShellCommandline()
{
try {
(new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('invalid'));
$this->fail('Exception not thrown');
} catch (RunProcessFailedException $e) {
$this->assertSame('invalid', $e->context->message->commandLine);
$this->assertContains(
$e->context->exitCode,
[null, '\\' === \DIRECTORY_SEPARATOR ? 1 : 127],
'Exit code should be 1 on Windows, 127 on other systems, or null',
);
}
}
}
Loading
0