Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Bootstrappers/BootFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pest\Contracts\Bootstrapper;
use Pest\Exceptions\FatalException;
use Pest\Plugins\Concerns\HandleArguments;
use Pest\Support\BootstrappedFiles;
use Pest\Support\DatasetInfo;
use Pest\Support\Str;
use Pest\TestSuite;
Expand Down Expand Up @@ -89,7 +90,13 @@ private function load(string $filename): void
if (! file_exists($filename)) {
return;
}

$before = get_included_files();
include_once $filename;

foreach (array_diff(get_included_files(), $before) as $included) {
BootstrappedFiles::add($included);
}
}

private function bootDatasets(string $testsPath): void
Expand Down
73 changes: 44 additions & 29 deletions src/Support/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,51 @@ final class Backtrace

public static function testFile(): string
{
$current = null;
return self::callSiteFile('Test file not found.');
}

public static function datasetsFile(): string
{
return self::callSiteFile('Dataset file not found.');
}

private static function callSiteFile(string $missingMessage): string
{
$fallback = null;

foreach (debug_backtrace(self::BACKTRACE_OPTIONS) as $trace) {
if (array_key_exists(self::FILE, $trace) === false) {
if (! isset($trace[self::FILE])) {
break;
}

$traceFile = str_replace(DIRECTORY_SEPARATOR, '/', $trace[self::FILE]);
$filename = $trace[self::FILE];
$traceFile = str_replace(DIRECTORY_SEPARATOR, '/', $filename);

if (
Str::endsWith($traceFile, 'overrides/Runner/TestSuiteLoader.php') ||
Str::endsWith($traceFile, 'src/Bootstrappers/BootFiles.php')
Str::endsWith($traceFile, 'overrides/Runner/TestSuiteLoader.php')
|| Str::endsWith($traceFile, 'src/Bootstrappers/BootFiles.php')
) {
break;
}

$current = $trace;
}

if ($current === null) {
throw ShouldNotHappen::fromMessage('Test file not found.');
}

return $current[self::FILE];
}

public static function datasetsFile(): string
{
$current = null;

foreach (debug_backtrace(self::BACKTRACE_OPTIONS) as $trace) {
assert(array_key_exists(self::FILE, $trace));
if (self::isFrameworkFile($traceFile)) {
continue;
}

$traceFile = str_replace(DIRECTORY_SEPARATOR, '/', $trace[self::FILE]);
if (BootstrappedFiles::contains($filename)) {
$fallback ??= $filename;

if (Str::endsWith($traceFile, 'Bootstrappers/BootFiles.php') || Str::endsWith($traceFile, 'overrides/Runner/TestSuiteLoader.php')) {
break;
continue;
}

$current = $trace;
return $filename;
}

if ($current === null) {
throw ShouldNotHappen::fromMessage('Dataset file not found.');
if (is_string($fallback)) {
return $fallback;
}

return $current[self::FILE];
throw ShouldNotHappen::fromMessage($missingMessage);
}

public static function file(): string
Expand Down Expand Up @@ -99,7 +97,9 @@ private static function backtrace(): array
continue;
}

if (str_starts_with($trace['file'], dirname(__DIR__).DIRECTORY_SEPARATOR)) {
$traceFile = str_replace(DIRECTORY_SEPARATOR, '/', $trace['file']);

if (self::isFrameworkFile($traceFile)) {
continue;
}

Expand All @@ -108,4 +108,19 @@ private static function backtrace(): array

throw ShouldNotHappen::fromMessage('Backtrace not found.');
}

private static function isFrameworkFile(string $traceFile): bool
{
$packageRoot = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__DIR__, 2));

if (
str_starts_with($traceFile, $packageRoot.'/src/')
|| str_starts_with($traceFile, $packageRoot.'/overrides/')
) {
return true;
}

return str_contains($traceFile, '/pestphp/pest/src/')
|| str_contains($traceFile, '/pestphp/pest/overrides/');
}
}
33 changes: 33 additions & 0 deletions src/Support/BootstrappedFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Pest\Support;

/**
* @internal
*/
final class BootstrappedFiles
{
/**
* @var array<string, true>
*/
private static array $files = [];

public static function add(string $filename): void
{
self::$files[self::key($filename)] = true;
}

public static function contains(string $filename): bool
{
return isset(self::$files[self::key($filename)]);
}

private static function key(string $filename): string
{
$real = realpath($filename);

return str_replace('\\', '/', $real !== false ? $real : $filename);
}
}
Loading