From 8c6e0a0c637b340cabbb9628f0c59027627650b1 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Thu, 1 Feb 2024 13:35:25 +0100 Subject: [PATCH 1/3] Fix retrieval of relative path. --- src/Cleanup.php | 9 ++++----- src/FileEnumerator.php | 7 +++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Cleanup.php b/src/Cleanup.php index 1529f3b3..dae76f1a 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 3c9f8052..4ec8223f 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($sourcePath, $sourceRelativePath, $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; } From 5d13c778c0bf57e0e537d1f3ea2567fe044e25d3 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Thu, 1 Feb 2024 14:55:56 +0100 Subject: [PATCH 2/3] added integration test ensuring the existence of the orignal symlink --- .../CleanupSymlinkIntegrationTest.php | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tests/Integration/CleanupSymlinkIntegrationTest.php diff --git a/tests/Integration/CleanupSymlinkIntegrationTest.php b/tests/Integration/CleanupSymlinkIntegrationTest.php new file mode 100644 index 00000000..dcfc37da --- /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 << Date: Thu, 29 Feb 2024 11:09:02 +0100 Subject: [PATCH 3/3] Adjust for configured paths not suffixed by a DirectorySeparator --- src/FileEnumerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FileEnumerator.php b/src/FileEnumerator.php index 4ec8223f..518c305c 100644 --- a/src/FileEnumerator.php +++ b/src/FileEnumerator.php @@ -165,7 +165,7 @@ public function compileFileList(): void foreach ($finder as $foundFile) { $sourceAbsoluteFilepath = $foundFile->getPathname(); - $sourceRelativeFilePath = str_replace($sourcePath, $sourceRelativePath, $sourceAbsoluteFilepath); + $sourceRelativeFilePath = str_replace(rtrim($sourcePath, DIRECTORY_SEPARATOR), rtrim($sourceRelativePath, DIRECTORY_SEPARATOR), $sourceAbsoluteFilepath); $outputRelativeFilepath = str_replace($prefixToRemove, '', $sourceAbsoluteFilepath); // For symlinked packages.