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 +122,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 +143,21 @@ private function scopeFiles( } } + private function dumpFileWithPermissions( + string $inputFilePath, + string $inputContents, + string $outputFilePath + ): void { + $this->fileSystem->dumpFile($outputFilePath, $inputContents); + + $originalFilePermissions = fileperms($inputFilePath) & 0o777; + + if ($originalFilePermissions !== 420) { + // Only change the permissions if necessary + $this->fileSystem->chmod($outputFilePath, $originalFilePermissions); + } + } + /** * @return array{array, array} */ @@ -228,7 +244,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; }, diff --git a/tests/Console/Command/AddPrefixCommandTest.php b/tests/Console/Command/AddPrefixCommandTest.php index f9bc150c4..73aa2a025 100644 --- a/tests/Console/Command/AddPrefixCommandTest.php +++ b/tests/Console/Command/AddPrefixCommandTest.php @@ -105,6 +105,7 @@ public function test_scope_the_given_paths(): void $expectedFiles = [ 'composer/installed.json' => 'f1', + 'executable-file.php' => 'f5', 'file.php' => 'f2', 'invalid-file.php' => 'f3', 'scoper.inc.php' => 'f4', @@ -112,6 +113,13 @@ public function test_scope_the_given_paths(): void $root = realpath($root); + $this->fileSystemProphecy + ->chmod( + $this->tmp.'/executable-file.php', + 493, + ) + ->shouldBeCalled(); + foreach ($expectedFiles as $expectedFile => $prefixedContents) { $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); @@ -159,6 +167,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file(): void $expectedFiles = [ 'composer/installed.json' => 'f1', + 'executable-file.php' => 'f5', 'file.php' => 'f2', 'invalid-file.php' => 'f3', 'scoper.inc.php' => null, @@ -166,6 +175,13 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file(): void $root = realpath($root); + $this->fileSystemProphecy + ->chmod( + $this->tmp.'/executable-file.php', + 493, + ) + ->shouldBeCalled(); + foreach ($expectedFiles as $expectedFile => $prefixedContents) { $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); @@ -315,6 +331,7 @@ public function test_scope_the_current_working_directory_if_no_path_given(): voi $expectedFiles = [ 'composer/installed.json' => 'f1', + 'executable-file.php' => 'f5', 'file.php' => 'f2', 'invalid-file.php' => 'f3', 'scoper.inc.php' => 'f4', @@ -322,6 +339,13 @@ public function test_scope_the_current_working_directory_if_no_path_given(): voi $root = realpath($root); + $this->fileSystemProphecy + ->chmod( + $this->tmp.'/executable-file.php', + 493, + ) + ->shouldBeCalled(); + foreach ($expectedFiles as $expectedFile => $prefixedContents) { $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); @@ -365,6 +389,7 @@ public function test_an_output_directory_can_be_given(): void $expectedFiles = [ 'composer/installed.json' => 'f1', + 'executable-file.php' => 'f5', 'file.php' => 'f2', 'invalid-file.php' => 'f3', 'scoper.inc.php' => 'f4', @@ -372,6 +397,13 @@ public function test_an_output_directory_can_be_given(): void $root = realpath($root); + $this->fileSystemProphecy + ->chmod( + $outDir.'/executable-file.php', + 493, + ) + ->shouldBeCalled(); + foreach ($expectedFiles as $expectedFile => $prefixedContents) { $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($outDir.'/'.$expectedFile); @@ -416,6 +448,7 @@ public function test_relative_output_directory_are_made_absolute(): void $expectedFiles = [ 'composer/installed.json' => 'f1', + 'executable-file.php' => 'f5', 'file.php' => 'f2', 'invalid-file.php' => 'f3', 'scoper.inc.php' => 'f4', @@ -423,6 +456,13 @@ public function test_relative_output_directory_are_made_absolute(): void $root = realpath($root); + $this->fileSystemProphecy + ->chmod( + $this->tmp.DIRECTORY_SEPARATOR.$outDir.'/executable-file.php', + 493, + ) + ->shouldBeCalled(); + foreach ($expectedFiles as $expectedFile => $prefixedContents) { $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile); @@ -526,6 +566,7 @@ public function test_can_scope_projects_with_invalid_files(): void $this->fileSystemProphecy->mkdir($this->tmp)->shouldBeCalled(); $this->fileSystemProphecy->exists(Argument::cetera())->willReturn(false); $this->fileSystemProphecy->remove(Argument::cetera())->shouldNotBeCalled(); + $this->fileSystemProphecy->chmod(Argument::cetera())->shouldNotBeCalled(); $expectedFiles = [ 'invalid-json.json' => 'f1',