mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 23:32:17 +01:00
fix(tia): support a Pest project nested in a git repository (#1914)
* fix(tia): support a Pest project nested in a git repository Tia refused to run whenever the Pest project was not the git repository root, because git prints and addresses paths relative to the repository root while the dependency graph is keyed on project-relative paths. Translate at every git boundary instead of guarding against the mismatch. `Git::pathPrefix()` resolves the directory of the project inside the repository, and `ChangedFiles` strips that prefix off the paths git prints, drops the paths of sibling packages, and prepends it to `git show`. Both diffs read `-z` records, because `core.quotePath` C-quotes a path outside ASCII, and `--no-relative` holds the frame when a user sets `diff.relative`. Three places read `<projectRoot>/.git` directly, which is absent in a nested project and in a worktree, and now ask git: `BaseRemote`, `Storage` and `Fingerprint::isTrackedByGit()`. `Storage` folds the prefix into the state key so two projects in one repository do not share a graph, and the fingerprint carries the prefix so a baseline published by a sibling project is rejected. A project at the repository root keeps the key and the fingerprint it had. `git check-ignore` runs without `--no-index`, so a tracked file that an ignore rule matches counts as part of the project on both sides of the boundary. Changes outside the project are dropped and reported by count in the run summary, so a nested project does not look idle when the repository around it moved. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(tia): keep whitespace at the edge of the paths git returns * fix(tia): run the full suite when a PHP file outside a nested project changes --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -1963,6 +1963,25 @@
|
||||
✓ units → it collects every test class the given units bundle
|
||||
✓ units → it writes bundled units, and reads back both shapes
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\ChangedFiles
|
||||
✓ it resolves the project prefix inside the repository
|
||||
✓ it keeps a space that begins the name of the project directory
|
||||
✓ it keeps the character that ends the name of the repository root with dataset "a space"
|
||||
✓ it keeps the character that ends the name of the repository root with dataset "a carriage return"
|
||||
✓ it reports working tree changes relative to the project, dropping siblings
|
||||
✓ it reports committed changes relative to the project, dropping siblings
|
||||
✓ it drops a file whose content came back to its baseline across the prefix boundary
|
||||
✓ it honours a gitignore inside the project
|
||||
✓ it leaves a project at the repository root untouched
|
||||
✓ it reports the changes it left outside the project
|
||||
✓ it reports nothing outside the project for a project at the repository root
|
||||
✓ it ties a fingerprint to the project location inside the repository
|
||||
✓ it sees a committed non-ascii path that git would otherwise quote
|
||||
✓ it sees a committed non-ascii path at the repository root too
|
||||
✓ it keeps a tracked outside change and drops an untracked ignored one
|
||||
✓ it keeps a tracked project change that the ignore rules match
|
||||
✓ it reads committed paths from the repository root even when git is told to be relative
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\ConfigurationFile
|
||||
✓ it fingerprints the path and the content of the configuration file
|
||||
✓ it tells an identical copy under another name apart from the original
|
||||
@@ -2140,6 +2159,7 @@
|
||||
PASS Tests\Unit\Plugins\Tia\Storage
|
||||
✓ it uses a project-relative configured directory
|
||||
✓ it uses an absolute configured directory
|
||||
✓ it keeps the state directory of a project at the repository root
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\TableExtractor
|
||||
✓ fromSql() → it extracts tables from plain DML
|
||||
@@ -2627,4 +2647,4 @@
|
||||
✓ pass with dataset with ('my-datas-set-value')
|
||||
✓ within describe → pass with dataset with ('my-datas-set-value')
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1919 passed (4374 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 38 skipped, 1937 passed (4405 assertions)
|
||||
@@ -158,18 +158,6 @@ test('the default branch baseline survives every branch that comes and goes', fu
|
||||
->and($project->graph()['baselines']['master']['results'])->toHaveCount(Project::TOTAL_TESTS);
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a project below the git repository root refuses to run and writes nothing', function (array $arguments): void {
|
||||
$project = Project::make('master');
|
||||
$nested = $project->nested();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
expect($result->exitCode)->toBe(1, $result->describe())
|
||||
->and($result->output)->toContain('Tia mode requires the git repository root')
|
||||
->and($project->path('.home/.pest'))->not->toBeDirectory()
|
||||
->and($nested.DIRECTORY_SEPARATOR.'.pest')->not->toBeDirectory();
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a repository with no commits says so, and leaves plain runs alone', function (): void {
|
||||
$project = Project::withoutGit();
|
||||
$project->git()->run(['init', '--quiet']);
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia;
|
||||
use Pest\Support\Coverage;
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
afterEach(function (): void {
|
||||
Project::destroyAll();
|
||||
});
|
||||
|
||||
/**
|
||||
* @return array{0: Project, 1: string}
|
||||
*/
|
||||
function tiaMonorepo(string $directory = 'nested'): array
|
||||
{
|
||||
$project = Project::make('master');
|
||||
$nested = $project->nested($directory);
|
||||
|
||||
$project->write($directory.'/resources/views/greeting.blade.php', "<p>Hello</p>\n");
|
||||
$project->write('frontend/widget.php', "<?php\n\n\$widget = 1;\n");
|
||||
$project->git()->commit('add the nested project and a sibling');
|
||||
|
||||
$project->seedFor($nested, 'master');
|
||||
|
||||
$project->mutateGraph(function (array $graph): array {
|
||||
$id = count($graph['files']);
|
||||
$graph['files'][$id] = 'resources/views/greeting.blade.php';
|
||||
$graph['edges']['tests/Unit/GreeterTest.php'][] = $id;
|
||||
|
||||
return $graph;
|
||||
});
|
||||
|
||||
return [$project, $nested];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: Project, 1: string}
|
||||
*/
|
||||
function tiaMonorepoWithSharedDependency(): array
|
||||
{
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Hello, %s!'));
|
||||
$project->write('nested/tests/Pest.php', implode("\n", [
|
||||
'<?php',
|
||||
'',
|
||||
'declare(strict_types=1);',
|
||||
'',
|
||||
"require_once __DIR__.'/../../shared/greeting.php';",
|
||||
"require_once __DIR__.'/../app/Calculator.php';",
|
||||
"require_once __DIR__.'/../app/Greeter.php';",
|
||||
'',
|
||||
]));
|
||||
$project->write('nested/app/Greeter.php', implode("\n", [
|
||||
'<?php',
|
||||
'',
|
||||
'declare(strict_types=1);',
|
||||
'',
|
||||
'namespace Fixture\\App;',
|
||||
'',
|
||||
'final class Greeter',
|
||||
'{',
|
||||
' public function greet(string $name): string',
|
||||
' {',
|
||||
' return \\shared_greeting($name);',
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
]));
|
||||
$project->git()->commit('let the project load a shared PHP dependency');
|
||||
|
||||
$project->seedFor($nested, 'master');
|
||||
|
||||
return [$project, $nested];
|
||||
}
|
||||
|
||||
function tiaSharedGreeting(string $format, string $wrapper = ''): string
|
||||
{
|
||||
return implode("\n", [
|
||||
'<?php',
|
||||
'',
|
||||
'declare(strict_types=1);',
|
||||
'',
|
||||
'function shared_greeting(string $name): string',
|
||||
'{',
|
||||
sprintf(' return %s(sprintf(%s, $name));', $wrapper, var_export($format, true)),
|
||||
'}',
|
||||
'',
|
||||
]);
|
||||
}
|
||||
|
||||
test('a project in a subdirectory of the repository replays its baseline', function (array $arguments): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($result->affected())->toBe(0, $result->describe());
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a working tree change inside the subdirectory selects the tests that depend on it', function (array $arguments): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('nested/resources/views/greeting.blade.php', "<p>Hi</p>\n");
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->affected())->toBe(2, $result->describe())
|
||||
->and($result->replayed())->toBe(4, $result->describe());
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a committed change inside the subdirectory selects the tests that depend on it', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('nested/resources/views/greeting.blade.php', "<p>Hi</p>\n");
|
||||
$project->git()->commit('reword the greeting');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->affected())->toBe(2, $result->describe())
|
||||
->and($result->replayed())->toBe(4, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a project in a directory whose name begins with a space sees its own changes', function (): void {
|
||||
[$project, $nested] = tiaMonorepo(' backend');
|
||||
|
||||
$project->write(' backend/resources/views/greeting.blade.php', "<p>Hi</p>\n");
|
||||
$project->git()->commit('reword the greeting');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->affected())->toBe(2, $result->describe())
|
||||
->and($result->replayed())->toBe(4, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a change in a sibling package that holds no PHP leaves the subdirectory project untouched', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('frontend/app.js', "export const widget = 2;\n");
|
||||
$project->write('frontend/README.md', "# Frontend\n");
|
||||
$project->git()->commit('rework the sibling');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($result->affected())->toBe(0, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a committed change in a shared PHP dependency outside the project runs the full suite', function (array $arguments): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Goodbye, %s!'));
|
||||
$project->git()->commit('reword the shared greeting');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
expect($result->exitCode)->not->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Detected 1 changed PHP file outside this project.')
|
||||
->and($result->replayed())->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain('2 failed')
|
||||
->and($result->tally())->toContain('4 passed');
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a committed change in a shared PHP dependency outside the project runs the full suite once', function (array $arguments): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Hello, %s!', 'trim'));
|
||||
$project->git()->commit('route the shared greeting through trim');
|
||||
|
||||
$first = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
$second = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
expect($first->exitCode)->toBe(0, $first->describe())
|
||||
->and($first->replayed())->toBe(0, $first->describe())
|
||||
->and($first->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($second->exitCode)->toBe(0, $second->describe())
|
||||
->and($second->output)->not->toContain('changed PHP file outside this project')
|
||||
->and($second->replayed())->toBe(Project::TOTAL_TESTS, $second->describe());
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows();
|
||||
|
||||
test('a committed change in a shared PHP dependency outside the project records the edges of the project again', function (array $arguments): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Hello, %s!', 'trim'));
|
||||
$project->write('nested/app/Greeter.php', <<<'PHP'
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fixture\App;
|
||||
|
||||
final class Greeter
|
||||
{
|
||||
public function greet(string $name): string
|
||||
{
|
||||
(new Calculator)->add(1, 1);
|
||||
|
||||
return \shared_greeting($name);
|
||||
}
|
||||
}
|
||||
PHP);
|
||||
$project->git()->commit('let the greeter use the calculator');
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', ...$arguments);
|
||||
|
||||
$graph = $project->graph();
|
||||
$edges = array_map(fn (int $id): string => $graph['files'][$id], $graph['edges']['tests/Unit/GreeterTest.php']);
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Detected 1 changed PHP file outside this project.')
|
||||
->and($edges)->toContain('app/Calculator.php');
|
||||
})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
|
||||
|
||||
test('an uncommitted change in a shared PHP dependency outside the project runs the full suite and records nothing', function (): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
$recorded = $project->graph()['baselines']['master'] ?? null;
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Goodbye, %s!'));
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->not->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Detected 1 changed PHP file outside this project.')
|
||||
->and($result->output)->toContain('recording nothing')
|
||||
->and($result->replayed())->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain('2 failed')
|
||||
->and($project->graph()['baselines']['master'] ?? null)->toBe($recorded);
|
||||
})->skipOnWindows();
|
||||
|
||||
test('an uncommitted change in a shared PHP dependency outside the project leaves the coverage cache alone', function (): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
$recording = $project->pestIn($nested, '--tia', '--coverage');
|
||||
|
||||
$cache = $project->graphDir().DIRECTORY_SEPARATOR.'coverage.bin.gz';
|
||||
$recorded = file_get_contents($cache);
|
||||
|
||||
$project->write('shared/greeting.php', implode("\n", [
|
||||
'<?php',
|
||||
'',
|
||||
'declare(strict_types=1);',
|
||||
'',
|
||||
'function shared_greeting(string $name): string',
|
||||
'{',
|
||||
' (new \\Fixture\\App\\Calculator)->add(1, 1);',
|
||||
'',
|
||||
' return sprintf(\'Hello, %s!\', $name);',
|
||||
'}',
|
||||
'',
|
||||
]));
|
||||
|
||||
$result = $project->pestIn($nested, '--tia', '--coverage');
|
||||
|
||||
expect($recording->exitCode)->toBe(0, $recording->describe())
|
||||
->and($recorded)->toBeString()
|
||||
->and($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('recording nothing')
|
||||
->and(file_get_contents($cache))->toBe($recorded);
|
||||
})->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available');
|
||||
|
||||
test('an uncommitted change in a shared PHP dependency outside the project records no first graph', function (): void {
|
||||
[$project, $nested] = tiaMonorepoWithSharedDependency();
|
||||
|
||||
unlink($project->graphDir().DIRECTORY_SEPARATOR.Tia::KEY_GRAPH);
|
||||
|
||||
$project->write('shared/greeting.php', tiaSharedGreeting('Hello, %s!', 'trim'));
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('recording nothing')
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($project->graphExists())->toBeFalse($result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a committed change inside the subdirectory that is undone again selects nothing', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('nested/resources/views/greeting.blade.php', "<p>Hi</p>\n");
|
||||
$project->git()->commit('reword the greeting');
|
||||
|
||||
$project->write('nested/resources/views/greeting.blade.php', "<p>Hello</p>\n");
|
||||
$project->git()->commit('put the greeting back');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($result->affected())->toBe(0, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('two projects in the same repository keep their state apart', function (): void {
|
||||
$project = Project::make('master');
|
||||
|
||||
$api = $project->nested('services/api');
|
||||
$admin = $project->nested('services/admin');
|
||||
|
||||
$project->git()->commit('add both projects');
|
||||
|
||||
expect($project->stateDirFor($api))->not->toBe($project->stateDirFor($admin));
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a nested project fetches the baseline published for it', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$environment = $project->gh('ok', $project->detachGraph());
|
||||
|
||||
$result = $project->pestWithEnvironment($nested, $environment, '--tia', '--baselined');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Downloading TIA baseline')
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a nested project refuses a baseline published by a sibling project', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
/** @var array<string, mixed> $graph */
|
||||
$graph = json_decode($project->detachGraph(), true);
|
||||
$graph['fingerprint']['structural']['project_prefix'] = 'services/admin/';
|
||||
|
||||
$environment = $project->gh('ok', (string) json_encode($graph, JSON_UNESCAPED_SLASHES));
|
||||
|
||||
$result = $project->pestWithEnvironment($nested, $environment, '--tia', '--baselined');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed')
|
||||
->and($result->replayed())->toBe(0, $result->describe());
|
||||
})->skipOnWindows();
|
||||
|
||||
test('it says when changes outside the project were ignored', function (): void {
|
||||
[$project, $nested] = tiaMonorepo();
|
||||
|
||||
$project->write('frontend/app.js', "export const widget = 2;\n");
|
||||
$project->git()->commit('rework the sibling');
|
||||
$project->snapshot();
|
||||
|
||||
$result = $project->pestIn($nested, '--tia');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('1 changed file outside this project was ignored');
|
||||
})->skipOnWindows();
|
||||
@@ -131,6 +131,9 @@ final class Project
|
||||
}
|
||||
|
||||
self::copy(__DIR__.'/app', $path);
|
||||
|
||||
$this->write($directory.DIRECTORY_SEPARATOR.'.gitignore', implode("\n", ['/vendor/', '/.phpunit.cache/', '']));
|
||||
|
||||
$this->scaffoldVendor($path);
|
||||
|
||||
return $path;
|
||||
@@ -395,7 +398,12 @@ final class Project
|
||||
|
||||
public function graphDir(): string
|
||||
{
|
||||
return $this->withHome(fn (): string => Storage::tempDir($this->graphRoot));
|
||||
return $this->stateDirFor($this->graphRoot);
|
||||
}
|
||||
|
||||
public function stateDirFor(string $root): string
|
||||
{
|
||||
return $this->withHome(fn (): string => Storage::tempDir($root));
|
||||
}
|
||||
|
||||
public function graphExists(): bool
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia\ChangedFiles;
|
||||
use Pest\Plugins\Tia\Fingerprint;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Tests\Fixtures\Tia\GitRepo;
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
function tiaMonorepoRoots(?string $created = null): array
|
||||
{
|
||||
static $roots = [];
|
||||
|
||||
if ($created !== null) {
|
||||
$roots[] = $created;
|
||||
|
||||
return $roots;
|
||||
}
|
||||
|
||||
$all = $roots;
|
||||
$roots = [];
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{root: string, project: string, repo: GitRepo, sha: string}
|
||||
*/
|
||||
function tiaMonorepoRepository(): array
|
||||
{
|
||||
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8));
|
||||
|
||||
mkdir($root.'/backend/app', 0755, true);
|
||||
mkdir($root.'/frontend', 0755, true);
|
||||
|
||||
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 1;\n");
|
||||
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 1;\n");
|
||||
|
||||
tiaMonorepoRoots($root);
|
||||
|
||||
$repo = new GitRepo($root);
|
||||
$repo->init('master');
|
||||
|
||||
return ['root' => $root, 'project' => $root.'/backend', 'repo' => $repo, 'sha' => $repo->sha()];
|
||||
}
|
||||
|
||||
afterEach(function (): void {
|
||||
foreach (tiaMonorepoRoots() as $root) {
|
||||
new Process(['rm', '-rf', $root])->run();
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves the project prefix inside the repository', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->gitPrefix())->toBe('backend/')
|
||||
->and(new ChangedFiles($monorepo['root'])->gitPrefix())->toBeEmpty();
|
||||
})->skipOnWindows();
|
||||
|
||||
it('keeps a space that begins the name of the project directory', function (): void {
|
||||
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8));
|
||||
|
||||
mkdir($root.'/ backend/app', 0755, true);
|
||||
file_put_contents($root.'/ backend/app/Service.php', "<?php\n\$service = 1;\n");
|
||||
|
||||
tiaMonorepoRoots($root);
|
||||
|
||||
$repo = new GitRepo($root);
|
||||
$repo->init('master');
|
||||
|
||||
file_put_contents($root.'/ backend/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
|
||||
$project = new ChangedFiles($root.'/ backend');
|
||||
|
||||
expect($project->gitPrefix())->toBe(' backend/')
|
||||
->and($project->since($repo->sha()))->toBe(['app/Service.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('keeps the character that ends the name of the repository root', function (string $lastCharacter): void {
|
||||
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-monorepo-'.bin2hex(random_bytes(8)).$lastCharacter;
|
||||
|
||||
mkdir($root.'/backend/app', 0755, true);
|
||||
mkdir($root.'/frontend', 0755, true);
|
||||
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 1;\n");
|
||||
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 1;\n");
|
||||
|
||||
tiaMonorepoRoots($root);
|
||||
|
||||
$repo = new GitRepo($root);
|
||||
$repo->init('master');
|
||||
|
||||
file_put_contents($root.'/backend/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
file_put_contents($root.'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
$project = new ChangedFiles($root.'/backend');
|
||||
|
||||
expect($project->since($repo->sha()))->toBe(['app/Service.php'])
|
||||
->and($project->outsideProject())->toBe(['frontend/widget.php']);
|
||||
})->with(['a space' => ' ', 'a carriage return' => "\r"])->skipOnWindows();
|
||||
|
||||
it('reports working tree changes relative to the project, dropping siblings', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
||||
->toBe(['app/Service.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('reports committed changes relative to the project, dropping siblings', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
$monorepo['repo']->commit('Second commit');
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
||||
->toBe(['app/Service.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('drops a file whose content came back to its baseline across the prefix boundary', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
$monorepo['repo']->commit('Change the service');
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 1;\n");
|
||||
$monorepo['repo']->commit('Put the service back');
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))->toBe([]);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('honours a gitignore inside the project', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['project'].'/.gitignore', "/ignored.php\n");
|
||||
file_put_contents($monorepo['project'].'/ignored.php', "<?php\n");
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->since($monorepo['sha']))
|
||||
->toContain('app/Service.php')
|
||||
->not->toContain('ignored.php');
|
||||
})->skipOnWindows();
|
||||
|
||||
it('leaves a project at the repository root untouched', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
expect(new ChangedFiles($monorepo['root'])->since($monorepo['sha']))
|
||||
->toBe(['frontend/widget.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('reports the changes it left outside the project', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
$changedFiles = new ChangedFiles($monorepo['project']);
|
||||
$changedFiles->since($monorepo['sha']);
|
||||
|
||||
expect($changedFiles->outsideProject())->toBe(['frontend/widget.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('reports nothing outside the project for a project at the repository root', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
|
||||
$changedFiles = new ChangedFiles($monorepo['root']);
|
||||
$changedFiles->since($monorepo['sha']);
|
||||
|
||||
expect($changedFiles->outsideProject())->toBeEmpty();
|
||||
})->skipOnWindows();
|
||||
|
||||
it('ties a fingerprint to the project location inside the repository', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
mkdir($monorepo['root'].'/admin/app', 0755, true);
|
||||
file_put_contents($monorepo['root'].'/admin/app/Service.php', "<?php\n\$service = 1;\n");
|
||||
$monorepo['repo']->commit('add a second project');
|
||||
|
||||
$nested = Fingerprint::compute($monorepo['project']);
|
||||
$sibling = Fingerprint::compute($monorepo['root'].'/admin');
|
||||
$root = Fingerprint::compute($monorepo['root']);
|
||||
|
||||
expect($nested['structural']['project_prefix'])->toBe('backend/')
|
||||
->and($sibling['structural']['project_prefix'])->toBe('admin/')
|
||||
->and($root['structural'])->not->toHaveKey('project_prefix')
|
||||
->and(Fingerprint::structuralMatches($nested, $sibling))->toBeFalse()
|
||||
->and(Fingerprint::structuralMatches($nested, $root))->toBeFalse()
|
||||
->and(Fingerprint::structuralDrift($sibling, $nested))->toContain('project_prefix');
|
||||
})->skipOnWindows();
|
||||
|
||||
it('sees a committed non-ascii path that git would otherwise quote', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
mkdir($monorepo['project'].'/resources/views', 0755, true);
|
||||
file_put_contents($monorepo['project'].'/resources/views/\u{26a1}orders.blade.php', "<div>a</div>\n");
|
||||
$monorepo['repo']->commit('add a lightning view');
|
||||
|
||||
$sha = $monorepo['repo']->sha();
|
||||
|
||||
file_put_contents($monorepo['project'].'/resources/views/\u{26a1}orders.blade.php', "<div>b</div>\n");
|
||||
$monorepo['repo']->commit('reword the lightning view');
|
||||
|
||||
$changedFiles = new ChangedFiles($monorepo['project']);
|
||||
|
||||
expect($changedFiles->since($sha))->toBe(['resources/views/\u{26a1}orders.blade.php'])
|
||||
->and($changedFiles->outsideProject())->toBeEmpty();
|
||||
})->skipOnWindows();
|
||||
|
||||
it('sees a committed non-ascii path at the repository root too', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['root'].'/frontend/\u{26a1}widget.php', "<?php\n\$widget = 1;\n");
|
||||
$monorepo['repo']->commit('add a lightning widget');
|
||||
|
||||
$sha = $monorepo['repo']->sha();
|
||||
|
||||
file_put_contents($monorepo['root'].'/frontend/\u{26a1}widget.php', "<?php\n\$widget = 2;\n");
|
||||
$monorepo['repo']->commit('rework the lightning widget');
|
||||
|
||||
expect(new ChangedFiles($monorepo['root'])->since($sha))
|
||||
->toBe(['frontend/\u{26a1}widget.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('keeps a tracked outside change and drops an untracked ignored one', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['root'].'/.gitignore', "dist/\n");
|
||||
mkdir($monorepo['root'].'/frontend/dist', 0755, true);
|
||||
file_put_contents($monorepo['root'].'/frontend/dist/bundle.js', "var a = 1;\n");
|
||||
$monorepo['repo']->run(['add', '-f', 'frontend/dist/bundle.js']);
|
||||
$monorepo['repo']->commit('track a built bundle that the ignore rules match');
|
||||
|
||||
$sha = $monorepo['repo']->sha();
|
||||
|
||||
file_put_contents($monorepo['root'].'/frontend/dist/bundle.js', "var a = 2;\n");
|
||||
file_put_contents($monorepo['root'].'/frontend/dist/untracked.js', "var b = 1;\n");
|
||||
$monorepo['repo']->commit('rebuild the bundle');
|
||||
|
||||
$changedFiles = new ChangedFiles($monorepo['project']);
|
||||
$changedFiles->since($sha);
|
||||
|
||||
expect($changedFiles->outsideProject())->toBe(['frontend/dist/bundle.js']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('keeps a tracked project change that the ignore rules match', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
file_put_contents($monorepo['root'].'/.gitignore', "build/\n");
|
||||
mkdir($monorepo['project'].'/build', 0755, true);
|
||||
file_put_contents($monorepo['project'].'/build/Generated.php', "<?php\n\$built = 1;\n");
|
||||
$monorepo['repo']->run(['add', '-f', 'backend/build/Generated.php']);
|
||||
$monorepo['repo']->commit('track a generated file that the ignore rules match');
|
||||
|
||||
$sha = $monorepo['repo']->sha();
|
||||
|
||||
file_put_contents($monorepo['project'].'/build/Generated.php', "<?php\n\$built = 2;\n");
|
||||
file_put_contents($monorepo['project'].'/build/Untracked.php', "<?php\n");
|
||||
$monorepo['repo']->commit('regenerate the file');
|
||||
|
||||
expect(new ChangedFiles($monorepo['project'])->since($sha))
|
||||
->toBe(['build/Generated.php']);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('reads committed paths from the repository root even when git is told to be relative', function (): void {
|
||||
$monorepo = tiaMonorepoRepository();
|
||||
|
||||
$monorepo['repo']->run(['config', 'diff.relative', 'true']);
|
||||
|
||||
file_put_contents($monorepo['project'].'/app/Service.php', "<?php\n\$service = 2;\n");
|
||||
file_put_contents($monorepo['root'].'/frontend/widget.php', "<?php\n\$widget = 2;\n");
|
||||
$monorepo['repo']->commit('rework both sides');
|
||||
|
||||
$changedFiles = new ChangedFiles($monorepo['project']);
|
||||
|
||||
expect($changedFiles->since($monorepo['sha']))->toBe(['app/Service.php'])
|
||||
->and($changedFiles->outsideProject())->toBe(['frontend/widget.php']);
|
||||
})->skipOnWindows();
|
||||
@@ -9,6 +9,8 @@ use Pest\Plugins\Tia\FileState;
|
||||
use Pest\Plugins\Tia\Storage;
|
||||
use Pest\Support\Container;
|
||||
use Pest\TestSuite;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Tests\Fixtures\Tia\GitRepo;
|
||||
|
||||
afterEach(function (): void {
|
||||
Storage::useDirectory(null);
|
||||
@@ -37,3 +39,32 @@ it('uses an absolute configured directory', function (): void {
|
||||
|
||||
expect(Storage::tempDir('/project'))->toBe('/tmp/pest-tia');
|
||||
});
|
||||
|
||||
it('keeps the state directory of a project at the repository root', function (): void {
|
||||
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-tia-storage-'.bin2hex(random_bytes(8));
|
||||
|
||||
mkdir($root.'/backend', 0755, true);
|
||||
|
||||
$repo = new GitRepo($root);
|
||||
$repo->init('master');
|
||||
$repo->addOrigin('git@github.com:pestphp/tia-fixture.git');
|
||||
|
||||
$home = $root.DIRECTORY_SEPARATOR.'.home';
|
||||
mkdir($home, 0755, true);
|
||||
|
||||
$original = getenv('HOME');
|
||||
putenv('HOME='.$home);
|
||||
|
||||
try {
|
||||
$rootDir = Storage::tempDir($root);
|
||||
$nestedDir = Storage::tempDir($root.'/backend');
|
||||
} finally {
|
||||
putenv($original === false ? 'HOME' : 'HOME='.$original);
|
||||
new Process(['rm', '-rf', $root])->run();
|
||||
}
|
||||
|
||||
$identity = substr(hash('sha256', 'github.com/pestphp/tia-fixture'), 0, 16);
|
||||
|
||||
expect(basename($rootDir))->toEndWith('-'.$identity)
|
||||
->and(basename($nestedDir))->not->toEndWith('-'.$identity);
|
||||
})->skipOnWindows();
|
||||
|
||||
@@ -21,7 +21,7 @@ test('parallel', function () use ($run): void {
|
||||
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
|
||||
$profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile');
|
||||
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1792 passed (3849 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 30 skipped, 1810 passed (3880 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user