From 2eac1ba12d0a3a4b610bb2fb661225233d8eda8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sat, 12 Nov 2022 10:14:52 +0100 Subject: [PATCH] Preserve the files permissions Closes #463 --- fixtures/set002/original/executable-file.php | 3 +++ fixtures/set002/scoped/executable-file.php | 3 +++ src/Console/ConsoleScoper.php | 27 +++++++++++++++---- .../AddPrefixCommandIntegrationTest.php | 16 ++++++----- 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100755 fixtures/set002/original/executable-file.php create mode 100755 fixtures/set002/scoped/executable-file.php diff --git a/fixtures/set002/original/executable-file.php b/fixtures/set002/original/executable-file.php new file mode 100755 index 000000000..174d7fd70 --- /dev/null +++ b/fixtures/set002/original/executable-file.php @@ -0,0 +1,3 @@ +fileSystem->mkdir($outputDir); - [$files, $whitelistedFiles] = self::getFiles($config, $outputDir); + [$files, $excludedFilesWithContents] = self::getFiles($config, $outputDir); $logger->outputFileCount(count($files)); @@ -121,14 +123,14 @@ private function scopeFiles( ); } - foreach ($whitelistedFiles as [$inputFilePath, $inputContents, $outputFilePath]) { - $this->fileSystem->dumpFile($outputFilePath, $inputContents); + foreach ($excludedFilesWithContents as $excludedFileWithContent) { + $this->dumpFileWithPermissions(...$excludedFileWithContent); } $vendorDir = self::findVendorDir( [ ...array_column($files, 2), - ...array_column($whitelistedFiles, 2), + ...array_column($excludedFilesWithContents, 2), ], ); @@ -142,6 +144,17 @@ private function scopeFiles( } } + private function dumpFileWithPermissions( + string $inputFilePath, + string $inputContents, + string $outputFilePath + ): void { + $this->fileSystem->dumpFile($outputFilePath, $inputContents); + + $originalFilePermissions = fileperms($inputFilePath) & 0o777; + chmod($outputFilePath, $originalFilePermissions); + } + /** * @return array{array, array} */ @@ -228,7 +241,11 @@ private function scopeFile( $scoppedContent = file_get_contents($inputFilePath); } - $this->fileSystem->dumpFile($outputFilePath, $scoppedContent); + $this->dumpFileWithPermissions( + $inputFilePath, + $scoppedContent, + $outputFilePath, + ); if (!isset($exception)) { $logger->outputSuccess($inputFilePath); diff --git a/tests/Console/Command/AddPrefixCommandIntegrationTest.php b/tests/Console/Command/AddPrefixCommandIntegrationTest.php index ddc10e467..92bcd3da2 100644 --- a/tests/Console/Command/AddPrefixCommandIntegrationTest.php +++ b/tests/Console/Command/AddPrefixCommandIntegrationTest.php @@ -26,6 +26,7 @@ use function iterator_to_array; use function Safe\file_get_contents; use function Safe\file_put_contents; +use function Safe\fileperms; use function Safe\preg_replace; use function Safe\realpath; use function sprintf; @@ -36,7 +37,6 @@ * @coversNothing * * @group integration - * @runTestsInSeparateProcesses * * @internal */ @@ -135,10 +135,10 @@ public function test_scope_in_normal_mode(): void PhpScoper version TestVersion 28/01/2020 - 0/4 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% - 4/4 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% + 0/5 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% + 5/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% - [OK] Successfully prefixed 4 files. + [OK] Successfully prefixed 5 files. // Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s @@ -196,12 +196,13 @@ public function test_scope_in_verbose_mode(): void PhpScoper version TestVersion 28/01/2020 * [NO] /path/to/composer/installed.json + * [OK] /path/to/executable-file.php * [OK] /path/to/file.php * [NO] /path/to/invalid-file.php * [OK] /path/to/scoper.inc.php - [OK] Successfully prefixed 4 files. + [OK] Successfully prefixed 5 files. // Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s @@ -257,6 +258,7 @@ public function test_scope_in_very_verbose_mode(): void #7 #8 #9 + * [OK] /path/to/executable-file.php * [OK] /path/to/file.php * [NO] /path/to/invalid-file.php Could not parse the file "/path/to/invalid-file.php".: PhpParser @@ -274,7 +276,7 @@ public function test_scope_in_very_verbose_mode(): void * [OK] /path/to/scoper.inc.php - [OK] Successfully prefixed 4 files. + [OK] Successfully prefixed 5 files. // Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s @@ -351,7 +353,7 @@ static function (array $collectedFiles, SplFileInfo $file) use ($dir): array { $path = str_replace($dir, '', $realPath); - $collectedFiles[$path] = file_get_contents($realPath); + $collectedFiles[$path] = [file_get_contents($realPath), fileperms($realPath)]; return $collectedFiles; },