1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/tests/EndToEnd/PsalmRunnerTrait.php

59 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\EndToEnd;
use Symfony\Component\Process\Process;
use function array_merge;
2023-07-01 16:47:42 +02:00
use function array_unshift;
use function in_array;
trait PsalmRunnerTrait
{
2022-12-16 19:58:47 +01:00
private string $psalm = __DIR__ . '/../../psalm';
2022-12-16 19:58:47 +01:00
private string $psalter = __DIR__ . '/../../psalter';
/**
* @param list<string> $args
* @return array{STDOUT: string, STDERR: string, CODE: int|null}
*/
private function runPsalm(
array $args,
string $workingDir,
bool $shouldFail = false,
2023-07-24 10:48:32 +02:00
bool $relyOnConfigDir = true,
): array {
2023-07-01 13:28:05 +02:00
// Ensure CI agnostic output
2023-07-01 13:40:04 +02:00
if (!in_array('--init', $args, true) && !in_array('--alter', $args, true)) {
2023-07-01 13:36:42 +02:00
array_unshift($args, '--output-format=console');
}
2023-07-01 13:28:05 +02:00
// As config files all contain `resolveFromConfigFile="true"` Psalm
// shouldn't need to be run from the same directory that the code being
// analysed exists in.
// Windows doesn't read shabangs, so to allow this to work on windows
// we run `php psalm` rather than just `psalm`.
if ($relyOnConfigDir) {
$process = new Process(array_merge(['php', $this->psalm, '-c=' . $workingDir . '/psalm.xml'], $args), null);
} else {
$process = new Process(array_merge(['php', $this->psalm], $args), $workingDir);
}
if (!$shouldFail) {
$process->mustRun();
} else {
$process->run();
$this->assertEquals(2, $process->getExitCode(), 'Expected Psalm to report errors');
}
return [
'STDOUT' => $process->getOutput(),
'STDERR' => $process->getErrorOutput(),
'CODE' => $process->getExitCode(),
];
}
}