diff --git a/src/Cleanup.php b/src/Cleanup.php index 1529f3b..dae76f1 100644 --- a/src/Cleanup.php +++ b/src/Cleanup.php @@ -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; } diff --git a/src/FileEnumerator.php b/src/FileEnumerator.php index 3c9f805..518c305 100644 --- a/src/FileEnumerator.php +++ b/src/FileEnumerator.php @@ -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) @@ -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. @@ -192,13 +191,13 @@ public function compileFileList(): void continue; } - if (!$this->filesystem->fileExists($sourceRelativePath)) { + if (!$this->filesystem->fileExists($sourceRelativeFilePath)) { continue; } if ('filesystem->read($sourceRelativePath) + $this->filesystem->read($sourceRelativeFilePath) ) { continue; } diff --git a/tests/Integration/CleanupSymlinkIntegrationTest.php b/tests/Integration/CleanupSymlinkIntegrationTest.php new file mode 100644 index 0000000..dcfc37d --- /dev/null +++ b/tests/Integration/CleanupSymlinkIntegrationTest.php @@ -0,0 +1,127 @@ +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 <<