fix(cli): repair legacy core ledgers and make --keep-local durable
Store canonical core hashes in lock.core, add core.schema and repair legacy ledgers from the registered commit before deciding drift. Skipped --keep-local files remain tracked with the target canonical hash, fail closed when the old core cannot be resolved, and update core reports per-file actions.
This commit is contained in:
@@ -46,6 +46,30 @@ Harness::it('lock guarda core y componentes con schema esperado', function () {
|
||||
rrmdir($dir);
|
||||
});
|
||||
|
||||
Harness::it('guardar componentes no asciende el esquema del core', function () {
|
||||
$dir = tmpdir();
|
||||
$lock = Lock::empty();
|
||||
$lock->setCore(['schema' => 1, 'version' => '1.0.0', 'commit' => 'abc', 'files' => ['a' => 'sha256:x']]);
|
||||
$lock->setComponent('p:c', [
|
||||
'package' => 'p',
|
||||
'version' => '1.0.0',
|
||||
'toolbox_commit' => null,
|
||||
'mode' => 'copy',
|
||||
'files' => [],
|
||||
]);
|
||||
$lock->save($dir);
|
||||
|
||||
eq(1, Lock::load($dir)->coreSchema(), 'un guardado de componentes preserva el esquema legacy');
|
||||
$raw = json_decode((string) file_get_contents(Lock::path($dir)), true);
|
||||
eq(1, $raw['core']['schema'], 'el JSON conserva schema 1');
|
||||
|
||||
unset($raw['core']['schema']);
|
||||
file_put_contents(Lock::path($dir), json_encode($raw));
|
||||
eq(1, Lock::load($dir)->coreSchema(), 'sin marca se interpreta como esquema 1');
|
||||
|
||||
rrmdir($dir);
|
||||
});
|
||||
|
||||
Harness::it('ownership detecta colisión de rutas', function () {
|
||||
$lock = Lock::empty();
|
||||
$lock->setCore(['files' => ['src/Libs/X.php' => 'sha256:a']]);
|
||||
|
||||
@@ -6,6 +6,8 @@ use Toolbox\Commands\ListCommand;
|
||||
use Toolbox\Commands\RemoveCommand;
|
||||
use Toolbox\Commands\UpdateCommand;
|
||||
use Toolbox\Console;
|
||||
use Toolbox\CoreProvider;
|
||||
use Toolbox\Drift;
|
||||
use Toolbox\Lock;
|
||||
|
||||
Harness::suite('group7');
|
||||
@@ -71,6 +73,82 @@ function g7_console(&$stream): Console
|
||||
return new Console($stream, $stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repo del core con dos versiones y una ruta que desaparece en v0.1.1.
|
||||
* Queda con el working tree en el último tag.
|
||||
*/
|
||||
function g7_core_history(): string
|
||||
{
|
||||
$dir = tmpdir('corehist');
|
||||
write_file($dir . '/autoload.php', <<<'PHP'
|
||||
<?php
|
||||
define('ROOT_DIR', __DIR__);
|
||||
define('ROOT_CORE', ROOT_DIR . '/src');
|
||||
spl_autoload_register(function (string $class): void {
|
||||
$fp = str_replace('\\', '/', $class);
|
||||
$file = ROOT_CORE . '/' . dirname($fp) . '/' . basename($fp) . '.php';
|
||||
if (file_exists($file)) {
|
||||
require_once $file;
|
||||
}
|
||||
});
|
||||
PHP);
|
||||
write_file($dir . '/config.php', "<?php\n");
|
||||
write_file($dir . '/src/Libs/Router.php', "<?php\n// router-v1\n");
|
||||
write_file($dir . '/src/Libs/Legacy.php', "<?php\n// legacy\n");
|
||||
write_file($dir . '/src/Empty/.gitkeep', '');
|
||||
git_init($dir);
|
||||
git_commit($dir, 'core v1');
|
||||
git_tag($dir, 'v0.1.0');
|
||||
|
||||
write_file($dir . '/src/Libs/Router.php', "<?php\n// router-v2\n");
|
||||
exec('git -C ' . escapeshellarg($dir) . ' rm -q src/Libs/Legacy.php');
|
||||
git_commit($dir, 'core v2');
|
||||
git_tag($dir, 'v0.1.1');
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
function g7_core_tag(string $dir, string $version, string $router): void
|
||||
{
|
||||
write_file($dir . '/src/Libs/Router.php', "<?php\n// " . $router . "\n");
|
||||
git_commit($dir, 'core ' . $version);
|
||||
git_tag($dir, 'v' . $version);
|
||||
}
|
||||
|
||||
/** @param list<string> $paths */
|
||||
function g7_materialize(string $repo, string $ref, string $root, array $paths): void
|
||||
{
|
||||
foreach ($paths as $path) {
|
||||
$contents = shell_exec(
|
||||
'git -C ' . escapeshellarg($repo) . ' show ' . escapeshellarg($ref . ':' . $path) . ' 2>/dev/null'
|
||||
);
|
||||
write_file($root . '/' . $path, (string) $contents);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
function g7_legacy_lock(string $proj, string $repo): array
|
||||
{
|
||||
$oldCommit = trim((string) shell_exec('git -C ' . escapeshellarg($repo) . ' rev-parse v0.1.0'));
|
||||
$routerHash = 'sha256:' . hash('sha256', (string) shell_exec(
|
||||
'git -C ' . escapeshellarg($repo) . ' show v0.1.0:src/Libs/Router.php'
|
||||
));
|
||||
|
||||
$files = [
|
||||
'autoload.php' => Drift::hash($proj . '/autoload.php'),
|
||||
'config.php' => Drift::hash($proj . '/config.php'),
|
||||
'src/Libs/Router.php' => $routerHash,
|
||||
'src/Libs/Legacy.php' => Drift::hash($proj . '/src/Libs/Legacy.php'),
|
||||
];
|
||||
|
||||
write_file($proj . '/.duckbrain/lock.json', json_encode([
|
||||
'core' => ['version' => '0.1.0', 'commit' => $oldCommit, 'files' => $files],
|
||||
'components' => [],
|
||||
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n");
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
Harness::it('install descarga el core en un directorio vacío', function () {
|
||||
$proj = tmpdir('proj');
|
||||
$core = g7_core('0.1.0');
|
||||
@@ -320,3 +398,146 @@ Harness::it('un comando del proyecto sin describe() no carga (interfaz estricta)
|
||||
|
||||
rrmdir($proj);
|
||||
});
|
||||
|
||||
Harness::it('canonicalAt lee un ref viejo y falla cerrado con refs o rutas ausentes', function () {
|
||||
$repo = g7_core_history();
|
||||
$old = trim((string) shell_exec('git -C ' . escapeshellarg($repo) . ' rev-parse v0.1.0'));
|
||||
$provider = CoreProvider::fromDirectory($repo);
|
||||
|
||||
$hashes = $provider->canonicalAt($old, null, ['src/Libs/Router.php', 'src/Libs/Legacy.php', 'src/Empty/.gitkeep']);
|
||||
ok(is_array($hashes), 'resuelve el ref viejo');
|
||||
$canonicalRouter = 'sha256:' . hash('sha256', (string) shell_exec(
|
||||
'git -C ' . escapeshellarg($repo) . ' show v0.1.0:src/Libs/Router.php'
|
||||
));
|
||||
eq($canonicalRouter, $hashes['src/Libs/Router.php'], 'hash canónico histórico');
|
||||
eq('sha256:' . hash('sha256', ''), $hashes['src/Empty/.gitkeep'], 'archivo vacío resuelto');
|
||||
|
||||
eq(null, $provider->canonicalAt('deadbeef', '9.9.9', ['src/Libs/Router.php']), 'ref inexistente');
|
||||
eq(null, $provider->canonicalAt($old, null, ['src/Libs/Nope.php']), 'ruta inexistente');
|
||||
|
||||
rrmdir($repo);
|
||||
});
|
||||
|
||||
Harness::it('update core repara un lock heredado y protege keep-local de forma durable', function () {
|
||||
$repo = g7_core_history();
|
||||
$tool = g7_toolbox();
|
||||
$proj = tmpdir('proj');
|
||||
$paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php'];
|
||||
|
||||
g7_materialize($repo, 'v0.1.0', $proj, $paths);
|
||||
$config = "<?php\n// config local del proyecto\n";
|
||||
$legacy = "<?php\n// legacy custom\n";
|
||||
write_file($proj . '/config.php', $config);
|
||||
write_file($proj . '/src/Libs/Legacy.php', $legacy);
|
||||
g7_legacy_lock($proj, $repo);
|
||||
|
||||
$stream = null;
|
||||
$console = g7_console($stream);
|
||||
$code = (new UpdateCommand($console))->run([
|
||||
'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local',
|
||||
]);
|
||||
rewind($stream);
|
||||
$out = (string) stream_get_contents($stream);
|
||||
|
||||
eq(0, $code, 'el update termina sin abortos');
|
||||
eq($config, (string) file_get_contents($proj . '/config.php'), 'config local intacto');
|
||||
eq($legacy, (string) file_get_contents($proj . '/src/Libs/Legacy.php'), 'obsoleto local intacto');
|
||||
ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v2'), 'Router actualizado');
|
||||
ok(str_contains($out, 'skipped config.php'), 'reporta el archivo saltado');
|
||||
ok(str_contains($out, 'resumen:'), 'reporta un resumen por archivo');
|
||||
ok(str_contains($out, 'obsoleto conservado localmente: src/Libs/Legacy.php'), 'avisa del obsoleto conservado');
|
||||
|
||||
$lock = Lock::load($proj);
|
||||
eq(2, $lock->coreSchema(), 'el lock migra a esquema 2');
|
||||
eq('0.1.1', $lock->core()['version']);
|
||||
ok(!isset($lock->core()['files']['src/Libs/Legacy.php']), 'el obsoleto sale del ledger');
|
||||
$canonicalConfig = 'sha256:' . hash('sha256', (string) shell_exec(
|
||||
'git -C ' . escapeshellarg($repo) . ' show v0.1.1:config.php'
|
||||
));
|
||||
eq($canonicalConfig, $lock->core()['files']['config.php'], 'el ledger guarda el canónico destino, no el hash local');
|
||||
|
||||
g7_core_tag($repo, '0.1.2', 'router-v3');
|
||||
(new UpdateCommand(g7_console($quiet)))->run([
|
||||
'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local',
|
||||
]);
|
||||
eq($config, (string) file_get_contents($proj . '/config.php'), 'keep-local durable: config sigue intacto');
|
||||
ok(str_contains((string) file_get_contents($proj . '/src/Libs/Router.php'), 'router-v3'), 'Router sigue actualizándose');
|
||||
eq('0.1.2', Lock::load($proj)->core()['version']);
|
||||
|
||||
g7_core_tag($repo, '0.1.3', 'router-v4');
|
||||
$rc = (new UpdateCommand(g7_console($quiet2)))->run([
|
||||
'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes',
|
||||
]);
|
||||
eq(1, $rc, 'sin --keep-local el drift aborta');
|
||||
eq($config, (string) file_get_contents($proj . '/config.php'), 'el aborto no pisa config');
|
||||
eq('0.1.2', Lock::load($proj)->core()['version'], 'el lock no avanza con abortos');
|
||||
|
||||
rrmdir($proj);
|
||||
rrmdir($tool);
|
||||
rrmdir($repo);
|
||||
});
|
||||
|
||||
Harness::it('update core falla cerrado si no puede reparar el lock heredado', function () {
|
||||
$repo = g7_core_history();
|
||||
$fresh = g7_core('9.9.9', 'router-fresh');
|
||||
$tool = g7_toolbox();
|
||||
$proj = tmpdir('proj');
|
||||
$paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php'];
|
||||
|
||||
g7_materialize($repo, 'v0.1.0', $proj, $paths);
|
||||
write_file($proj . '/config.php', "<?php\n// custom\n");
|
||||
g7_legacy_lock($proj, $repo);
|
||||
|
||||
$lockBefore = (string) file_get_contents(Lock::path($proj));
|
||||
$routerBefore = (string) file_get_contents($proj . '/src/Libs/Router.php');
|
||||
|
||||
$stream = null;
|
||||
$console = g7_console($stream);
|
||||
$code = (new UpdateCommand($console))->run([
|
||||
'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $fresh, '--yes', '--keep-local',
|
||||
]);
|
||||
rewind($stream);
|
||||
$out = (string) stream_get_contents($stream);
|
||||
|
||||
eq(1, $code, 'falla cerrado');
|
||||
eq($lockBefore, (string) file_get_contents(Lock::path($proj)), 'el lock no se reescribe');
|
||||
eq($routerBefore, (string) file_get_contents($proj . '/src/Libs/Router.php'), 'no se escribe el core');
|
||||
ok(str_contains($out, 'No se pudo reparar el lock del core'), 'explica el bloqueo');
|
||||
|
||||
rrmdir($proj);
|
||||
rrmdir($tool);
|
||||
rrmdir($repo);
|
||||
rrmdir($fresh);
|
||||
});
|
||||
|
||||
Harness::it('update core --dry-run planifica el drift sin escribir', function () {
|
||||
$repo = g7_core_history();
|
||||
$tool = g7_toolbox();
|
||||
$proj = tmpdir('proj');
|
||||
$paths = ['autoload.php', 'config.php', 'src/Libs/Router.php', 'src/Libs/Legacy.php'];
|
||||
|
||||
g7_materialize($repo, 'v0.1.0', $proj, $paths);
|
||||
write_file($proj . '/config.php', "<?php\n// custom\n");
|
||||
g7_legacy_lock($proj, $repo);
|
||||
|
||||
$lockBefore = (string) file_get_contents(Lock::path($proj));
|
||||
$routerBefore = (string) file_get_contents($proj . '/src/Libs/Router.php');
|
||||
|
||||
$stream = null;
|
||||
$console = g7_console($stream);
|
||||
$code = (new UpdateCommand($console))->run([
|
||||
'core', '--root=' . $proj, '--toolbox=' . $tool, '--core-dir=' . $repo, '--yes', '--keep-local', '--dry-run',
|
||||
]);
|
||||
rewind($stream);
|
||||
$out = (string) stream_get_contents($stream);
|
||||
|
||||
eq(0, $code, 'dry-run termina bien');
|
||||
ok(str_contains($out, 'planned-skipped config.php'), 'planifica el archivo protegido');
|
||||
ok(str_contains($out, 'planned-overwritten src/Libs/Router.php'), 'planifica el archivo a actualizar');
|
||||
eq($lockBefore, (string) file_get_contents(Lock::path($proj)), 'dry-run no toca el lock');
|
||||
eq($routerBefore, (string) file_get_contents($proj . '/src/Libs/Router.php'), 'dry-run no escribe archivos');
|
||||
|
||||
rrmdir($proj);
|
||||
rrmdir($tool);
|
||||
rrmdir($repo);
|
||||
});
|
||||
|
||||
@@ -143,6 +143,7 @@ Harness::it('adopt del core registra hash canónico y marca ediciones locales',
|
||||
eq('sha256:' . hash('sha256', $canonicalRouter), $coreHashRouter, 'hash de Router.php = canónico');
|
||||
eq('sha256:' . hash('sha256', $canonicalConfig), $coreHashConfig, 'hash de config.php = canónico (aunque el local difiera)');
|
||||
eq('local', $lock->core()['origin'] ?? null, 'origin = local porque hay ediciones locales');
|
||||
eq(2, $lock->coreSchema(), 'adopt graba el esquema canónico');
|
||||
|
||||
ok(file_get_contents($proj . '/config.php') !== $canonicalConfig, 'la copia local customizada no se tocó');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user