mirror of
https://github.com/pestphp/pest.git
synced 2026-12-06 15:52:16 +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:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Parallel;
|
||||
use Pest\Plugins\Tia\ByteSafeJson;
|
||||
|
||||
it('round-trips keys and values that are not valid UTF-8', function (): void {
|
||||
$value = [
|
||||
"app/caf\xe9.php" => ["tests/caf\xe9Test.php", 'tests/PlainTest.php'],
|
||||
'plain' => "message \xff\xfe",
|
||||
'count' => 3,
|
||||
'list' => [1, 2.5, true, null],
|
||||
];
|
||||
|
||||
expect(ByteSafeJson::decode(ByteSafeJson::encode($value)))->toBe($value);
|
||||
});
|
||||
|
||||
it('keeps valid UTF-8 as plain JSON', function (): void {
|
||||
expect(ByteSafeJson::encode(['app/café.php' => ['tests/a/b.php']]))
|
||||
->toBe('{"app/caf\\u00e9.php":["tests/a/b.php"]}');
|
||||
});
|
||||
|
||||
it('does not confuse a real string with an escaped one', function (): void {
|
||||
$lookalike = "\0base64:".base64_encode("caf\xe9");
|
||||
$value = [$lookalike => $lookalike, "caf\xe9" => "caf\xe9"];
|
||||
|
||||
expect(ByteSafeJson::decode(ByteSafeJson::encode($value)))->toBe($value);
|
||||
});
|
||||
|
||||
it('leaves a malformed escape untouched', function (): void {
|
||||
expect(ByteSafeJson::decode('["\u0000base64:!!"]'))->toBe(["\0base64:!!"]);
|
||||
});
|
||||
|
||||
it('returns null for invalid JSON', function (): void {
|
||||
expect(ByteSafeJson::decode('{'))->toBeNull();
|
||||
});
|
||||
|
||||
it('throws with the reason when a value cannot be encoded', function (): void {
|
||||
ByteSafeJson::encode(['time' => INF]);
|
||||
})->throws(JsonException::class, 'Inf and NaN cannot be JSON encoded');
|
||||
|
||||
it('carries a string that is not valid UTF-8 through a parallel global', function (): void {
|
||||
Parallel::setGlobal('BYTE_SAFE_JSON_TEST', ByteSafeJson::encode("feature/caf\xe9"));
|
||||
|
||||
try {
|
||||
expect(ByteSafeJson::decode((string) Parallel::getGlobal('BYTE_SAFE_JSON_TEST')))->toBe("feature/caf\xe9");
|
||||
} finally {
|
||||
unset($_ENV['PEST_PARALLEL_GLOBAL_BYTE_SAFE_JSON_TEST']);
|
||||
}
|
||||
});
|
||||
@@ -409,3 +409,54 @@ describe('branch baseline over the fallback', function (): void {
|
||||
->and($graph->getTime('feature', 'Tests\BarTest::it passes', 'main'))->toBe(0.5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode() and decode()', function (): void {
|
||||
it('round-trips paths that are not valid UTF-8', function (): void {
|
||||
$root = sys_get_temp_dir();
|
||||
$graph = new Graph($root);
|
||||
$graph->link("tests/caf\xe9Test.php", "app/caf\xe9.php");
|
||||
$graph->setLastRunTree('master', ["caf\xe9.txt" => 'hash']);
|
||||
$graph->setResult('master', "P\\Tests\\caf\xe9Test::__pest_evaluable_it_works", 0, "caf\xe9", 0.1, 1, "tests/caf\xe9Test.php");
|
||||
|
||||
$decoded = Graph::decode($graph->encode(), $root);
|
||||
|
||||
expect($decoded)->toBeInstanceOf(Graph::class)
|
||||
->and($decoded->affected(["app/caf\xe9.php"]))->toBe(["tests/caf\xe9Test.php"])
|
||||
->and($decoded->lastRunTree('master'))->toBe(["caf\xe9.txt" => 'hash'])
|
||||
->and($decoded->getAssertions('master', "P\\Tests\\caf\xe9Test::__pest_evaluable_it_works"))->toBe(1)
|
||||
->and($decoded->testFilesToRerun('master'))->toBeEmpty();
|
||||
});
|
||||
|
||||
it('lists a branch whose name is not valid UTF-8', function (): void {
|
||||
$graph = new Graph(sys_get_temp_dir());
|
||||
$graph->setRecordedAtSha("feature/caf\xe9", 'abc');
|
||||
|
||||
expect(Graph::branchesIn($graph->encode()))->toBe(["feature/caf\xe9"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('legacy schema', function (): void {
|
||||
it('reads a graph of the previous schema without unescaping its strings', function (): void {
|
||||
$root = sys_get_temp_dir();
|
||||
$id = 'P\\Tests\\FooTest::__pest_evaluable_it_skips';
|
||||
$legacy = json_encode([
|
||||
'schema' => 1,
|
||||
'baselines' => ['master' => [
|
||||
'sha' => null,
|
||||
'tree' => [],
|
||||
'results' => [$id => ['status' => 1, 'message' => "\0base64:b2xk", 'time' => 0.1]],
|
||||
]],
|
||||
], JSON_UNESCAPED_SLASHES);
|
||||
|
||||
$graph = Graph::decode((string) $legacy, $root);
|
||||
|
||||
expect($graph)->toBeInstanceOf(Graph::class)
|
||||
->and($graph->getResult('master', $id)?->message())->toBe("\0base64:b2xk")
|
||||
->and(Graph::decode($graph->encode(), $root)?->getResult('master', $id)?->message())->toBe("\0base64:b2xk");
|
||||
});
|
||||
|
||||
it('rebuilds a graph of an unknown schema but still lists its branches', function (): void {
|
||||
expect(Graph::decode('{"schema":99}', sys_get_temp_dir()))->toBeNull()
|
||||
->and(Graph::branchesIn('{"schema":99,"baselines":{"master":{}}}'))->toBe(['master']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user