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
6 changes: 3 additions & 3 deletions src/Commands/StaticSiteServe.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StaticSiteServe extends Command
/**
* The list of requests being handled and their start time.
*
* @var array<int, \Illuminate\Support\Carbon>
* @var array<int, Carbon>
*/
protected $requestsPool;

Expand Down Expand Up @@ -68,7 +68,7 @@ public function handle()
* Start a new server process.
*
* @param bool $hasEnvironment
* @return \Symfony\Component\Process\Process
* @return Process
*/
protected function startProcess()
{
Expand Down Expand Up @@ -208,7 +208,7 @@ protected function handleProcessOutput()
* Get the date from the given PHP server output.
*
* @param string $line
* @return \Illuminate\Support\Carbon
* @return Carbon
*/
protected function getDateFromLine($line)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ protected function write($request)
$html = $response->getContent();

if (! $this->files->exists($this->directory())) {
$this->files->makeDirectory($this->directory(), 0755, true);
// Force the creation, since when running with multiple workers,
// another worker may have created the directory in the meantime.
$this->files->makeDirectory($this->directory(), 0755, true, true);
}

$this->files->put($this->path(), $html);
Expand Down
30 changes: 30 additions & 0 deletions tests/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Entries\Entry;
use Statamic\StaticSite\Page;
Expand Down Expand Up @@ -45,6 +47,34 @@ public function it_gets_the_path_of_the_404_url()
$this->assertEquals('/path/to/static', $page->directory());
}

#[Test]
public function it_writes_when_another_worker_creates_the_directory_after_the_existence_check()
{
// Simulate the race between parallel workers, where the directory doesn't
// exist when checked but has appeared by the time mkdir runs.
$files = new class extends Filesystem
{
public function exists($path)
{
return false;
}
};

$destination = base_path('static');
$files->deleteDirectory($destination);
$files->makeDirectory($destination.'/foo/bar', 0755, true);

$entry = $this->mock(Entry::class);
$entry->shouldReceive('urlWithoutRedirect')->andReturn('/foo/bar');
$entry->shouldReceive('toResponse')->andReturn(new Response('html'));

$page = new Page($files, ['destination' => $destination], $entry);

$page->generate(Request::create('/foo/bar'));

$this->assertFileExists($destination.'/foo/bar/index.html');
}

private function page($entry, $config)
{
return new Page($this->mock(Filesystem::class), $config, $entry);
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Filesystem\Filesystem;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Statamic\Providers\StatamicServiceProvider;
use Statamic\Statamic;
use Wilderborn\Partyline\ServiceProvider;

class TestCase extends OrchestraTestCase
{
Expand All @@ -14,8 +16,8 @@ class TestCase extends OrchestraTestCase
protected function getPackageProviders($app)
{
return [
\Statamic\Providers\StatamicServiceProvider::class,
\Wilderborn\Partyline\ServiceProvider::class,
StatamicServiceProvider::class,
ServiceProvider::class,
\Statamic\StaticSite\ServiceProvider::class,
];
}
Expand Down