Skip to content

Fix exit code can exceed 255 #203

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
Dec 11, 2022
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: 4 additions & 2 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
"DecrementInteger": {
"ignoreSourceCodeByRegex": [
".*\\$numberOfStreamedItems = .*",
".*\\$numberOfItems \\?\\? 0.*"
".*\\$numberOfItems \\?\\? 0.*",
".*return min\\(\\$exitCode, 255\\);.*"
]
},
"IncrementInteger": {
"ignoreSourceCodeByRegex": [
".*\\$numberOfItems \\?\\? 0.*"
".*\\$numberOfItems \\?\\? 0.*",
".*return min\\(\\$exitCode, 255\\);.*"
]
},
"MBString": false,
Expand Down
7 changes: 4 additions & 3 deletions src/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Webmozarts\Console\Parallelization\Process\ProcessLauncher;
use Webmozarts\Console\Parallelization\Process\ProcessLauncherFactory;
use function mb_strlen;
use function min;
use function sprintf;

final class ParallelExecutor
Expand Down Expand Up @@ -193,7 +194,7 @@ public function execute(
* items of the processed data set and terminates. As long as there is data
* left to process, new child processes are spawned automatically.
*
* @return 0|positive-int
* @return int<0,255>
*/
private function executeMainProcess(
ParallelizationInput $parallelizationInput,
Expand Down Expand Up @@ -298,7 +299,7 @@ private function executeChildProcess(
/**
* @param callable():void $advance
*
* @return 0|positive-int
* @return int<0,255>
*/
private function processItems(
ChunkedItemsIterator $itemIterator,
Expand All @@ -321,7 +322,7 @@ private function processItems(
($this->runAfterBatch)($input, $output, $items);
}

return $exitCode;
return min($exitCode, 255);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Process/ProcessLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface ProcessLauncher
* @param list<string>|Iterator<string> $items The items to process. None of the items must
* contain newlines
*
* @return 0|positive-int
* @return int<0,255>
*/
public function run(iterable $items): int;
}
3 changes: 2 additions & 1 deletion src/Process/SymfonyProcessLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Webmozart\Assert\Assert;
use Webmozarts\Console\Parallelization\Logger\Logger;
use function count;
use function min;
use function sprintf;
use const PHP_EOL;

Expand Down Expand Up @@ -157,7 +158,7 @@ public function run(iterable $items): int
($this->tick)();
}

return $exitCode;
return min($exitCode, 255);
}

private function startProcess(InputStream $inputStream): void
Expand Down
42 changes: 42 additions & 0 deletions tests/Process/SymfonyProcessLauncherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
namespace Webmozarts\Console\Parallelization\Process;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Webmozarts\Console\Parallelization\FakeCallable;
use Webmozarts\Console\Parallelization\Logger\DummyLogger;
use Webmozarts\Console\Parallelization\Logger\FakeLogger;
Expand All @@ -31,6 +34,8 @@
*/
final class SymfonyProcessLauncherTest extends TestCase
{
use ProphecyTrait;

public function test_it_does_nothing_if_there_is_no_items(): void
{
$launcher = new SymfonyProcessLauncher(
Expand Down Expand Up @@ -268,6 +273,43 @@ public static function inputProvider(): iterable
];
}

public function test_it_caps_the_exit_code_to_255(): void
{
$output = new BufferedOutput();

$processProphecy = $this->prophesize(Process::class);
$processProphecy->getPid()->willReturn(10);
$processProphecy->getCommandLine()->willReturn('');
$processProphecy->isRunning()->willReturn(false);
$processProphecy->getExitCode()->willReturn(500);

$processFactoryProphecy = $this->prophesize(SymfonyProcessFactory::class);
$processFactoryProphecy
->startProcess(Argument::cetera())
->willReturn($processProphecy->reveal());

$processOutput = self::createProcessOutput($output);
$numberOfTicksRecorded = 0;

$launcher = new SymfonyProcessLauncher(
[],
'',
null,
1,
1,
new DummyLogger(),
$processOutput,
static function () use (&$numberOfTicksRecorded): void {
++$numberOfTicksRecorded;
},
$processFactoryProphecy->reveal(),
);

$exitCode = $launcher->run(['item0']);

self::assertSame(255, $exitCode);
}

/**
* @return ProcessOutput
*/
Expand Down