Skip to content

Commit 202fa11

Browse files
Propagate COMPOSER_MEMORY_LIMIT to script executor
1 parent dcbe58c commit 202fa11

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/ScriptExecutor.php

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

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

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

tests/ScriptExecutorTest.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Flex\Tests;
6+
7+
use Composer\Composer;
8+
use Composer\IO\NullIO;
9+
use Composer\Util\ProcessExecutor;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Process\PhpExecutableFinder;
12+
use Symfony\Flex\Options;
13+
use Symfony\Flex\ScriptExecutor;
14+
15+
final class ScriptExecutorTest extends TestCase
16+
{
17+
/** @backupGlobals enabled */
18+
public function testMemoryLimit(): void
19+
{
20+
// Arrange
21+
$command = './command.php';
22+
$memoryLimit = '32M';
23+
putenv("COMPOSER_MEMORY_LIMIT={$memoryLimit}");
24+
$executorMock = $this->createMock(ProcessExecutor::class);
25+
$scriptExecutor = new ScriptExecutor(
26+
new Composer(),
27+
new NullIO(),
28+
new Options(),
29+
$executorMock,
30+
);
31+
$expectedCommand = $this->buildExpectedCommand($command, $memoryLimit);
32+
33+
// Expect
34+
$executorMock
35+
->method('execute')
36+
->with($expectedCommand)
37+
;
38+
$this->expectNotToPerformAssertions();
39+
40+
// Act
41+
$scriptExecutor->execute(
42+
'php-script',
43+
$command
44+
);
45+
}
46+
47+
private function buildExpectedCommand(string $command, ?string $memoryLimit = null): string
48+
{
49+
$phpFinder = new PhpExecutableFinder();
50+
if (!$php = $phpFinder->find(false)) {
51+
throw new \RuntimeException('The PHP executable could not be found, add it to your PATH and try again.');
52+
}
53+
54+
$arguments = $phpFinder->findArguments();
55+
$ini = php_ini_loaded_file();
56+
$arguments[] = "--php-ini={$ini}";
57+
58+
if (null !== $memoryLimit) {
59+
$arguments[] = "-d memory_limit={$memoryLimit}";
60+
}
61+
62+
$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));
63+
64+
return ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$command;
65+
}
66+
}

0 commit comments

Comments
 (0)