diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 68b43a2e..ed6519a0 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -2368,6 +2368,8 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument 'package_json' => 'package.json', 'package_lock' => 'Node lockfile', 'js_config' => 'JS/TS config', + 'phpunit_bootstrap' => '--bootstrap', + 'phpunit_include_path' => '--include-path', 'pest_factory' => 'Pest internals', 'pest_method_factory' => 'Pest internals', 'project_prefix' => 'project location in the repository', diff --git a/src/Plugins/Tia/ConfigurationFile.php b/src/Plugins/Tia/ConfigurationFile.php index 310f711e..397a35d4 100644 --- a/src/Plugins/Tia/ConfigurationFile.php +++ b/src/Plugins/Tia/ConfigurationFile.php @@ -18,9 +18,16 @@ final readonly class ConfigurationFile private const array DEFAULT_NAMES = ['phpunit.xml', 'phpunit.dist.xml', 'phpunit.xml.dist']; + private const array VALUE_OPTIONS = ['--configuration', '--bootstrap', '--include-path']; + + /** + * @param list|null $includePaths + */ private function __construct( private ?string $path, private bool $suppressed, + private ?string $bootstrap = null, + private ?array $includePaths = null, ) {} /** @@ -34,18 +41,23 @@ final readonly class ConfigurationFile return self::projectDefault(); } + $bootstrap = $configuration->hasBootstrap() ? self::resolved($configuration->bootstrap()) : null; + $includePaths = $configuration->hasIncludePath() + ? array_map(self::resolved(...), explode(PATH_SEPARATOR, $configuration->includePath())) + : null; + if (! $configuration->hasConfigurationFile() && ! $configuration->useDefaultConfiguration()) { - return new self(null, true); + return new self(null, true, $bootstrap, $includePaths); } $file = new XmlConfigurationFileFinder()->find($configuration); $path = $file === false ? false : realpath($file); if ($path !== false && $generatedPath !== null && $path === realpath($generatedPath)) { - return self::projectDefault(); + return new self(null, false, $bootstrap, $includePaths); } - return new self($path === false ? null : $path, false); + return new self($path === false ? null : $path, false, $bootstrap, $includePaths); } public static function at(string $path): self @@ -58,6 +70,25 @@ final readonly class ConfigurationFile return new self(null, false); } + public function bootstrapFingerprint(string $projectRoot): ?string + { + return $this->bootstrap === null ? null : $this->relativeTo($projectRoot, $this->bootstrap); + } + + public function includePathFingerprint(string $projectRoot): ?string + { + if ($this->includePaths === null) { + return null; + } + + $paths = array_map( + fn (string $path): string => $this->relativeTo($projectRoot, $path), + $this->includePaths, + ); + + return implode("\0", $paths); + } + public function fingerprint(string $projectRoot): ?string { if ($this->suppressed) { @@ -88,10 +119,14 @@ final readonly class ConfigurationFile private function relativeTo(string $projectRoot, string $path): string { - $root = rtrim(str_replace('\\', '/', realpath($projectRoot) ?: $projectRoot), '/').'/'; - $file = str_replace('\\', '/', realpath($path) ?: $path); + $root = rtrim($this->withForwardSlashes(realpath($projectRoot) ?: $projectRoot), '/').'/'; + $resolvedPath = $this->withForwardSlashes(self::resolved($path)); - return str_starts_with($file, $root) ? substr($file, strlen($root)) : $file; + if ($resolvedPath.'/' === $root) { + return '.'; + } + + return str_starts_with($resolvedPath, $root) ? substr($resolvedPath, strlen($root)) : $resolvedPath; } /** @@ -106,20 +141,48 @@ final readonly class ConfigurationFile foreach ($arguments as $index => $argument) { if ($argument === '--no-configuration') { $parameters[] = $argument; - } elseif ($argument === '--configuration' || $argument === '-c') { - if (isset($arguments[$index + 1])) { - $parameters[] = '--configuration'; + + continue; + } + + $option = self::withLongConfigurationOption($argument); + + foreach (self::VALUE_OPTIONS as $name) { + if ($option === $name && isset($arguments[$index + 1])) { + $parameters[] = $name; $parameters[] = $arguments[$index + 1]; + } elseif (str_starts_with($option, $name.'=')) { + $parameters[] = $name; + $parameters[] = substr($option, strlen($name.'=')); } - } elseif (str_starts_with($argument, '--configuration=')) { - $parameters[] = '--configuration'; - $parameters[] = substr($argument, strlen('--configuration=')); - } elseif (str_starts_with($argument, '-c') && ! str_starts_with($argument, '--')) { - $parameters[] = '--configuration'; - $parameters[] = substr($argument, 2); } } return $parameters; } + + private static function withLongConfigurationOption(string $argument): string + { + if ($argument === '-c') { + return '--configuration'; + } + + if (str_starts_with($argument, '-c')) { + return '--configuration='.substr($argument, 2); + } + + return $argument; + } + + private function withForwardSlashes(string $path): string + { + return DIRECTORY_SEPARATOR === '\\' ? str_replace('\\', '/', $path) : $path; + } + + private static function resolved(string $path): string + { + $real = $path === '' ? false : realpath($path); + + return $real === false ? $path : $real; + } } diff --git a/src/Plugins/Tia/Fingerprint.php b/src/Plugins/Tia/Fingerprint.php index 89dc43da..236e2e06 100644 --- a/src/Plugins/Tia/Fingerprint.php +++ b/src/Plugins/Tia/Fingerprint.php @@ -31,13 +31,16 @@ final readonly class Fingerprint */ public static function compute(string $projectRoot, ?ConfigurationFile $configuration = null): array { + $configuration ??= ConfigurationFile::projectDefault(); + $structural = [ 'schema' => self::SCHEMA_VERSION, 'composer_lock' => self::composerLockHash($projectRoot), - 'phpunit_configuration' => ($configuration ?? ConfigurationFile::projectDefault())->fingerprint($projectRoot), + 'phpunit_configuration' => $configuration->fingerprint($projectRoot), 'vite_config' => self::viteConfigHash($projectRoot), 'package_lock' => self::packageLockHash($projectRoot), 'js_config' => self::jsConfigHash($projectRoot), + ...self::commandLineOverrides($projectRoot, $configuration), ]; $prefix = self::projectPrefix($projectRoot); @@ -55,6 +58,17 @@ final readonly class Fingerprint ]; } + /** + * @return array + */ + private static function commandLineOverrides(string $projectRoot, ConfigurationFile $configuration): array + { + return array_filter([ + 'phpunit_bootstrap' => $configuration->bootstrapFingerprint($projectRoot), + 'phpunit_include_path' => $configuration->includePathFingerprint($projectRoot), + ], fn (?string $value): bool => $value !== null); + } + /** * @param array $a * @param array $b diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 21ef7210..8a79c0a1 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1991,6 +1991,18 @@ ✓ it fingerprints the configuration Pest generates the way the Xdebug restarter does with dataset "fallback name" ✓ it fingerprints the configuration Pest generates the way the Xdebug restarter does with dataset "random name" ✓ it fingerprints an explicit configuration apart from the one Pest generates + ✓ it keeps the bootstrap and the include path the command line sets with the configuration Pest generates + ✓ it keeps the fingerprint of a run that sets no bootstrap and no include path + ✓ it fingerprints the bootstrap the command line sets relative to the project with dataset "separate value" + ✓ it fingerprints the bootstrap the command line sets relative to the project with dataset "equals form" + ✓ it tells two bootstraps apart + ✓ it tells a bootstrap with a backslash in its name apart from a bootstrap in a directory + ✓ it fingerprints each include path the command line sets relative to the project + ✓ it fingerprints the bootstrap of a run with --no-configuration + ✓ it fingerprints an include path equal to the project root as the project root + ✓ it keeps an empty include path entry empty + ✓ it keeps the fingerprint of a relative bootstrap when the working directory changes during the run + ✓ it tells an include path through a symlink to a directory with a separator in its name apart from two include paths PASS Tests\Unit\Plugins\Tia\ContentHash ✓ of() → it returns false when file does not exist @@ -2044,6 +2056,8 @@ ✓ compute() → it fingerprints tracked structural files in a regular clone ✓ compute() → it fingerprints tracked structural files inside a linked git worktree ✓ compute() → it excludes gitignored untracked files from the fingerprint + ✓ compute() → it keeps the command line bootstrap and include path out of a run that sets neither + ✓ compute() → it tells a bootstrap whose name looks like an include path apart from a bootstrap and an include path PASS Tests\Unit\Plugins\Tia\Graph ✓ shouldRerunStatus() → it re-runs failures and errors, replays successes @@ -2666,4 +2680,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, 39 skipped, 1955 passed (4433 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1969 passed (4458 assertions) \ No newline at end of file diff --git a/tests/Features/Tia/ActiveConfiguration.php b/tests/Features/Tia/ActiveConfiguration.php index ac30d975..f3413051 100644 --- a/tests/Features/Tia/ActiveConfiguration.php +++ b/tests/Features/Tia/ActiveConfiguration.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Pest\Support\Coverage; use Tests\Fixtures\Tia\Project; afterEach(function (): void { @@ -131,3 +132,71 @@ test('a leftover generated configuration file does not discard the graph', funct expect($replayed->exitCode)->toBe(0, $replayed->describe()) ->and($replayed->replayed())->toBe(Project::TOTAL_TESTS, $replayed->describe()); })->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +test('a graph recorded under one bootstrap is not replayed under the bootstrap the command line sets', function (array $arguments): void { + $project = Project::make('master'); + $project->write('tests/AlternateBootstrap.php', "git()->commit('add an alternate bootstrap'); + $project->seed('master'); + + $result = $project->pest('--tia', '--bootstrap', 'tests/AlternateBootstrap.php', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(0, $result->describe()) + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows(); + +test('a graph recorded under the bootstrap the command line sets is replayed under the same bootstrap', function (array $arguments): void { + $project = Project::make('master'); + $project->write('tests/AlternateBootstrap.php', "git()->commit('add an alternate bootstrap'); + + $project->pest('--tia', '--bootstrap', 'tests/AlternateBootstrap.php', ...$arguments); + + $result = $project->pest('--tia', '--bootstrap', 'tests/AlternateBootstrap.php', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe()); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available'); + +test('a graph recorded under a bootstrap whose name looks like an include path is not replayed under that bootstrap and include path', function (array $arguments): void { + $project = Project::make('master'); + $bootstrap = "write('tests/Boot.php', $bootstrap); + $project->write('tests/Boot.php;include-path=app', $bootstrap); + $project->git()->commit('add two bootstraps'); + + $project->pest('--tia', '--bootstrap', 'tests/Boot.php;include-path=app', ...$arguments); + + $result = $project->pest('--tia', '--bootstrap', 'tests/Boot.php', '--include-path', 'app'); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(0, $result->describe()) + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available'); + +test('a graph recorded under a bootstrap with a backslash in its name is not replayed under the bootstrap in a directory', function (array $arguments): void { + $project = Project::make('master'); + $project->write('tests/Boot.php', "write('tests\\Boot.php', "git()->commit('add two bootstraps'); + + $project->pest('--tia', '--bootstrap', 'tests\\Boot.php', ...$arguments); + + $result = $project->pest('--tia', '--bootstrap', 'tests/Boot.php', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(0, $result->describe()) + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->skipOnWindows()->skip(! Coverage::isAvailable(), 'Coverage is not available'); + +test('a graph recorded without an include path is not replayed under the include path the command line sets', function (): void { + $project = Project::make('master'); + $project->seed('master'); + + $result = $project->pest('--tia', '--include-path', 'app'); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->replayed())->toBe(0, $result->describe()) + ->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed'); +})->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/ConfigurationFile.php b/tests/Unit/Plugins/Tia/ConfigurationFile.php index e3ed2565..5d454202 100644 --- a/tests/Unit/Plugins/Tia/ConfigurationFile.php +++ b/tests/Unit/Plugins/Tia/ConfigurationFile.php @@ -7,7 +7,7 @@ use Pest\Plugins\Tia\ConfigurationFile; beforeEach(function (): void { $this->projectRoot = sys_get_temp_dir().'/pest-tia-configuration-'.bin2hex(random_bytes(4)); - mkdir($this->projectRoot, 0755, true); + mkdir($this->projectRoot.'/lib', 0755, true); $contents = "\n\n"; file_put_contents($this->projectRoot.'/phpunit.xml', $contents); @@ -21,6 +21,7 @@ afterEach(function (): void { } } + @rmdir($this->projectRoot.'/lib'); @rmdir($this->projectRoot); }); @@ -92,3 +93,125 @@ it('fingerprints an explicit configuration apart from the one Pest generates', f unlink($generatedPath); } }); + +it('keeps the bootstrap and the include path the command line sets with the configuration Pest generates', function (): void { + $generatedPath = $this->projectRoot.'/.pest.xml'; + file_put_contents($generatedPath, ''); + + $workingDirectory = (string) getcwd(); + chdir($this->projectRoot); + + try { + $configuration = ConfigurationFile::fromArguments(['--configuration', $generatedPath, '--bootstrap', 'phpunit.copy.xml', '--include-path', 'lib'], $generatedPath); + + expect($configuration->fingerprint($this->projectRoot))->toBe(ConfigurationFile::projectDefault()->fingerprint($this->projectRoot)) + ->and($configuration->bootstrapFingerprint($this->projectRoot))->toBe('phpunit.copy.xml') + ->and($configuration->includePathFingerprint($this->projectRoot))->toBe('lib'); + } finally { + chdir($workingDirectory); + unlink($generatedPath); + } +}); + +it('keeps the fingerprint of a run that sets no bootstrap and no include path', function (): void { + $configuration = ConfigurationFile::fromArguments(['--tia', '--parallel', '--configuration', $this->projectRoot.'/phpunit.xml']); + + expect($configuration->fingerprint($this->projectRoot)) + ->toBe(ConfigurationFile::at($this->projectRoot.'/phpunit.xml')->fingerprint($this->projectRoot)) + ->and($configuration->bootstrapFingerprint($this->projectRoot))->toBeNull() + ->and($configuration->includePathFingerprint($this->projectRoot))->toBeNull(); +}); + +it('fingerprints the bootstrap the command line sets relative to the project', function (array $bootstrap): void { + $configuration = ConfigurationFile::fromArguments(['--tia', '--configuration', $this->projectRoot.'/phpunit.xml', ...$bootstrap]); + + expect($configuration->bootstrapFingerprint($this->projectRoot))->toBe('phpunit.copy.xml') + ->and($configuration->fingerprint($this->projectRoot))->toBe(ConfigurationFile::at($this->projectRoot.'/phpunit.xml')->fingerprint($this->projectRoot)); +})->with([ + 'separate value' => fn (): array => ['--bootstrap', $this->projectRoot.'/phpunit.copy.xml'], + 'equals form' => fn (): array => ['--bootstrap='.$this->projectRoot.'/phpunit.copy.xml'], +]); + +it('tells two bootstraps apart', function (): void { + $configuration = ['--configuration', $this->projectRoot.'/phpunit.xml']; + + expect(ConfigurationFile::fromArguments([...$configuration, '--bootstrap', 'one.php'])->bootstrapFingerprint($this->projectRoot)) + ->not->toBe(ConfigurationFile::fromArguments([...$configuration, '--bootstrap', 'two.php'])->bootstrapFingerprint($this->projectRoot)); +}); + +it('tells a bootstrap with a backslash in its name apart from a bootstrap in a directory', function (): void { + $configuration = ['--configuration', $this->projectRoot.'/phpunit.xml']; + + expect(ConfigurationFile::fromArguments([...$configuration, '--bootstrap', 'tests\\Boot.php'])->bootstrapFingerprint($this->projectRoot)) + ->toBe('tests\\Boot.php') + ->not->toBe(ConfigurationFile::fromArguments([...$configuration, '--bootstrap', 'tests/Boot.php'])->bootstrapFingerprint($this->projectRoot)); +})->skipOnWindows(); + +it('fingerprints each include path the command line sets relative to the project', function (): void { + $includePath = $this->projectRoot.'/lib'.PATH_SEPARATOR.'/elsewhere'; + + expect(ConfigurationFile::fromArguments(['--configuration', $this->projectRoot.'/phpunit.xml', '--include-path', $includePath])->includePathFingerprint($this->projectRoot)) + ->toBe("lib\0/elsewhere"); +}); + +it('fingerprints the bootstrap of a run with --no-configuration', function (): void { + $configuration = ConfigurationFile::fromArguments(['--no-configuration', '--bootstrap', 'boot.php']); + + expect($configuration->fingerprint($this->projectRoot))->toBe('none') + ->and($configuration->bootstrapFingerprint($this->projectRoot))->toBe('boot.php'); +}); + +it('fingerprints an include path equal to the project root as the project root', function (): void { + $includePath = $this->projectRoot.PATH_SEPARATOR.$this->projectRoot.'/lib'; + + expect(ConfigurationFile::fromArguments(['--configuration', $this->projectRoot.'/phpunit.xml', '--include-path', $includePath])->includePathFingerprint($this->projectRoot)) + ->toBe(".\0lib"); +}); + +it('keeps an empty include path entry empty', function (): void { + expect(ConfigurationFile::fromArguments(['--configuration', $this->projectRoot.'/phpunit.xml', '--include-path', PATH_SEPARATOR.'lib'])->includePathFingerprint($this->projectRoot)) + ->toBe("\0lib"); +}); + +it('keeps the fingerprint of a relative bootstrap when the working directory changes during the run', function (): void { + $workingDirectory = (string) getcwd(); + chdir($this->projectRoot); + + try { + $configuration = ConfigurationFile::fromArguments(['--configuration', 'phpunit.xml', '--bootstrap', './phpunit.copy.xml', '--include-path', './lib']); + + chdir($this->projectRoot.'/lib'); + + expect($configuration->bootstrapFingerprint($this->projectRoot))->toBe('phpunit.copy.xml') + ->and($configuration->includePathFingerprint($this->projectRoot))->toBe('lib'); + } finally { + chdir($workingDirectory); + } +}); + +it('tells an include path through a symlink to a directory with a separator in its name apart from two include paths', function (): void { + $directories = ['lib-a', 'lib-b', 'lib-a'.PATH_SEPARATOR.'lib-b']; + + foreach ($directories as $directory) { + mkdir($this->projectRoot.'/'.$directory); + } + + symlink($this->projectRoot.'/lib-a'.PATH_SEPARATOR.'lib-b', $this->projectRoot.'/alias'); + + $workingDirectory = (string) getcwd(); + chdir($this->projectRoot); + + try { + $configuration = ['--configuration', 'phpunit.xml']; + + expect(ConfigurationFile::fromArguments([...$configuration, '--include-path', 'alias'])->includePathFingerprint($this->projectRoot)) + ->not->toBe(ConfigurationFile::fromArguments([...$configuration, '--include-path', 'lib-a'.PATH_SEPARATOR.'lib-b'])->includePathFingerprint($this->projectRoot)); + } finally { + chdir($workingDirectory); + unlink($this->projectRoot.'/alias'); + + foreach ($directories as $directory) { + rmdir($this->projectRoot.'/'.$directory); + } + } +})->skipOnWindows(); diff --git a/tests/Unit/Plugins/Tia/Fingerprint.php b/tests/Unit/Plugins/Tia/Fingerprint.php index 45b1fe77..4a66b9d3 100644 --- a/tests/Unit/Plugins/Tia/Fingerprint.php +++ b/tests/Unit/Plugins/Tia/Fingerprint.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Pest\Plugins\Tia\ConfigurationFile; use Pest\Plugins\Tia\Fingerprint; use Symfony\Component\Process\Process; @@ -106,4 +107,30 @@ describe('compute()', function (): void { fingerprintRemoveDirectory($clone); } }); + it('keeps the command line bootstrap and include path out of a run that sets neither', function (): void { + $root = fingerprintRepository(); + + try { + $structural = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--tia']))['structural']; + + expect($structural)->not->toHaveKey('phpunit_bootstrap') + ->and($structural)->not->toHaveKey('phpunit_include_path'); + } finally { + fingerprintRemoveDirectory($root); + } + }); + + it('tells a bootstrap whose name looks like an include path apart from a bootstrap and an include path', function (): void { + $root = fingerprintRepository(); + + try { + $bootstrapOnly = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--bootstrap', 'bootstrap.php;include-path=app'])); + $bootstrapAndIncludePath = Fingerprint::compute($root, ConfigurationFile::fromArguments(['--bootstrap', 'bootstrap.php', '--include-path', 'app'])); + + expect(Fingerprint::structuralMatches($bootstrapOnly, $bootstrapAndIncludePath))->toBeFalse() + ->and(Fingerprint::structuralDrift($bootstrapOnly, $bootstrapAndIncludePath))->toBe(['phpunit_bootstrap', 'phpunit_include_path']); + } finally { + fingerprintRemoveDirectory($root); + } + }); }); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 0c934df4..e522f00b 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -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, 31 skipped, 1828 passed (3908 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1842 passed (3933 assertions)'; expect($output) ->toContain("Tests: {$expected}")