Skip to content

Commit 8de23f9

Browse files
authored
Preserve the files permissions (#753)
Closes #463
1 parent d2781db commit 8de23f9

File tree

5 files changed

+81
-12
lines changed

5 files changed

+81
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
declare(strict_types=1);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
declare (strict_types=1);

src/Console/ConsoleScoper.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function Humbug\PhpScoper\get_common_path;
3232
use function preg_match as native_preg_match;
3333
use function Safe\file_get_contents;
34+
use function Safe\fileperms;
3435
use function sprintf;
3536
use function str_replace;
3637
use function strlen;
@@ -99,7 +100,7 @@ private function scopeFiles(
99100
// Creates output directory if does not already exist
100101
$this->fileSystem->mkdir($outputDir);
101102

102-
[$files, $whitelistedFiles] = self::getFiles($config, $outputDir);
103+
[$files, $excludedFilesWithContents] = self::getFiles($config, $outputDir);
103104

104105
$logger->outputFileCount(count($files));
105106

@@ -121,14 +122,14 @@ private function scopeFiles(
121122
);
122123
}
123124

124-
foreach ($whitelistedFiles as [$inputFilePath, $inputContents, $outputFilePath]) {
125-
$this->fileSystem->dumpFile($outputFilePath, $inputContents);
125+
foreach ($excludedFilesWithContents as $excludedFileWithContent) {
126+
$this->dumpFileWithPermissions(...$excludedFileWithContent);
126127
}
127128

128129
$vendorDir = self::findVendorDir(
129130
[
130131
...array_column($files, 2),
131-
...array_column($whitelistedFiles, 2),
132+
...array_column($excludedFilesWithContents, 2),
132133
],
133134
);
134135

@@ -142,6 +143,21 @@ private function scopeFiles(
142143
}
143144
}
144145

146+
private function dumpFileWithPermissions(
147+
string $inputFilePath,
148+
string $inputContents,
149+
string $outputFilePath
150+
): void {
151+
$this->fileSystem->dumpFile($outputFilePath, $inputContents);
152+
153+
$originalFilePermissions = fileperms($inputFilePath) & 0o777;
154+
155+
if ($originalFilePermissions !== 420) {
156+
// Only change the permissions if necessary
157+
$this->fileSystem->chmod($outputFilePath, $originalFilePermissions);
158+
}
159+
}
160+
145161
/**
146162
* @return array{array<array{string, string, string}>, array<array{string, string, string}>}
147163
*/
@@ -228,7 +244,11 @@ private function scopeFile(
228244
$scoppedContent = file_get_contents($inputFilePath);
229245
}
230246

231-
$this->fileSystem->dumpFile($outputFilePath, $scoppedContent);
247+
$this->dumpFileWithPermissions(
248+
$inputFilePath,
249+
$scoppedContent,
250+
$outputFilePath,
251+
);
232252

233253
if (!isset($exception)) {
234254
$logger->outputSuccess($inputFilePath);

tests/Console/Command/AddPrefixCommandIntegrationTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function iterator_to_array;
2727
use function Safe\file_get_contents;
2828
use function Safe\file_put_contents;
29+
use function Safe\fileperms;
2930
use function Safe\preg_replace;
3031
use function Safe\realpath;
3132
use function sprintf;
@@ -36,7 +37,6 @@
3637
* @coversNothing
3738
*
3839
* @group integration
39-
* @runTestsInSeparateProcesses
4040
*
4141
* @internal
4242
*/
@@ -135,10 +135,10 @@ public function test_scope_in_normal_mode(): void
135135
136136
PhpScoper version TestVersion 28/01/2020
137137
138-
0/4 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
139-
4/4 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
138+
0/5 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
139+
5/5 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
140140
141-
[OK] Successfully prefixed 4 files.
141+
[OK] Successfully prefixed 5 files.
142142
143143
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
144144

@@ -196,12 +196,13 @@ public function test_scope_in_verbose_mode(): void
196196
PhpScoper version TestVersion 28/01/2020
197197
198198
* [NO] /path/to/composer/installed.json
199+
* [OK] /path/to/executable-file.php
199200
* [OK] /path/to/file.php
200201
* [NO] /path/to/invalid-file.php
201202
* [OK] /path/to/scoper.inc.php
202203
203204
204-
[OK] Successfully prefixed 4 files.
205+
[OK] Successfully prefixed 5 files.
205206
206207
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
207208

@@ -257,6 +258,7 @@ public function test_scope_in_very_verbose_mode(): void
257258
#7
258259
#8
259260
#9
261+
* [OK] /path/to/executable-file.php
260262
* [OK] /path/to/file.php
261263
* [NO] /path/to/invalid-file.php
262264
Could not parse the file "/path/to/invalid-file.php".: PhpParser
@@ -274,7 +276,7 @@ public function test_scope_in_very_verbose_mode(): void
274276
* [OK] /path/to/scoper.inc.php
275277
276278
277-
[OK] Successfully prefixed 4 files.
279+
[OK] Successfully prefixed 5 files.
278280
279281
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
280282

@@ -351,7 +353,7 @@ static function (array $collectedFiles, SplFileInfo $file) use ($dir): array {
351353

352354
$path = str_replace($dir, '', $realPath);
353355

354-
$collectedFiles[$path] = file_get_contents($realPath);
356+
$collectedFiles[$path] = [file_get_contents($realPath), fileperms($realPath)];
355357

356358
return $collectedFiles;
357359
},

tests/Console/Command/AddPrefixCommandTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,21 @@ public function test_scope_the_given_paths(): void
105105

106106
$expectedFiles = [
107107
'composer/installed.json' => 'f1',
108+
'executable-file.php' => 'f5',
108109
'file.php' => 'f2',
109110
'invalid-file.php' => 'f3',
110111
'scoper.inc.php' => 'f4',
111112
];
112113

113114
$root = realpath($root);
114115

116+
$this->fileSystemProphecy
117+
->chmod(
118+
$this->tmp.'/executable-file.php',
119+
493,
120+
)
121+
->shouldBeCalled();
122+
115123
foreach ($expectedFiles as $expectedFile => $prefixedContents) {
116124
$inputPath = escape_path($root.'/'.$expectedFile);
117125
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
@@ -159,13 +167,21 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file(): void
159167

160168
$expectedFiles = [
161169
'composer/installed.json' => 'f1',
170+
'executable-file.php' => 'f5',
162171
'file.php' => 'f2',
163172
'invalid-file.php' => 'f3',
164173
'scoper.inc.php' => null,
165174
];
166175

167176
$root = realpath($root);
168177

178+
$this->fileSystemProphecy
179+
->chmod(
180+
$this->tmp.'/executable-file.php',
181+
493,
182+
)
183+
->shouldBeCalled();
184+
169185
foreach ($expectedFiles as $expectedFile => $prefixedContents) {
170186
$inputPath = escape_path($root.'/'.$expectedFile);
171187
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
@@ -315,13 +331,21 @@ public function test_scope_the_current_working_directory_if_no_path_given(): voi
315331

316332
$expectedFiles = [
317333
'composer/installed.json' => 'f1',
334+
'executable-file.php' => 'f5',
318335
'file.php' => 'f2',
319336
'invalid-file.php' => 'f3',
320337
'scoper.inc.php' => 'f4',
321338
];
322339

323340
$root = realpath($root);
324341

342+
$this->fileSystemProphecy
343+
->chmod(
344+
$this->tmp.'/executable-file.php',
345+
493,
346+
)
347+
->shouldBeCalled();
348+
325349
foreach ($expectedFiles as $expectedFile => $prefixedContents) {
326350
$inputPath = escape_path($root.'/'.$expectedFile);
327351
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
@@ -365,13 +389,21 @@ public function test_an_output_directory_can_be_given(): void
365389

366390
$expectedFiles = [
367391
'composer/installed.json' => 'f1',
392+
'executable-file.php' => 'f5',
368393
'file.php' => 'f2',
369394
'invalid-file.php' => 'f3',
370395
'scoper.inc.php' => 'f4',
371396
];
372397

373398
$root = realpath($root);
374399

400+
$this->fileSystemProphecy
401+
->chmod(
402+
$outDir.'/executable-file.php',
403+
493,
404+
)
405+
->shouldBeCalled();
406+
375407
foreach ($expectedFiles as $expectedFile => $prefixedContents) {
376408
$inputPath = escape_path($root.'/'.$expectedFile);
377409
$outputPath = escape_path($outDir.'/'.$expectedFile);
@@ -416,13 +448,21 @@ public function test_relative_output_directory_are_made_absolute(): void
416448

417449
$expectedFiles = [
418450
'composer/installed.json' => 'f1',
451+
'executable-file.php' => 'f5',
419452
'file.php' => 'f2',
420453
'invalid-file.php' => 'f3',
421454
'scoper.inc.php' => 'f4',
422455
];
423456

424457
$root = realpath($root);
425458

459+
$this->fileSystemProphecy
460+
->chmod(
461+
$this->tmp.DIRECTORY_SEPARATOR.$outDir.'/executable-file.php',
462+
493,
463+
)
464+
->shouldBeCalled();
465+
426466
foreach ($expectedFiles as $expectedFile => $prefixedContents) {
427467
$inputPath = escape_path($root.'/'.$expectedFile);
428468
$outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);
@@ -526,6 +566,7 @@ public function test_can_scope_projects_with_invalid_files(): void
526566
$this->fileSystemProphecy->mkdir($this->tmp)->shouldBeCalled();
527567
$this->fileSystemProphecy->exists(Argument::cetera())->willReturn(false);
528568
$this->fileSystemProphecy->remove(Argument::cetera())->shouldNotBeCalled();
569+
$this->fileSystemProphecy->chmod(Argument::cetera())->shouldNotBeCalled();
529570

530571
$expectedFiles = [
531572
'invalid-json.json' => 'f1',

0 commit comments

Comments
 (0)