mirror of
https://github.com/pestphp/pest.git
synced 2026-06-05 10:52:14 +02:00
wip
This commit is contained in:
@ -73,7 +73,13 @@ final readonly class Fingerprint
|
||||
// watch pattern + `Recorder::linkAncestorFiles` reflection
|
||||
// walk, which gives precise per-test invalidation rather
|
||||
// than a wholesale rebuild that trashes the entire graph.
|
||||
private const int SCHEMA_VERSION = 11;
|
||||
// v12: PHP/JS structural inputs (pest_factory*, vite.config.*)
|
||||
// now hash via `ContentHash::of()` so cosmetic comment +
|
||||
// whitespace edits don't fire rebuilds. composer.json and
|
||||
// composer.lock hash a behavioural subset — description,
|
||||
// keywords, scripts, authors, install timestamps, dist
|
||||
// URLs etc. no longer drift the structural fingerprint.
|
||||
private const int SCHEMA_VERSION = 12;
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
@ -86,28 +92,35 @@ final readonly class Fingerprint
|
||||
return [
|
||||
'structural' => [
|
||||
'schema' => self::SCHEMA_VERSION,
|
||||
'composer_lock' => self::hashIfExists($projectRoot.'/composer.lock'),
|
||||
// `composer.lock` hashed against a *behavioural*
|
||||
// subset (per-package version + reference + autoload +
|
||||
// extra). Skips per-package install timestamps, dist
|
||||
// URLs, support links, descriptions — none of which
|
||||
// affect what code runs.
|
||||
'composer_lock' => self::composerLockHash($projectRoot),
|
||||
'phpunit_xml' => self::hashIfExists($projectRoot.'/phpunit.xml'),
|
||||
'phpunit_xml_dist' => self::hashIfExists($projectRoot.'/phpunit.xml.dist'),
|
||||
// Pest's generated classes bake the code-generation logic
|
||||
// in — if TestCaseFactory changes (new attribute, different
|
||||
// method signature, etc.) every previously-recorded edge is
|
||||
// stale. Hashing the factory sources makes path-repo /
|
||||
// dev-main installs automatically rebuild their graphs when
|
||||
// Pest itself is edited.
|
||||
'pest_factory' => self::hashIfExists(__DIR__.'/../../Factories/TestCaseFactory.php'),
|
||||
'pest_method_factory' => self::hashIfExists(__DIR__.'/../../Factories/TestCaseMethodFactory.php'),
|
||||
// stale. Hashing via `ContentHash::of()` so cosmetic edits
|
||||
// (comments, formatting) don't drift the fingerprint.
|
||||
'pest_factory' => self::contentHashOrNull(__DIR__.'/../../Factories/TestCaseFactory.php'),
|
||||
'pest_method_factory' => self::contentHashOrNull(__DIR__.'/../../Factories/TestCaseMethodFactory.php'),
|
||||
// `vite.config.*` reshapes the module graph
|
||||
// `JsModuleGraph` records at the next `--tia` run; if
|
||||
// the config drifts without a rebuild, the stored
|
||||
// `$jsFileToComponents` map is silently stale.
|
||||
// `viteConfigHash` itself uses `ContentHash::of()` so
|
||||
// a comment-only edit to vite.config doesn't rebuild.
|
||||
'vite_config' => self::viteConfigHash($projectRoot),
|
||||
// `composer.json` carries `autoload-dev`, `extra.laravel`
|
||||
// package discovery, etc. — any change reshapes which
|
||||
// classes Pest can resolve at boot. Hashing the whole
|
||||
// file is over-conservative (cosmetic edits force
|
||||
// rebuild) but cheap, and over-rebuild is always safe.
|
||||
'composer_json' => self::hashIfExists($projectRoot.'/composer.json'),
|
||||
// `composer.json` hashed against a behavioural subset:
|
||||
// autoload(-dev), require(-dev), extra (Laravel
|
||||
// package discovery), repositories, minimum-stability,
|
||||
// and the platform / allow-plugins entries from
|
||||
// `config`. Cosmetic fields (description, keywords,
|
||||
// scripts, authors, funding, support) are excluded.
|
||||
'composer_json' => self::composerJsonHash($projectRoot),
|
||||
],
|
||||
'environmental' => [
|
||||
// PHP **minor** only (8.4, not 8.4.19) — CI's resolved patch
|
||||
@ -137,6 +150,45 @@ final readonly class Fingerprint
|
||||
return $aStructural === $bStructural;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of structural field names that drifted between
|
||||
* the stored and current fingerprints. Empty list = no drift.
|
||||
* Caller uses this to tell the user *why* the graph rebuilt — a
|
||||
* generic "graph outdated" message leaves people staring at
|
||||
* unrelated diffs.
|
||||
*
|
||||
* @param array<string, mixed> $stored
|
||||
* @param array<string, mixed> $current
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function structuralDrift(array $stored, array $current): array
|
||||
{
|
||||
$a = self::structuralOnly($stored);
|
||||
$b = self::structuralOnly($current);
|
||||
|
||||
$drifts = [];
|
||||
|
||||
foreach ($a as $key => $value) {
|
||||
if ($key === 'schema') {
|
||||
continue;
|
||||
}
|
||||
if (($b[$key] ?? null) !== $value) {
|
||||
$drifts[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($b as $key => $value) {
|
||||
if ($key === 'schema') {
|
||||
continue;
|
||||
}
|
||||
if (! array_key_exists($key, $a) && $value !== null) {
|
||||
$drifts[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($drifts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of field names that drifted between the stored and
|
||||
* current environmental fingerprints. Empty list = no drift. Caller
|
||||
@ -227,7 +279,7 @@ final readonly class Fingerprint
|
||||
$parts = [];
|
||||
|
||||
foreach (['vite.config.ts', 'vite.config.js', 'vite.config.mjs', 'vite.config.cjs', 'vite.config.mts'] as $name) {
|
||||
$hash = self::hashIfExists($projectRoot.'/'.$name);
|
||||
$hash = self::contentHashOrNull($projectRoot.'/'.$name);
|
||||
|
||||
if ($hash !== null) {
|
||||
$parts[] = $name.':'.$hash;
|
||||
@ -237,6 +289,184 @@ final readonly class Fingerprint
|
||||
return $parts === [] ? null : hash('xxh128', implode("\n", $parts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Behavioural subset of `composer.json`. Keeps the keys that
|
||||
* actually move test outcomes (autoload, require, extra,
|
||||
* repositories, minimum-stability, platform / allow-plugins
|
||||
* config) and drops cosmetic ones (description, keywords,
|
||||
* scripts, authors, funding, homepage, support). Falls back to
|
||||
* a raw hash on parse errors so any change still rebuilds.
|
||||
*/
|
||||
private static function composerJsonHash(string $projectRoot): ?string
|
||||
{
|
||||
$path = $projectRoot.'/composer.json';
|
||||
|
||||
if (! is_file($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($path);
|
||||
|
||||
if ($raw === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
|
||||
if (! is_array($data)) {
|
||||
$hash = @hash_file('xxh128', $path);
|
||||
|
||||
return $hash === false ? null : $hash;
|
||||
}
|
||||
|
||||
$config = is_array($data['config'] ?? null) ? $data['config'] : [];
|
||||
$relevantConfig = array_intersect_key($config, [
|
||||
'platform' => true,
|
||||
'allow-plugins' => true,
|
||||
]);
|
||||
|
||||
$relevant = [
|
||||
'autoload' => $data['autoload'] ?? null,
|
||||
'autoload-dev' => $data['autoload-dev'] ?? null,
|
||||
'require' => $data['require'] ?? null,
|
||||
'require-dev' => $data['require-dev'] ?? null,
|
||||
'extra' => $data['extra'] ?? null,
|
||||
'repositories' => $data['repositories'] ?? null,
|
||||
'minimum-stability' => $data['minimum-stability'] ?? null,
|
||||
'prefer-stable' => $data['prefer-stable'] ?? null,
|
||||
'config' => $relevantConfig === [] ? null : $relevantConfig,
|
||||
];
|
||||
|
||||
self::sortRecursively($relevant);
|
||||
|
||||
$json = json_encode($relevant);
|
||||
|
||||
return $json === false ? null : hash('xxh128', $json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Behavioural subset of `composer.lock`. For every package in
|
||||
* `packages` and `packages-dev`, keeps version + dist/source
|
||||
* reference (commit SHA — catches dev-branch updates that don't
|
||||
* bump the version string) + autoload(-dev) + extra (Laravel
|
||||
* package discovery). Drops install timestamps, dist URLs,
|
||||
* support links, descriptions, etc. — none of which change what
|
||||
* code runs.
|
||||
*/
|
||||
private static function composerLockHash(string $projectRoot): ?string
|
||||
{
|
||||
$path = $projectRoot.'/composer.lock';
|
||||
|
||||
if (! is_file($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($path);
|
||||
|
||||
if ($raw === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
|
||||
if (! is_array($data)) {
|
||||
$hash = @hash_file('xxh128', $path);
|
||||
|
||||
return $hash === false ? null : $hash;
|
||||
}
|
||||
|
||||
$relevant = [
|
||||
'platform' => $data['platform'] ?? null,
|
||||
'platform-dev' => $data['platform-dev'] ?? null,
|
||||
];
|
||||
|
||||
foreach (['packages', 'packages-dev'] as $section) {
|
||||
if (! isset($data[$section])) {
|
||||
continue;
|
||||
}
|
||||
if (! is_array($data[$section])) {
|
||||
continue;
|
||||
}
|
||||
$packages = [];
|
||||
|
||||
foreach ($data[$section] as $package) {
|
||||
if (! is_array($package)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $package['name'] ?? null;
|
||||
|
||||
if (! is_string($name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$packages[$name] = [
|
||||
'version' => $package['version'] ?? null,
|
||||
'reference' => self::lockReference($package),
|
||||
'autoload' => $package['autoload'] ?? null,
|
||||
'autoload-dev' => $package['autoload-dev'] ?? null,
|
||||
'extra' => $package['extra'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
ksort($packages);
|
||||
$relevant[$section] = $packages;
|
||||
}
|
||||
|
||||
self::sortRecursively($relevant);
|
||||
|
||||
$json = json_encode($relevant);
|
||||
|
||||
return $json === false ? null : hash('xxh128', $json);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $package
|
||||
*/
|
||||
private static function lockReference(array $package): ?string
|
||||
{
|
||||
$dist = is_array($package['dist'] ?? null) ? $package['dist'] : [];
|
||||
$source = is_array($package['source'] ?? null) ? $package['source'] : [];
|
||||
|
||||
$reference = $dist['reference'] ?? $source['reference'] ?? null;
|
||||
|
||||
return is_string($reference) ? $reference : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively sorts associative arrays by key so semantically
|
||||
* equivalent JSON produces the same hash regardless of key
|
||||
* ordering. Lists (numeric arrays) keep their order — they're
|
||||
* meaningful in `repositories`, `autoload.files`, etc.
|
||||
*/
|
||||
private static function sortRecursively(mixed &$value): void
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$isAssoc = ! array_is_list($value);
|
||||
|
||||
if ($isAssoc) {
|
||||
ksort($value);
|
||||
}
|
||||
|
||||
foreach ($value as &$child) {
|
||||
self::sortRecursively($child);
|
||||
}
|
||||
}
|
||||
|
||||
private static function contentHashOrNull(string $path): ?string
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = ContentHash::of($path);
|
||||
|
||||
return $hash === false ? null : $hash;
|
||||
}
|
||||
|
||||
private static function hashIfExists(string $path): ?string
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
|
||||
Reference in New Issue
Block a user