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
7 changes: 7 additions & 0 deletions src/Filesystem/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function create(string $path, int $mode = 0777): bool
public function remove(string $directory): bool
{
$directory = realpath($directory);

// Nothing to remove: the directory does not exist (any more) or is a file.
// Both would make the iterator below throw, which is fatal in a destructor.
if ($directory === false || !is_dir($directory)) {
return false;
}

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Command/Extension/UploadExtensionVersionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function transactionDirectoryIsRemovedAfterwards(): void
self::assertDirectoryDoesNotExist($this->workingDirectory . '/tailor-version-upload');
}

#[Test]
public function transactionDirectoryRemovalSurvivesAnAlreadyRemovedDirectory(): void
{
$command = $this->command();
$tester = $this->apiTester($command, self::jsonResponse([], 201));
$tester->execute($this->uploadArguments());

// Something removed the transaction directory before the destructor could
$transactionPath = $this->workingDirectory . '/tailor-version-upload';
foreach ((array)glob($transactionPath . '/*') as $file) {
unlink((string)$file);
}
rmdir($transactionPath);

unset($command, $tester);
gc_collect_cycles();

self::assertDirectoryDoesNotExist($transactionPath);
}

#[Test]
public function failingRequestReturnsFailure(): void
{
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/Filesystem/DirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 project - inspiring people to share!
* (c) 2020 Oliver Bartsch & Benni Mack
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace TYPO3\Tailor\Tests\Unit\Filesystem;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use TYPO3\Tailor\Filesystem\Directory;

class DirectoryTest extends TestCase
{
/** @var string */
private $temporaryDirectory = '';

protected function setUp(): void
{
$this->temporaryDirectory = sys_get_temp_dir() . '/tailor-test-' . bin2hex(random_bytes(8));
mkdir($this->temporaryDirectory, 0777, true);
}

protected function tearDown(): void
{
if (is_dir($this->temporaryDirectory)) {
(new Directory())->remove($this->temporaryDirectory);
}
}

#[Test]
public function directoryIsRemovedWithItsContent(): void
{
mkdir($this->temporaryDirectory . '/nested/deeper', 0777, true);
file_put_contents($this->temporaryDirectory . '/artefact.zip', 'content');
file_put_contents($this->temporaryDirectory . '/nested/deeper/file.txt', 'content');

self::assertTrue((new Directory())->remove($this->temporaryDirectory));
self::assertDirectoryDoesNotExist($this->temporaryDirectory);
}

#[Test]
public function removingANonExistingDirectoryReturnsFalse(): void
{
self::assertFalse((new Directory())->remove($this->temporaryDirectory . '/never-created'));
}

#[Test]
public function removingAFileReturnsFalse(): void
{
$file = $this->temporaryDirectory . '/artefact.zip';
file_put_contents($file, 'content');

self::assertFalse((new Directory())->remove($file));
self::assertFileExists($file);
}
}
Loading