Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions bin/pest
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare(strict_types=1);

use Pest\Kernel;
use Pest\Panic;
use Pest\Support\JsonOutput;
use Pest\TestCaseFilters\GitDirtyTestCaseFilter;
use Pest\TestCaseMethodFilters\AssigneeTestCaseFilter;
use Pest\TestCaseMethodFilters\IssueTestCaseFilter;
Expand All @@ -24,9 +25,15 @@ use Symfony\Component\Console\Output\ConsoleOutput;
$dirty = false;
$todo = false;
$notes = false;
$json = false;

foreach ($arguments as $key => $value) {

if ($value === '--json') {
$json = true;
unset($arguments[$key]);
}

if ($value === '--compact') {
$_SERVER['COLLISION_PRINTER_COMPACT'] = 'true';
unset($arguments[$key]);
Expand Down Expand Up @@ -125,6 +132,28 @@ use Symfony\Component\Console\Output\ConsoleOutput;
// Used when Pest maintainers are running Pest tests.
$localPath = dirname(__DIR__).'/vendor/autoload.php';

// Auto-detect AI agent environments before autoloader runs,
// so Collision's printer subscriber is not registered.
if (! $json) {
$vendorDir = dirname(file_exists($vendorPath) ? $vendorPath : $localPath);
$agentDetectorPath = $vendorDir.'/shipfastlabs/agent-detector/src/AgentDetector.php';

if (is_file($agentDetectorPath)) {
require_once $vendorDir.'/shipfastlabs/agent-detector/src/KnownAgent.php';
require_once $vendorDir.'/shipfastlabs/agent-detector/src/AgentResult.php';
require_once $agentDetectorPath;

if (\AgentDetector\AgentDetector::detect()->isAgent) {
$json = true;
}
}
}

if ($json) {
$_SERVER['PEST_JSON_OUTPUT'] = 'true';
unset($_SERVER['COLLISION_PRINTER']);
}

if (file_exists($vendorPath)) {
include_once $vendorPath;
$autoloadPath = $vendorPath;
Expand Down Expand Up @@ -174,9 +203,12 @@ use Symfony\Component\Console\Output\ConsoleOutput;
$testSuite->tests->addTestCaseMethodFilter(new PrTestCaseFilter((int) $pr));
}

$isDecorated = $input->getParameterOption('--colors', 'always') !== 'never';

$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
if ($json) {
$output = new JsonOutput(new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, false));
} else {
$isDecorated = $input->getParameterOption('--colors', 'always') !== 'never';
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
}

try {
$kernel = Kernel::boot($testSuite, $input, $output);
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"pestphp/pest-plugin-mutate": "^4.0.1",
"pestphp/pest-plugin-profanity": "^4.2.1",
"phpunit/phpunit": "^12.5.8",
"shipfastlabs/agent-detector": "^1.0",
"symfony/process": "^7.4.4|^8.0.0"
},
"conflict": {
Expand Down Expand Up @@ -103,10 +104,12 @@
"Pest\\Plugins\\Bail",
"Pest\\Plugins\\Cache",
"Pest\\Plugins\\Coverage",
"Pest\\Plugins\\Memory",
"Pest\\Plugins\\Shard",
"Pest\\Plugins\\Json",
"Pest\\Plugins\\Init",
"Pest\\Plugins\\Environment",
"Pest\\Plugins\\Help",
"Pest\\Plugins\\Memory",
"Pest\\Plugins\\Only",
"Pest\\Plugins\\Printer",
"Pest\\Plugins\\ProcessIsolation",
Expand All @@ -115,7 +118,6 @@
"Pest\\Plugins\\Snapshot",
"Pest\\Plugins\\Verbose",
"Pest\\Plugins\\Version",
"Pest\\Plugins\\Shard",
"Pest\\Plugins\\Parallel"
]
},
Expand Down
132 changes: 132 additions & 0 deletions src/Plugins/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace Pest\Plugins;

use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\Container;
use Pest\Support\JsonOutput;
use Pest\TestSuite;
use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Code\Throwable;
use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
use PHPUnit\Event\Test\Errored;
use PHPUnit\TestRunner\TestResult\Facade;
use PHPUnit\TestRunner\TestResult\TestResult;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
*/
final class Json implements AddsOutput, HandlesArguments
{
use Concerns\HandleArguments;

/**
* Creates a new Plugin instance.
*/
public function __construct(private readonly OutputInterface $output)
{
//
}

/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
if (! JsonOutput::isActive() || in_array('--no-output', $arguments, true)) {
return $arguments;
}

return $this->pushArgument('--no-output', $arguments);
}

/**
* {@inheritDoc}
*/
public function addOutput(int $exitCode): int
{
if (Parallel::isWorker()) {
return $exitCode;
}

if (! $this->output instanceof JsonOutput) {
return $exitCode;
}

$testSuite = Container::getInstance()->get(TestSuite::class);
assert($testSuite instanceof TestSuite);

$result = Facade::result();
$failures = $this->buildFailures($result, $testSuite->rootPath);

if ($failures === []) {
$this->output->writeJson(json_encode(['status' => 'pass'], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
} else {
$this->output->writeJson(json_encode([
'status' => 'fail',
'failures' => $failures,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
}

return $exitCode;
}

/**
* Builds the failures array from PHPUnit's TestResult.
*
* @return list<array{test: string, message: string, location: string, trace: string}>
*/
private function buildFailures(TestResult $result, string $rootPath): array
{
$failures = [];

foreach ($result->testErroredEvents() as $event) {
if ($event instanceof Errored) {
$failures[] = $this->buildFailureFromEvent($event->test(), $event->throwable(), $rootPath);
} elseif ($event instanceof BeforeFirstTestMethodErrored) {
$failures[] = [
'test' => $event->testClassName(),
'message' => $event->throwable()->message(),
'location' => $event->testClassName(),
'trace' => $event->throwable()->stackTrace(),
];
}
}

foreach ($result->testFailedEvents() as $event) {
$failures[] = $this->buildFailureFromEvent($event->test(), $event->throwable(), $rootPath);
}

return $failures;
}

/**
* Builds a failure array from a test event.
*
* @return array{test: string, message: string, location: string, trace: string}
*/
private function buildFailureFromEvent(Test $test, Throwable $throwable, string $rootPath): array
{
$testName = 'Unknown test';
$location = 'unknown';

if ($test instanceof TestMethod) {
$testName = $test->testDox()->prettifiedMethodName();
$file = str_replace($rootPath.DIRECTORY_SEPARATOR, '', $test->file());
$line = $test->line();
$location = "{$file}:{$line}";
}

return [
'test' => $testName,
'message' => $throwable->message(),
'location' => $location,
'trace' => str_replace($rootPath.DIRECTORY_SEPARATOR, '', $throwable->stackTrace()),
];
}
}
Loading