Skip to content

Commit afc5433

Browse files
[10.x] Minor fixes on the process module (#46033)
* Applies minor fixes on process module * Removes non-used class * Improves testing * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 33387c7 commit afc5433

File tree

9 files changed

+71
-15
lines changed

9 files changed

+71
-15
lines changed

src/Illuminate/Console/Process/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function isRecording()
123123
/**
124124
* Record the given process if processes should be recorded.
125125
*
126-
* @param \Illuminate\Console\Process\PendignProcess $process
126+
* @param \Illuminate\Console\Process\PendingProcess $process
127127
* @param \Illuminate\Contracts\Console\Process\ProcessResult $result
128128
* @return $this
129129
*/
@@ -139,7 +139,7 @@ public function recordIfRecording(PendingProcess $process, ProcessResultContract
139139
/**
140140
* Record the given process.
141141
*
142-
* @param \Illuminate\Console\Process\PendignProcess $process
142+
* @param \Illuminate\Console\Process\PendingProcess $process
143143
* @param \Illuminate\Contracts\Console\Process\ProcessResult $result
144144
* @return $this
145145
*/

src/Illuminate/Console/Process/FakeInvokedProcess.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class FakeInvokedProcess implements InvokedProcessContract
3131
/**
3232
* The number of times the process should indicate that it is "running".
3333
*
34-
* @var int
34+
* @var int|null
3535
*/
3636
protected $remainingRunIterations;
3737

3838
/**
3939
* The general output handler callback.
4040
*
41-
* @var null
41+
* @var callable|null
4242
*/
4343
protected $outputHandler;
4444

@@ -135,7 +135,6 @@ public function running()
135135
/**
136136
* Invoke the asynchronous output handler with the next single line of output if necessary.
137137
*
138-
* @param callable $until
139138
* @return array|false
140139
*/
141140
protected function invokeOutputHandlerWithNextLineOfOutput()
@@ -258,7 +257,7 @@ public function latestErrorOutput()
258257
* Wait for the process to finish.
259258
*
260259
* @param callable|null $output
261-
* @return \Illuminate\Console\Process\ProcessResult
260+
* @return \Illuminate\Contracts\Console\Process\ProcessResult
262261
*/
263262
public function wait(callable $output = null)
264263
{

src/Illuminate/Console/Process/FakeProcessResult.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Console\Process\Exceptions\ProcessFailedException;
66
use Illuminate\Contracts\Console\Process\ProcessResult as ProcessResultContract;
7-
use Symfony\Component\Process\Process;
87

98
class FakeProcessResult implements ProcessResultContract
109
{

src/Illuminate/Console/Process/PendingProcess.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Console\Process;
44

55
use Closure;
6+
use Illuminate\Console\Process\Exceptions\ProcessTimedOutException;
67
use Illuminate\Support\Str;
78
use LogicException;
89
use RuntimeException;
@@ -28,7 +29,7 @@ class PendingProcess
2829
/**
2930
* The working directory of the process.
3031
*
31-
* @var string
32+
* @var string|null
3233
*/
3334
public $path;
3435

@@ -56,7 +57,7 @@ class PendingProcess
5657
/**
5758
* Indicates whether output should be disabled for the process.
5859
*
59-
* @var array
60+
* @var bool
6061
*/
6162
public $quietly = false;
6263

@@ -160,6 +161,7 @@ public function forever()
160161
* Set the additional environent variables for the process.
161162
*
162163
* @param array $environment
164+
* @return $this
163165
*/
164166
public function env(array $environment)
165167
{
@@ -225,7 +227,7 @@ public function run(array|string $command = null, callable $output = null)
225227
$this->factory->recordIfRecording($this, $result);
226228
});
227229
} elseif ($this->factory->isRecording() && $this->factory->preventingStrayProcesses()) {
228-
throw new RuntimeException('Attempted process ['.(string) $this->command.'] without a matching fake.');
230+
throw new RuntimeException('Attempted process ['.$command.'] without a matching fake.');
229231
}
230232

231233
return new ProcessResult(tap($process)->run($output));
@@ -252,7 +254,7 @@ public function start(array|string $command = null, callable $output = null)
252254
$this->factory->recordIfRecording($this, $process->predictProcessResult());
253255
});
254256
} elseif ($this->factory->isRecording() && $this->factory->preventingStrayProcesses()) {
255-
throw new RuntimeException('Attempted process ['.(string) $this->command.'] without a matching fake.');
257+
throw new RuntimeException('Attempted process ['.$command.'] without a matching fake.');
256258
}
257259

258260
return new InvokedProcess(tap($process)->start($output));

src/Illuminate/Console/Process/Pool.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use InvalidArgumentException;
66

7+
/**
8+
* @mixin \Illuminate\Console\Process\Factory
9+
* @mixin \Illuminate\Console\Process\PendingProcess
10+
*/
711
class Pool
812
{
913
/**

src/Illuminate/Console/Process/ProcessResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function failed()
5959
/**
6060
* Get the exit code of the process.
6161
*
62-
* @return int
62+
* @return int|null
6363
*/
6464
public function exitCode()
6565
{

src/Illuminate/Contracts/Console/Process/ProcessResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function failed();
2828
/**
2929
* Get the exit code of the process.
3030
*
31-
* @return int
31+
* @return int|null
3232
*/
3333
public function exitCode();
3434

src/Illuminate/Support/Facades/Process.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
use Illuminate\Console\Process\Factory;
77

88
/**
9+
* @method static \Illuminate\Console\Process\PendingProcess command(array|string $command)
10+
* @method static \Illuminate\Console\Process\PendingProcess path(string $path)
11+
* @method static \Illuminate\Console\Process\PendingProcess timeout(int $timeout)
12+
* @method static \Illuminate\Console\Process\PendingProcess idleTimeout(int $timeout)
13+
* @method static \Illuminate\Console\Process\PendingProcess forever()
14+
* @method static \Illuminate\Console\Process\PendingProcess env(array $environment)
15+
* @method static \Illuminate\Console\Process\PendingProcess quietly()
16+
* @method static \Illuminate\Console\Process\PendingProcess tty(bool $tty = true)
17+
* @method static \Illuminate\Console\Process\PendingProcess options(array $options)
18+
* @method static \Illuminate\Contracts\Console\Process\ProcessResult run(array|string|null $command = null, callable|null $output = null)
19+
* @method static \Illuminate\Console\Process\InvokedProcess start(array|string|null $command = null, callable $output = null)
20+
* @method static \Illuminate\Console\Process\PendingProcess withFakeHandlers(array $fakeHandlers)
921
* @method static \Illuminate\Console\Process\FakeProcessResult result(array|string $output = '', array|string $errorOutput = '', int $exitCode = 0)
1022
* @method static \Illuminate\Console\Process\FakeProcessDescription describe()
1123
* @method static \Illuminate\Console\Process\FakeProcessSequence sequence(array $processes = [])
@@ -28,6 +40,7 @@
2840
* @method static void flushMacros()
2941
* @method static mixed macroCall(string $method, array $parameters)
3042
*
43+
* @see \Illuminate\Console\Process\PendingProcess
3144
* @see \Illuminate\Console\Process\Factory
3245
*/
3346
class Process extends Facade

tests/Console/ProcessTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests;
44

55
use Illuminate\Console\Process\Exceptions\ProcessFailedException;
6+
use Illuminate\Console\Process\Exceptions\ProcessTimedOutException;
67
use Illuminate\Console\Process\Factory;
78
use Illuminate\Contracts\Console\Process\ProcessResult;
89
use Mockery as m;
@@ -311,10 +312,12 @@ public function testProcessFakeSequencesCanThrowWhenSequenceIsEmpty()
311312
$result = $factory->run('ls -la');
312313
}
313314

314-
public function testStrayProcessesCanBePrevented()
315+
public function testStrayProcessesCanBePreventedWithStringComand()
315316
{
316317
$this->expectException(RuntimeException::class);
317-
$this->expectExceptionMessage('Attempted process');
318+
$this->expectExceptionMessage('Attempted process [');
319+
$this->expectExceptionMessage('cat composer.json');
320+
$this->expectExceptionMessage('] without a matching fake.');
318321

319322
$factory = new Factory;
320323

@@ -327,6 +330,24 @@ public function testStrayProcessesCanBePrevented()
327330
$result = $factory->run('cat composer.json');
328331
}
329332

333+
public function testStrayProcessesCanBePreventedWithArrayCommand()
334+
{
335+
$this->expectException(RuntimeException::class);
336+
$this->expectExceptionMessage('Attempted process [');
337+
$this->expectExceptionMessage('cat composer.json');
338+
$this->expectExceptionMessage('] without a matching fake.');
339+
340+
$factory = new Factory;
341+
342+
$factory->preventStrayProcesses();
343+
344+
$factory->fake([
345+
'ls *' => 'ls command',
346+
]);
347+
348+
$result = $factory->run(['cat composer.json']);
349+
}
350+
330351
public function testStrayProcessesActuallyRunByDefault()
331352
{
332353
$factory = new Factory;
@@ -396,13 +417,31 @@ public function testRealProcessesCanThrow()
396417
}
397418

398419
$this->expectException(ProcessFailedException::class);
420+
$this->expectExceptionMessage('The process "echo "Hello World" >&2; exit 1;" failed.');
399421

400422
$factory = new Factory;
401423
$result = $factory->path(__DIR__)->run('echo "Hello World" >&2; exit 1;');
402424

403425
$result->throw();
404426
}
405427

428+
public function testRealProcessesCanTimeout()
429+
{
430+
if (windows_os()) {
431+
$this->markTestSkipped('Requires Linux.');
432+
}
433+
434+
$this->expectException(ProcessTimedOutException::class);
435+
$this->expectExceptionMessage(
436+
'The process "sleep 2; exit 1;" exceeded the timeout of 1 seconds.'
437+
);
438+
439+
$factory = new Factory;
440+
$result = $factory->timeout(1)->path(__DIR__)->run('sleep 2; exit 1;');
441+
442+
$result->throw();
443+
}
444+
406445
public function testRealProcessesCanThrowIfTrue()
407446
{
408447
if (windows_os()) {

0 commit comments

Comments
 (0)