Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle symlinks properly #84

Merged
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
9 changes: 4 additions & 5 deletions src/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,17 @@ function (string $path): string {
);

foreach ($it as $file) {
if ($file->isDir() && $this->dirIsEmpty($file)) {
if ($file->isDir() && $this->dirIsEmpty((string) $file)) {
rmdir((string)$file);
}
}
}
}

// TODO: Use Symphony or Flysystem functions.
protected function dirIsEmpty($dir): bool
// TODO: Use Symfony or Flysystem functions.
protected function dirIsEmpty(string $dir): bool
{
$absolutePath = $dir;
$di = new RecursiveDirectoryIterator($absolutePath, \FilesystemIterator::SKIP_DOTS);
$di = new RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
return iterator_count($di) === 0;
}

Expand Down
7 changes: 3 additions & 4 deletions src/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public function compileFileList(): void
'targetRelativeFilepath' => $outputRelativeFilepath,
);
$this->filesWithDependencies[ $outputRelativeFilepath ] = $file;
continue;
} elseif (is_dir($sourceAbsolutePath)) {
// trailingslashit().
$namespace_relative_path = rtrim($namespace_relative_path, DIRECTORY_SEPARATOR)
Expand All @@ -166,7 +165,7 @@ public function compileFileList(): void

foreach ($finder as $foundFile) {
$sourceAbsoluteFilepath = $foundFile->getPathname();
$sourceRelativePath = str_replace($this->workingDir, '', $sourceAbsoluteFilepath);
$sourceRelativeFilePath = str_replace(rtrim($sourcePath, DIRECTORY_SEPARATOR), rtrim($sourceRelativePath, DIRECTORY_SEPARATOR), $sourceAbsoluteFilepath);
$outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath);

// For symlinked packages.
Expand All @@ -192,13 +191,13 @@ public function compileFileList(): void
continue;
}

if (!$this->filesystem->fileExists($sourceRelativePath)) {
if (!$this->filesystem->fileExists($sourceRelativeFilePath)) {
continue;
}

if ('<?php // This file was deleted by {@see https://github.com/BrianHenryIE/strauss}.'
===
$this->filesystem->read($sourceRelativePath)
$this->filesystem->read($sourceRelativeFilePath)
) {
continue;
}
Expand Down
127 changes: 127 additions & 0 deletions tests/Integration/CleanupSymlinkIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace BrianHenryIE\Strauss\Tests\Integration;

use BrianHenryIE\Strauss\Console\Commands\Compose;
use BrianHenryIE\Strauss\Tests\Integration\Util\IntegrationTestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class CleanupSymlinkIntegrationTest extends IntegrationTestCase
{
/**
* Test case that ensures a symlinked package is not removed or cleared out by the strauss command.
*/
public function testEnsureNoRemovalOfSymlinks(): void
{
$paths = [
$main_package_dir = $this->testsWorkingDir . '/main-package',
$symlinked_package_dir = $this->testsWorkingDir . '/symlinked-package',
];

$this->removePaths($paths);

mkdir($main_package_dir);
mkdir($symlinked_package_dir . '/src/', 0777, true);

file_put_contents($main_package_dir . '/composer.json', $this->packageComposerFile());
file_put_contents($symlinked_package_dir . '/composer.json', $this->symlinkedComposerFile());
file_put_contents($symlinked_package_dir . '/src/File.php', $this->symlinkedPhpFile());

chdir($main_package_dir);
exec('composer install');

$inputInterfaceMock = $this->createMock(InputInterface::class);
$outputInterfaceMock = $this->createMock(OutputInterface::class);

$relative_symlinked_package_dir = $main_package_dir . '/vendor/strauss-test/symlinked-package';
try {
self::assertDirectoryExists($relative_symlinked_package_dir);
$strauss = new Compose();

$strauss->run($inputInterfaceMock, $outputInterfaceMock);

self::assertDirectoryExists($symlinked_package_dir);
self::assertFileExists($symlinked_package_dir . '/composer.json');
self::assertFileExists($symlinked_package_dir . '/src/File.php');

self::assertDirectoryDoesNotExist($relative_symlinked_package_dir);
self::assertFileExists(
$file = $main_package_dir . '/vendor_prefixed/strauss-test/symlinked-package/src/File.php'
);
self::assertStringContainsString('Prefixed\\Internal\\Package', file_get_contents($file) ?: '');
} finally {
$this->removePaths($paths);
}
}

/**
* Clean up after the tests.
* @param string[] $paths
*/
private function removePaths(array $paths): void
{
foreach ($paths as $path) {
if (!is_dir($path)) {
continue;
}
exec("rm -rf " . $path);
}
}

private function packageComposerFile(): string
{
return <<<JSON
{
"repositories": [
{
"type": "path",
"url": "../symlinked-package",
"options": {
"symlink": true
}
}
],
"name": "strauss-test/main-package",
"require": {
"strauss-test/symlinked-package": "@dev"
},
"extra": {
"strauss": {
"target_directory": "vendor_prefixed",
"namespace_prefix": "Prefixed\\\\",
"classmap_prefix": "Prefixed_",
"delete_vendor_packages": true
}
}
}
JSON;
}

private function symlinkedComposerFile(): string
{
return <<<JSON
{
"name": "strauss-test/symlinked-package",
"autoload": {
"psr-4": {
"Internal\\\\Package\\\\": "src/"
}
}
}
JSON;
}

private function symlinkedPhpFile(): string
{
return <<<PHP
<?php

namespace Internal\Package;

final class File {
}

PHP;
}
}