mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 23:32:17 +01:00
fix(tia): keep the graph writable when a path is not valid UTF-8 (#2079)
* fix(tia): keep the graph writable when a path is not valid UTF-8 * test: update the suite totals for the byte-safe JSON tests --------- Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
+45
-29
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins;
|
||||
|
||||
use JsonException;
|
||||
use NunoMaduro\Collision\Adapters\Phpunit\Printers\DefaultPrinter;
|
||||
use Pest\Contracts\Plugins\AddsOutput;
|
||||
use Pest\Contracts\Plugins\HandlesArguments;
|
||||
@@ -20,6 +21,7 @@ use Pest\Exceptions\TiaRequiresRemote;
|
||||
use Pest\Panic;
|
||||
use Pest\Plugins\Concerns\HandleArguments;
|
||||
use Pest\Plugins\Tia\BaselineSync;
|
||||
use Pest\Plugins\Tia\ByteSafeJson;
|
||||
use Pest\Plugins\Tia\ChangedFiles;
|
||||
use Pest\Plugins\Tia\CiBranch;
|
||||
use Pest\Plugins\Tia\ConfigurationFile;
|
||||
@@ -222,6 +224,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
private ?string $freshGraphReason = null;
|
||||
|
||||
private ?string $graphEncodeError = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly OutputInterface $output,
|
||||
private readonly Recorder $recorder,
|
||||
@@ -310,15 +314,26 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
private function saveGraph(Graph $graph): bool
|
||||
{
|
||||
$json = $graph->encode();
|
||||
try {
|
||||
$json = $graph->encode();
|
||||
} catch (JsonException $exception) {
|
||||
$this->graphEncodeError = $exception->getMessage();
|
||||
|
||||
if ($json === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->graphEncodeError = null;
|
||||
|
||||
return $this->state->write(self::KEY_GRAPH, $json);
|
||||
}
|
||||
|
||||
private function renderGraphWriteFailure(): void
|
||||
{
|
||||
$this->renderBadge('ERROR', $this->graphEncodeError === null
|
||||
? 'Could not write the dependency graph.'
|
||||
: sprintf('Could not write the dependency graph: %s.', $this->graphEncodeError));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $arguments
|
||||
*/
|
||||
@@ -671,7 +686,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->seedResultsInto($graph);
|
||||
|
||||
if (! $this->saveGraph($graph)) {
|
||||
$this->renderBadge('ERROR', 'Could not write the dependency graph.');
|
||||
$this->renderGraphWriteFailure();
|
||||
$recorder->reset();
|
||||
|
||||
return;
|
||||
@@ -783,7 +798,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$graph->replaceJsFileToComponents(JsModuleGraph::build($projectRoot));
|
||||
|
||||
if (! $this->saveGraph($graph)) {
|
||||
$this->renderBadge('ERROR', 'Could not write the dependency graph.');
|
||||
$this->renderGraphWriteFailure();
|
||||
|
||||
return $exitCode;
|
||||
}
|
||||
@@ -1032,7 +1047,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
return;
|
||||
}
|
||||
|
||||
$decoded = json_decode($raw, true);
|
||||
$decoded = ByteSafeJson::decode($raw);
|
||||
|
||||
if (! is_array($decoded)) {
|
||||
return;
|
||||
@@ -1408,9 +1423,9 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
*/
|
||||
private function persistAffectedSet(array $affected): bool
|
||||
{
|
||||
$json = json_encode(array_values($affected), JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if ($json === false) {
|
||||
try {
|
||||
$json = ByteSafeJson::encode(array_values($affected));
|
||||
} catch (JsonException) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1534,13 +1549,13 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
*/
|
||||
private function flushWorkerPartial(array $perTestFiles, array $perTestTables, array $perTestInertiaComponents): void
|
||||
{
|
||||
$json = json_encode([
|
||||
'files' => $perTestFiles,
|
||||
'tables' => $perTestTables,
|
||||
'inertia' => $perTestInertiaComponents,
|
||||
], JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if ($json === false) {
|
||||
try {
|
||||
$json = ByteSafeJson::encode([
|
||||
'files' => $perTestFiles,
|
||||
'tables' => $perTestTables,
|
||||
'inertia' => $perTestInertiaComponents,
|
||||
]);
|
||||
} catch (JsonException) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1614,15 +1629,15 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$results[$testId] = $this->replayedAsRecorded($testId, $result);
|
||||
}
|
||||
|
||||
$json = json_encode([
|
||||
'results' => $results,
|
||||
'replayed' => $this->replayedCount,
|
||||
'affected' => $this->affectedCount,
|
||||
'executed' => $this->executedCount,
|
||||
'truncated' => $this->stoppedEarly() || $collector->hasUnfinishedTest(),
|
||||
], JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if ($json === false) {
|
||||
try {
|
||||
$json = ByteSafeJson::encode([
|
||||
'results' => $results,
|
||||
'replayed' => $this->replayedCount,
|
||||
'affected' => $this->affectedCount,
|
||||
'executed' => $this->executedCount,
|
||||
'truncated' => $this->stoppedEarly() || $collector->hasUnfinishedTest(),
|
||||
]);
|
||||
} catch (JsonException) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1650,7 +1665,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
continue;
|
||||
}
|
||||
|
||||
$decoded = json_decode($raw, true);
|
||||
$decoded = ByteSafeJson::decode($raw);
|
||||
|
||||
if (! is_array($decoded)) {
|
||||
continue;
|
||||
@@ -1765,7 +1780,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
$data = ByteSafeJson::decode($raw);
|
||||
|
||||
if (! is_array($data)) {
|
||||
return null;
|
||||
@@ -2159,7 +2174,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
$this->fallbackBranchResolved = $resolved !== null;
|
||||
$this->fallbackBranch = $resolved ?? self::DEFAULT_BRANCH;
|
||||
|
||||
Parallel::setGlobal(self::FALLBACK_BRANCH_GLOBAL, $this->fallbackBranch);
|
||||
Parallel::setGlobal(self::FALLBACK_BRANCH_GLOBAL, ByteSafeJson::encode($this->fallbackBranch));
|
||||
|
||||
$this->branch = $changedFiles->currentBranch()
|
||||
?? CiBranch::detectCurrent()
|
||||
@@ -2177,9 +2192,10 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
private function resolveFallbackBranch(ChangedFiles $changedFiles): ?string
|
||||
{
|
||||
$inherited = Parallel::getGlobal(self::FALLBACK_BRANCH_GLOBAL);
|
||||
$inheritedBranch = is_string($inherited) ? ByteSafeJson::decode($inherited) : null;
|
||||
|
||||
if (is_string($inherited) && $inherited !== '') {
|
||||
return $inherited;
|
||||
if (is_string($inheritedBranch) && $inheritedBranch !== '') {
|
||||
return $inheritedBranch;
|
||||
}
|
||||
|
||||
return $this->watchPatterns->defaultBranch()
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ByteSafeJson
|
||||
{
|
||||
private const string ESCAPE_PREFIX = "\0base64:";
|
||||
|
||||
private const string ENCODED_ESCAPE_PREFIX = '\u0000base64:';
|
||||
|
||||
/**
|
||||
* @throws JsonException
|
||||
*/
|
||||
public static function encode(mixed $value): string
|
||||
{
|
||||
$plain = json_encode($value, JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if ($plain !== false && ! self::holdsEscapes($plain)) {
|
||||
return $plain;
|
||||
}
|
||||
|
||||
return json_encode(self::escape($value), JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
public static function decode(string $json): mixed
|
||||
{
|
||||
return self::restore(json_decode($json, true), $json);
|
||||
}
|
||||
|
||||
public static function restore(mixed $decoded, string $json): mixed
|
||||
{
|
||||
return self::holdsEscapes($json) ? self::unescape($decoded) : $decoded;
|
||||
}
|
||||
|
||||
private static function holdsEscapes(string $json): bool
|
||||
{
|
||||
return str_contains($json, self::ENCODED_ESCAPE_PREFIX);
|
||||
}
|
||||
|
||||
private static function escape(mixed $value): mixed
|
||||
{
|
||||
if (is_string($value)) {
|
||||
return self::escapeString($value);
|
||||
}
|
||||
|
||||
if (! is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$escaped = [];
|
||||
|
||||
foreach ($value as $key => $item) {
|
||||
$escaped[is_string($key) ? self::escapeString($key) : $key] = self::escape($item);
|
||||
}
|
||||
|
||||
return $escaped;
|
||||
}
|
||||
|
||||
private static function unescape(mixed $value): mixed
|
||||
{
|
||||
if (is_string($value)) {
|
||||
return self::unescapeString($value);
|
||||
}
|
||||
|
||||
if (! is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$unescaped = [];
|
||||
|
||||
foreach ($value as $key => $item) {
|
||||
$unescaped[is_string($key) ? self::unescapeString($key) : $key] = self::unescape($item);
|
||||
}
|
||||
|
||||
return $unescaped;
|
||||
}
|
||||
|
||||
private static function escapeString(string $value): string
|
||||
{
|
||||
if (preg_match('//u', $value) === 1 && ! str_starts_with($value, self::ESCAPE_PREFIX)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::ESCAPE_PREFIX.base64_encode($value);
|
||||
}
|
||||
|
||||
private static function unescapeString(string $value): string
|
||||
{
|
||||
if (! str_starts_with($value, self::ESCAPE_PREFIX)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$raw = base64_decode(substr($value, strlen(self::ESCAPE_PREFIX)), true);
|
||||
|
||||
return $raw === false ? $value : $raw;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins\Tia;
|
||||
|
||||
use JsonException;
|
||||
use Pest\Factories\TestCaseFactory;
|
||||
use Pest\Factories\TestCaseMethodFactory;
|
||||
use Pest\Support\Container;
|
||||
@@ -20,6 +21,10 @@ final class Graph
|
||||
{
|
||||
public const string WORKSPACE_BRANCH_PREFIX = '@workspace:';
|
||||
|
||||
private const int LEGACY_SCHEMA = 1;
|
||||
|
||||
private const int SCHEMA = 2;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
@@ -1698,7 +1703,7 @@ final class Graph
|
||||
*/
|
||||
public static function branchesIn(string $json): array
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
$data = ByteSafeJson::decode($json);
|
||||
|
||||
if (! is_array($data) || ! is_array($data['baselines'] ?? null)) {
|
||||
return [];
|
||||
@@ -1715,11 +1720,35 @@ final class Graph
|
||||
return $branches;
|
||||
}
|
||||
|
||||
public static function decode(string $json, string $projectRoot): ?self
|
||||
/**
|
||||
* @return array<array-key, mixed>|null
|
||||
*/
|
||||
private static function payload(string $json): ?array
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (! is_array($data) || ($data['schema'] ?? null) !== 1) {
|
||||
if (! is_array($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (($data['schema'] ?? null) === self::LEGACY_SCHEMA) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (($data['schema'] ?? null) !== self::SCHEMA) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$restored = ByteSafeJson::restore($data, $json);
|
||||
|
||||
return is_array($restored) ? $restored : null;
|
||||
}
|
||||
|
||||
public static function decode(string $json, string $projectRoot): ?self
|
||||
{
|
||||
$data = self::payload($json);
|
||||
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1922,10 +1951,13 @@ final class Graph
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function encode(): ?string
|
||||
/**
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function encode(): string
|
||||
{
|
||||
$payload = [
|
||||
'schema' => 1,
|
||||
'schema' => self::SCHEMA,
|
||||
'fingerprint' => $this->fingerprint,
|
||||
'files' => $this->files,
|
||||
'edges' => $this->edges,
|
||||
@@ -1935,9 +1967,7 @@ final class Graph
|
||||
'js_file_to_components' => $this->jsFileToComponents,
|
||||
];
|
||||
|
||||
$json = json_encode($payload, JSON_UNESCAPED_SLASHES);
|
||||
|
||||
return $json === false ? null : $json;
|
||||
return ByteSafeJson::encode($payload);
|
||||
}
|
||||
|
||||
private function relative(string $path): ?string
|
||||
|
||||
Reference in New Issue
Block a user