diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ae064fa..45943c96 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -77,7 +77,8 @@ jobs: # Compact prints a dot per passing test and the details of failures only. The runner's # terminal is 80 columns, which cuts the names of failed tests short, so widen it. + # Parallel runs a worker per CPU, each with its own storage directory (see tests/TestCase.php). - name: Execute tests - run: php artisan test --compact + run: php artisan test --compact --parallel env: COLUMNS: 200 diff --git a/tests/TestCase.php b/tests/TestCase.php index 54b4842d..ab6b6a3f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,8 @@ use App\Devnull\Account; use App\Devnull\Timezone; +use Illuminate\Filesystem\Filesystem; +use Illuminate\Foundation\Application; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Illuminate\Support\Facades\File; @@ -19,6 +21,30 @@ abstract class TestCase extends BaseTestCase */ use LazilyRefreshDatabase; + /** + * The storage directory this worker has emptied and set up, once it has. + */ + protected static ?string $workerStorage = null; + + /** + * Under `pest --parallel` each worker gets a storage directory of its own, + * storage/framework/testing/parallel/{token}, so one worker's tests can't + * delete the async spool, draft images or cached files another worker's + * test is still using. It's emptied when the worker starts. A serial run + * uses storage/ as it always has. + * + * The path is set before the app boots, so the config built from + * storage_path() (disks, logs, compiled views) follows it. + */ + public function createApplication(): Application + { + if ($storage = static::workerStorage()) { + $_ENV['LARAVEL_STORAGE_PATH'] = $storage; + } + + return parent::createApplication(); + } + /** * Background refreshes run inline, so a screen has its fresh answer by its * first frame. A test that wants to see the frame before it calls @@ -38,12 +64,47 @@ protected function setUp(): void $this->app->instance(Timezone::class, new Timezone(localtime: '/nonexistent')); } + /** + * The framework's teardown runs even when this cleanup throws. It rolls + * back the test's transaction, and without that every later test in the + * process fails on the one left open. + */ protected function tearDown(): void { - AsyncTask::clearFake(); - System::rememberPlatform(null); - File::deleteDirectory(AsyncTaskTransport::directory()); + try { + AsyncTask::clearFake(); + System::rememberPlatform(null); + File::deleteDirectory(AsyncTaskTransport::directory()); + } finally { + parent::tearDown(); + } + } + + /** + * This parallel worker's storage directory, or null outside a parallel run. + * It runs before the first app exists, so it can't use the File facade. + */ + protected static function workerStorage(): ?string + { + $token = $_SERVER['TEST_TOKEN'] ?? getenv('TEST_TOKEN'); + + if ($token === false || $token === '') { + return null; + } + + if (static::$workerStorage === null) { + $storage = Application::inferBasePath().'/storage/framework/testing/parallel/'.$token; + $files = new Filesystem; + + $files->deleteDirectory($storage); + + foreach (['app/private', 'app/public', 'framework/cache/data', 'framework/sessions', 'framework/views', 'logs'] as $directory) { + $files->ensureDirectoryExists($storage.'/'.$directory); + } + + static::$workerStorage = $storage; + } - parent::tearDown(); + return static::$workerStorage; } }