Skip to content

Commit c8d046a

Browse files
bug #911 Propagate COMPOSER_MEMORY_LIMIT to script executor (PabloKowalczyk)
This PR was merged into the 1.x branch. Discussion ---------- Propagate COMPOSER_MEMORY_LIMIT to script executor Fixes #899 Commits ------- 4a1f7a7 Propagate COMPOSER_MEMORY_LIMIT to script executor
2 parents cd634ef + 4a1f7a7 commit c8d046a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/ScriptExecutor.php

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ private function expandPhpScript(string $cmd): string
128128
$arguments[] = '--php-ini='.$ini;
129129
}
130130

131+
if ($memoryLimit = (string) getenv('COMPOSER_MEMORY_LIMIT')) {
132+
$arguments[] = "-d memory_limit={$memoryLimit}";
133+
}
134+
131135
$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));
132136

133137
return ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$cmd;

tests/ScriptExecutorTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Flex\Tests;
13+
14+
use Composer\Composer;
15+
use Composer\IO\NullIO;
16+
use Composer\Util\ProcessExecutor;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Process\PhpExecutableFinder;
19+
use Symfony\Flex\Options;
20+
use Symfony\Flex\ScriptExecutor;
21+
22+
final class ScriptExecutorTest extends TestCase
23+
{
24+
/**
25+
* @backupGlobals enabled
26+
*/
27+
public function testMemoryLimit(): void
28+
{
29+
$command = './command.php';
30+
$memoryLimit = '32M';
31+
putenv("COMPOSER_MEMORY_LIMIT={$memoryLimit}");
32+
$executorMock = $this->createMock(ProcessExecutor::class);
33+
$scriptExecutor = new ScriptExecutor(new Composer(), new NullIO(), new Options(), $executorMock);
34+
35+
$phpFinder = new PhpExecutableFinder();
36+
if (!$php = $phpFinder->find(false)) {
37+
throw new \RuntimeException('The PHP executable could not be found, add it to your PATH and try again.');
38+
}
39+
40+
$arguments = $phpFinder->findArguments();
41+
$ini = php_ini_loaded_file();
42+
$arguments[] = "--php-ini={$ini}";
43+
$arguments[] = "-d memory_limit={$memoryLimit}";
44+
45+
$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));
46+
47+
$expectedCommand = ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$command;
48+
49+
$executorMock
50+
->method('execute')
51+
->with($expectedCommand)
52+
->willReturn(0)
53+
;
54+
$this->expectNotToPerformAssertions();
55+
56+
$scriptExecutor->execute('php-script', $command);
57+
}
58+
}

0 commit comments

Comments
 (0)