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 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
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 @@ 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 {
$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 {
$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 {
if (function_exists('posix_isatty') && !@posix_isatty(STDIN)) {
$process->setInput(STDIN);
}
Expand All @@ -124,14 +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 {
if ($callback === NULL && $printOutput) {
$callback = function (string $type, iterable|string $buffer): void {
$this->output->write($buffer);
};
}
$process->start();
$process->wait($callback);
if ($process->getInput()) {
$this->runAsync($process, $callback);
}
else {
$process->run($callback);
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 @@ 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, callable $callback): void {
$process->start($callback);

// 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();
}

/**
* 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
Loading