feat: add --memory usage flag

This commit is contained in:
Owen Voke
2021-11-03 09:59:51 +00:00
parent b74a688677
commit 266447bcc0
2 changed files with 51 additions and 0 deletions

View File

@ -79,6 +79,7 @@
},
"pest": {
"plugins": [
"Pest\\Plugins\\Memory",
"Pest\\Plugins\\Coverage",
"Pest\\Plugins\\Init",
"Pest\\Plugins\\Version",

50
src/Plugins/Memory.php Normal file
View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Memory implements AddsOutput, HandlesArguments
{
/** @var OutputInterface */
private $output;
private bool $enabled = false;
public function __construct(OutputInterface $output)
{
$this->output = $output;
}
public function handleArguments(array $arguments): array
{
foreach ($arguments as $index => $argument) {
if ($argument === '--memory') {
unset($arguments[$index]);
$this->enabled = true;
}
}
return array_values($arguments);
}
public function addOutput(int $result): int
{
if ($this->enabled) {
$this->output->writeln(sprintf(
' <fg=white;options=bold>Memory: </><fg=default>%s MB</>',
round(memory_get_usage(true) / pow(1000, 2), 3)
));
}
return $result;
}
}