Skip to content

Commit

Permalink
Separate memory limit formatting from retrieval
Browse files Browse the repository at this point in the history
This addresses vimeo#6030 (comment)

Previously PHP would allow to set memory limit to be below current
memory usage, and the test changed in this PR abused that. As we
actually don't need to test `ini_get()` value decoding was split into
its own method, and we're testing that decoding instead.
  • Loading branch information
weirdan committed Jul 1, 2021
1 parent b6a228f commit 4b31058
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/Psalm/Internal/CliUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,12 @@ public static function getPathToConfig(array $options): ?string
*/
public static function getMemoryLimitInBytes(): int
{
$limit = ini_get('memory_limit');
return self::convertMemoryLimitToBytes(ini_get('memory_limit'));
}

/** @psalm-pure */
public static function convertMemoryLimitToBytes(string $limit): int
{
// for unlimited = -1
if ($limit < 0) {
return -1;
Expand Down
32 changes: 6 additions & 26 deletions tests/CommandFunctions/GetMemoryLimitInBytesTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
<?php

namespace Psalm\Tests\CommandFunctions;

use Psalm\Internal\CliUtils;

use function ini_get;
use function ini_set;

/**
* testcase for src/command_functions.php
*/
class GetMemoryLimitInBytesTest extends \Psalm\Tests\TestCase
{
/**
* @var string
*/
private $previousLimit;

public function setUp(): void
{
$this->previousLimit = (string)ini_get('memory_limit');
parent::setUp();
}

/**
* @return array<int,array<string|int>>
*/
Expand Down Expand Up @@ -56,19 +40,15 @@ public function memoryLimitSettingProvider(): array
*
* @param int|string $setting
* @param int|string $expectedBytes
*
*/
public function testGetMemoryLimitInBytes(
$setting,
$expectedBytes
): void {
ini_set('memory_limit', (string)$setting);
$this->assertSame($expectedBytes, CliUtils::getMemoryLimitInBytes(), 'Memory limit in bytes does not fit setting');
}

public function tearDown(): void
{
ini_set('memory_limit', $this->previousLimit);
parent::tearDown();
$this->assertSame(
$expectedBytes,
CliUtils::convertMemoryLimitToBytes((string)$setting),
'Memory limit in bytes does not fit setting'
);
}
}

0 comments on commit 4b31058

Please sign in to comment.