Skip to content

Propagate COMPOSER_MEMORY_LIMIT to script executor #911

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
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
4 changes: 4 additions & 0 deletions src/ScriptExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ private function expandPhpScript(string $cmd): string
$arguments[] = '--php-ini='.$ini;
}

if ($memoryLimit = (string) getenv('COMPOSER_MEMORY_LIMIT')) {
$arguments[] = "-d memory_limit={$memoryLimit}";
}

$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));

return ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$cmd;
Expand Down
58 changes: 58 additions & 0 deletions tests/ScriptExecutorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex\Tests;

use Composer\Composer;
use Composer\IO\NullIO;
use Composer\Util\ProcessExecutor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Flex\Options;
use Symfony\Flex\ScriptExecutor;

final class ScriptExecutorTest extends TestCase
{
/**
* @backupGlobals enabled
*/
public function testMemoryLimit(): void
{
$command = './command.php';
$memoryLimit = '32M';
putenv("COMPOSER_MEMORY_LIMIT={$memoryLimit}");
$executorMock = $this->createMock(ProcessExecutor::class);
$scriptExecutor = new ScriptExecutor(new Composer(), new NullIO(), new Options(), $executorMock);

$phpFinder = new PhpExecutableFinder();
if (!$php = $phpFinder->find(false)) {
throw new \RuntimeException('The PHP executable could not be found, add it to your PATH and try again.');
}

$arguments = $phpFinder->findArguments();
$ini = php_ini_loaded_file();
$arguments[] = "--php-ini={$ini}";
$arguments[] = "-d memory_limit={$memoryLimit}";

$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));

$expectedCommand = ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$command;

$executorMock
->method('execute')
->with($expectedCommand)
->willReturn(0)
;
$this->expectNotToPerformAssertions();

$scriptExecutor->execute('php-script', $command);
}
}