Skip to content

Commit beefe46

Browse files
bug #40178 [FrameworkBundle] Exclude unreadable files when executing About command (michaljusiega)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Exclude unreadable files when executing About command | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony/symfony#39916 | License | MIT | Doc PR | ~ Fix as explained in: symfony/symfony#39916. Tests failed looks like not releated to my changes. Commits ------- 25f503a813 [FrameworkBundle] Exclude unreadable files when executing About command
2 parents a50452d + 0455ba9 commit beefe46

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

Command/AboutCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ private static function formatFileSize(string $path): string
116116
} else {
117117
$size = 0;
118118
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveDirectoryIterator::FOLLOW_SYMLINKS)) as $file) {
119-
$size += $file->getSize();
119+
if ($file->isReadable()) {
120+
$size += $file->getSize();
121+
}
120122
}
121123
}
122124

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command\AboutCommand;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\AboutCommand;
15+
use Symfony\Bundle\FrameworkBundle\Console\Application;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Command\AboutCommand\Fixture\TestAppKernel;
17+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
use Symfony\Component\Filesystem\Exception\IOException;
20+
use Symfony\Component\Filesystem\Filesystem;
21+
22+
class AboutCommandTest extends TestCase
23+
{
24+
/** @var Filesystem */
25+
private $fs;
26+
27+
protected function setUp(): void
28+
{
29+
$this->fs = new Filesystem();
30+
}
31+
32+
public function testAboutWithReadableFiles()
33+
{
34+
$kernel = new TestAppKernel('test', true);
35+
$this->fs->mkdir($kernel->getProjectDir());
36+
37+
$this->fs->dumpFile($kernel->getCacheDir().'/readable_file', 'The file content.');
38+
$this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);
39+
40+
$tester = $this->createCommandTester($kernel);
41+
$ret = $tester->execute([]);
42+
43+
$this->assertSame(0, $ret);
44+
$this->assertStringContainsString('Cache directory', $tester->getDisplay());
45+
$this->assertStringContainsString('Log directory', $tester->getDisplay());
46+
47+
$this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);
48+
49+
try {
50+
$this->fs->remove($kernel->getProjectDir());
51+
} catch (IOException $e) {
52+
}
53+
}
54+
55+
public function testAboutWithUnreadableFiles()
56+
{
57+
$kernel = new TestAppKernel('test', true);
58+
$this->fs->mkdir($kernel->getProjectDir());
59+
60+
// skip test on Windows; PHP can't easily set file as unreadable on Windows
61+
if ('\\' === \DIRECTORY_SEPARATOR) {
62+
$this->markTestSkipped('This test cannot run on Windows.');
63+
}
64+
65+
$this->fs->dumpFile($kernel->getCacheDir().'/unreadable_file', 'The file content.');
66+
$this->fs->chmod($kernel->getCacheDir().'/unreadable_file', 0222);
67+
68+
$tester = $this->createCommandTester($kernel);
69+
$ret = $tester->execute([]);
70+
71+
$this->assertSame(0, $ret);
72+
$this->assertStringContainsString('Cache directory', $tester->getDisplay());
73+
$this->assertStringContainsString('Log directory', $tester->getDisplay());
74+
75+
$this->fs->chmod($kernel->getCacheDir().'/unreadable_file', 0777);
76+
77+
try {
78+
$this->fs->remove($kernel->getProjectDir());
79+
} catch (IOException $e) {
80+
}
81+
}
82+
83+
private function createCommandTester(TestAppKernel $kernel): CommandTester
84+
{
85+
$application = new Application($kernel);
86+
$application->add(new AboutCommand());
87+
88+
return new CommandTester($application->find('about'));
89+
}
90+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command\AboutCommand\Fixture;
13+
14+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\HttpKernel\Kernel;
18+
19+
class TestAppKernel extends Kernel
20+
{
21+
public function registerBundles(): iterable
22+
{
23+
return [
24+
new FrameworkBundle(),
25+
];
26+
}
27+
28+
public function getProjectDir(): string
29+
{
30+
return __DIR__.'/test';
31+
}
32+
33+
public function registerContainerConfiguration(LoaderInterface $loader)
34+
{
35+
}
36+
37+
protected function build(ContainerBuilder $container)
38+
{
39+
}
40+
}

0 commit comments

Comments
 (0)