Skip to content
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

CLI-1121: Fix sql imports #1581

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
47 changes: 33 additions & 14 deletions src/Helpers/LocalMachineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@
* 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 @@
* 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 @@
* @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,14 +122,18 @@
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();
$process->wait($callback);
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
danepowell marked this conversation as resolved.
Show resolved Hide resolved
}

$this->logger->notice('Command: {command} [Exit: {exit}]', [
'command' => $process->getCommandLine(),
Expand All @@ -141,6 +143,23 @@
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