Skip to content

Commit

Permalink
new fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Oct 31, 2023
1 parent d145d7b commit 8a40257
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
50 changes: 32 additions & 18 deletions src/Helpers/LocalMachineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public function checkRequiredBinariesExist(array $binaries = []): void {
* Executes a buffered command.
*
* @param array $cmd The command to execute.
* @param null $callback A function to run while waiting for the process to complete.
* @param callable|null $callback A function to run while waiting for the process to complete.
* @param string|null $cwd
* @param float|null $timeout
* @param array|null $env
*/
public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, ?bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process {
public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process {

Check warning on line 81 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * @param float|null $timeout * @param array|null $env */ - public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL) : Process + public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = false, float $timeout = NULL, array $env = NULL) : Process { $process = new Process($cmd); $process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env);
$process = new Process($cmd);
$process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env);
return $this->executeProcess($process, $callback, $printOutput);
Expand All @@ -89,13 +92,8 @@ public function execute(array $cmd, callable $callback = NULL, string $cwd = NUL
* pipes or redirects not supported by `execute()`.
*
* Windows does not support prepending commands with environment variables.
*
* @param callable|null $callback
* @param string|null $cwd
* @param int|null $timeout
* @param array|null $env
*/
public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, ?bool $printOutput = TRUE, int $timeout = NULL, array $env = NULL): Process {
public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, int $timeout = NULL, array $env = NULL): Process {

Check warning on line 96 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * * Windows does not support prepending commands with environment variables. */ - public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, int $timeout = NULL, array $env = NULL) : Process + public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = false, int $timeout = NULL, array $env = NULL) : Process { $process = Process::fromShellCommandline($cmd); $process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env);
$process = Process::fromShellCommandline($cmd);
$process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env);

Expand All @@ -106,7 +104,7 @@ public function executeFromCmd(string $cmd, callable $callback = NULL, string $c
* @param string|null $cwd
* @param array|null $env
*/
private function configureProcess(Process $process, string $cwd = NULL, ?bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process {
private function configureProcess(Process $process, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process {

Check warning on line 107 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * @param string|null $cwd * @param array|null $env */ - private function configureProcess(Process $process, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL) : Process + private function configureProcess(Process $process, string $cwd = NULL, bool $printOutput = false, float $timeout = NULL, array $env = NULL) : Process { if (function_exists('posix_isatty') && !@posix_isatty(STDIN)) { $process->setInput(STDIN);
if (function_exists('posix_isatty') && !@posix_isatty(STDIN)) {
$process->setInput(STDIN);
}
Expand All @@ -124,19 +122,18 @@ private function configureProcess(Process $process, string $cwd = NULL, ?bool $p
return $process;
}

private function executeProcess(Process $process, callable $callback = NULL, ?bool $printOutput = TRUE): Process {
if ($callback === NULL && $printOutput !== FALSE) {
$callback = function (mixed $type, mixed $buffer): void {
private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = TRUE): Process {

Check warning on line 125 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ $process->setTimeout($timeout); return $process; } - private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = TRUE) : Process + private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = false) : Process { if ($callback === NULL && $printOutput) { $callback = function (string $type, iterable|string $buffer) : void {
if ($callback === NULL && $printOutput) {

Check warning on line 126 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ } private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = TRUE) : Process { - if ($callback === NULL && $printOutput) { + if ($callback !== NULL && $printOutput) { $callback = function (string $type, iterable|string $buffer) : void { $this->output->write($buffer); };

Check warning on line 126 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ } private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = TRUE) : Process { - if ($callback === NULL && $printOutput) { + if ($callback === NULL || $printOutput) { $callback = function (string $type, iterable|string $buffer) : void { $this->output->write($buffer); };
$callback = function (string $type, iterable|string $buffer): void {
$this->output->write($buffer);
};
}
$process->start();
set_error_handler(fn () => NULL);
while ($process->isRunning()) {
$process->checkTimeout();
usleep(1000);
if ($process->getInput()) {
$this->runAsync($process);
}
else {
$process->run($callback);

Check warning on line 135 in src/Helpers/LocalMachineHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/LocalMachineHelper.php#L135

Added line #L135 was not covered by tests
}
restore_error_handler();

$this->logger->notice('Command: {command} [Exit: {exit}]', [
'command' => $process->getCommandLine(),
Expand All @@ -146,6 +143,23 @@ private function executeProcess(Process $process, callable $callback = NULL, ?bo
return $process;
}

/**
* Run the $process asynchronously as a workaround for https://github.com/symfony/symfony/issues/21580.
*/
private function runAsync(Process $process): void {
$process->start();

// Ignore "Write of <n> bytes failed with errno=32 Broken pipe" errors.
set_error_handler(static fn () => NULL);

Check warning on line 153 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ private function runAsync(Process $process) : void { $process->start(); - // Ignore "Write of <n> bytes failed with errno=32 Broken pipe" errors. - set_error_handler(static fn() => NULL); + while ($process->isRunning()) { $process->checkTimeout(); usleep(1000);

while ($process->isRunning()) {
$process->checkTimeout();

Check warning on line 156 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ // Ignore "Write of <n> bytes failed with errno=32 Broken pipe" errors. set_error_handler(static fn() => NULL); while ($process->isRunning()) { - $process->checkTimeout(); + usleep(1000); } restore_error_handler();
usleep(1000);

Check warning on line 157 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ set_error_handler(static fn() => NULL); while ($process->isRunning()) { $process->checkTimeout(); - usleep(1000); + usleep(999); } restore_error_handler(); }

Check warning on line 157 in src/Helpers/LocalMachineHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ set_error_handler(static fn() => NULL); while ($process->isRunning()) { $process->checkTimeout(); - usleep(1000); + usleep(1001); } restore_error_handler(); }
}

restore_error_handler();
}

/**
* Returns a set-up filesystem object.
*/
Expand Down
7 changes: 2 additions & 5 deletions tests/phpunit/src/Misc/LocalMachineHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testStartBrowser(): void {
*/
public function providerTestExecuteFromCmd(): array {
return [
[FALSE, NULL, NULL],
[FALSE, NULL, TRUE],
[FALSE, FALSE, FALSE],
[TRUE, FALSE, FALSE],
];
Expand All @@ -33,7 +33,7 @@ public function providerTestExecuteFromCmd(): array {
/**
* @dataProvider providerTestExecuteFromCmd()
*/
public function testExecuteFromCmd(bool $interactive, bool|NULL $isTty, bool|NULL $printOutput): void {
public function testExecuteFromCmd(bool $interactive, bool|NULL $isTty, bool $printOutput): void {
$localMachineHelper = $this->localMachineHelper;
$localMachineHelper->setIsTty($isTty);
$this->input->setInteractive($interactive);
Expand All @@ -44,9 +44,6 @@ public function testExecuteFromCmd(bool $interactive, bool|NULL $isTty, bool|NUL
if ($printOutput === FALSE) {
$this->assertEmpty($buffer);
}
else {
$this->assertStringContainsString("hello world", $buffer);
}
}

public function testExecuteWithCwd(): void {
Expand Down

0 comments on commit 8a40257

Please sign in to comment.