Skip to content

Commit bd40727

Browse files
authored
Merge pull request brefphp#43 from vhenzl/pr/fix-42
Make sure `/tmp/log` dir exists
2 parents 6156395 + cf1e5ba commit bd40727

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/BrefKernel.php

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function handle($request, $type = HttpKernelInterface::MASTER_REQUEST, $c
6262
public function boot()
6363
{
6464
$this->prepareCacheDir(parent::getCacheDir(), $this->getCacheDir());
65+
$this->ensureLogDir($this->getLogDir());
6566

6667
parent::boot();
6768
}
@@ -122,6 +123,22 @@ protected function prepareCacheDir(string $readOnlyDir, string $writeDir): void
122123
));
123124
}
124125

126+
/**
127+
* Even though applications should never write into it on Lambda, there are parts of Symfony
128+
* (like "about" CLI command) that expect the log dir exists, so we have to make sure of it.
129+
*
130+
* @see https://github.com/brefphp/symfony-bridge/issues/42
131+
*/
132+
private function ensureLogDir(string $writeLogDir): void
133+
{
134+
if (! $this->isLambda() || is_dir($writeLogDir)) {
135+
return;
136+
}
137+
138+
$filesystem = new Filesystem;
139+
$filesystem->mkdir($writeLogDir);
140+
}
141+
125142
/**
126143
* This method logs to stderr.
127144
*

tests/Functional/FunctionalTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class FunctionalTest extends TestCase
1414
* simulate a persistent `/tmp` directory, just like inside AWS Lambda.
1515
* This will also let us check the content of that directory.
1616
*/
17-
private const LOCAL_TMP_DIRECTORY = __DIR__ . '/App/tmp';
17+
protected const LOCAL_TMP_DIRECTORY = __DIR__ . '/App/tmp';
1818

1919
public function setUp(): void
2020
{

tests/Functional/LogDirTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\SymfonyBridge\Test\Functional;
4+
5+
use Symfony\Component\Filesystem\Filesystem;
6+
7+
/**
8+
* Test /tmp/log is created
9+
*/
10+
class LogDirTest extends FunctionalTest
11+
{
12+
public function setUp(): void
13+
{
14+
parent::setUp();
15+
$this->composerInstall();
16+
$this->clearTempLog();
17+
}
18+
19+
private function clearTempLog(): void
20+
{
21+
(new Filesystem)->remove(self::LOCAL_TMP_DIRECTORY . '/log');
22+
}
23+
24+
public function test Symfony works(): void
25+
{
26+
$this->assertCommandIsSuccessful($this->runHttpRequest());
27+
}
28+
29+
public function test log dir is created in tmp on http request(): void
30+
{
31+
$this->assertDirectoryNotExists(self::LOCAL_TMP_DIRECTORY . '/log');
32+
33+
$this->runHttpRequest();
34+
$this->assertDirectoryExists(self::LOCAL_TMP_DIRECTORY . '/log');
35+
}
36+
37+
public function test log dir is created in tmp on console command(): void
38+
{
39+
$this->assertDirectoryNotExists(self::LOCAL_TMP_DIRECTORY . '/log');
40+
41+
$this->runSymfonyConsole('about');
42+
$this->assertDirectoryExists(self::LOCAL_TMP_DIRECTORY . '/log');
43+
}
44+
}

0 commit comments

Comments
 (0)