Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 65 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
}
}