8000 [Messenger][Process] add `fromShellCommandline` to `RunProcessMessage` · symfony/symfony@4afeaee · GitHub
[go: up one dir, main page]

Skip to content

Commit 4afeaee

Browse files
committed
[Messenger][Process] add fromShellCommandline to RunProcessMessage
Allows using the Process::fromShellCommandline when using a RunProcessMessage
1 parent d824d53 commit 4afeaee

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5+
7.3
6+
---
7+
8+
* Add `RunProcessMessage::fromShellCommandline()` to instantiate a Process via the fromShellCommandline method
9+
410
7.1
511
---
612

src/Symfony/Component/Process/Messenger/RunProcessMessage.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class RunProcessMessage implements \Stringable
1818
{
19+
public ?string $commandLine = null;
20+
1921
public function __construct(
2022
public readonly array $command,
2123
public readonly ?string $cwd = null,
@@ -27,6 +29,19 @@ public function __construct(
2729

2830
public function __toString(): string
2931
{
30-
return implode(' ', $this->command);
32+
return $this->commandLine ?? implode(' ', $this->command);
33+
}
34+
35+
/**
36+
* Create a process message instance that will instantiate a Process using the fromShellCommandline method.
37+
*
38+
* @see Process::fromShellCommandline
39+
*/
40+
public static function fromShellCommandline(string $command, ?string $cwd = null, ?array $env = null, mixed $input = null, ?float $timeout = 60): self
41+
{
42+
$message = new self([], $cwd, $env, $input, $timeout);
43+
$message->commandLine = $command;
44+
45+
return $message;
3146
}
3247
}

src/Symfony/Component/Process/Messenger/RunProcessMessageHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ final class RunProcessMessageHandler
2222
{
2323
public function __invoke(RunProcessMessage $message): RunProcessContext
2424
{
25-
$process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout);
25+
match ($message->commandLine) {
26+
null => $process = new Process($message->command, $message->cwd, $message->env, $message->input, $message->timeout),
27+
default => $process = Process::fromShellCommandline($message->commandLine, $message->cwd, $message->env, $message->input, $message->timeout),
28+
};
2629

2730
try {
2831
return new RunProcessContext($message, $process->mustRun());

src/Symfony/Component/Process/Tests/Messenger/RunProcessMessageHandlerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,31 @@ public function testRunFailedProcess()
4444

4545
$this->fail('Exception not thrown');
4646
}
47+
48+
public function testRunSuccessfulProcessFromShellCommandline()
49+
{
50+
$context = (new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('ls | grep Test', cwd: __DIR__));
51+
52+
$this->assertSame('ls | grep Test', $context->message->commandLine);
53+
$this->assertSame(0, $context->exitCode);
54+
$this->assertStringContainsString(basename(__FILE__), $context->output);
55+
}
56+
57+
public function testRunFailedProcessFromShellCommandline()
58+
{
59+
try {
60+
(new RunProcessMessageHandler())(RunProcessMessage::fromShellCommandline('invalid'));
61+
} catch (RunProcessFailedException $e) {
62+
$this->assertSame('invalid', $e->context->message->commandLine);
63+
$this->assertContains(
64+
$e->context->exitCode,
65+
[null, '\\' === \DIRECTORY_SEPARATOR ? 1 : 127],
66+
'Exit code should be 1 on Windows, 127 on other systems, or null',
67+
);
68+
69+
return;
70+
}
71+
72+
$this->fail('Exception not thrown');
73+
}
4774
}

0 commit comments

Comments
 (0)
0