Skip to content

Commit 9d85c96

Browse files
authored
Cache unchanged files on dry run v2 (#4281)
Co-authored-by: Jiří Bok <jiri.bok@nice.com>
1 parent a808deb commit 9d85c96

File tree

10 files changed

+88
-5
lines changed

10 files changed

+88
-5
lines changed

.github/workflows/e2e_consecutive_changes.yaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ jobs:
2222
fail-fast: false
2323
matrix:
2424
php_version: ['8.1']
25+
rector_disable_parallel: ['true', 'false']
2526
directory:
2627
- 'e2e/consecutive-changes-with-cache'
2728

28-
name: End to end test - ${{ matrix.directory }}
29+
name: End to end test - ${{ matrix.directory }} [disableParallel=${{ matrix.rector_disable_parallel }}]
2930

3031
steps:
3132
- uses: actions/checkout@v3
@@ -43,10 +44,28 @@ jobs:
4344
run: composer install --ansi
4445
working-directory: ${{ matrix.directory }}
4546

47+
# run e2e test with dry run, we expect non-zero exit code
48+
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-1-dry-run.diff --dry-run
49+
working-directory: ${{ matrix.directory }}
50+
continue-on-error: true
51+
env:
52+
RECTOR_DISABLE_PARALLEL: ${{ matrix.rector_disable_parallel }}
53+
54+
# run e2e test with dry run once more, we expect non-zero exit code again
55+
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-2-dry-run.diff --dry-run
56+
working-directory: ${{ matrix.directory }}
57+
continue-on-error: true
58+
env:
59+
RECTOR_DISABLE_PARALLEL: ${{ matrix.rector_disable_parallel }}
60+
4661
# run e2e test
47-
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-1.diff
62+
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-3.diff
4863
working-directory: ${{ matrix.directory }}
64+
env:
65+
RECTOR_DISABLE_PARALLEL: ${{ matrix.rector_disable_parallel }}
4966

5067
# this tests that a 2nd run with cache and consecutive changes works, see https://github.com/rectorphp/rector-src/pull/3614#issuecomment-1507742338
51-
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-2.diff
68+
- run: php ../e2eTestChangingRunnerWithCache.php -o expected-output-4.diff
5269
working-directory: ${{ matrix.directory }}
70+
env:
71+
RECTOR_DISABLE_PARALLEL: ${{ matrix.rector_disable_parallel }}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1 file with changes
2+
===================
3+
4+
1) src/Source.php:1
5+
6+
---------- begin diff ----------
7+
@@ @@
8+
9+
$a = true;
10+
$b = true;
11+
-
12+
-if ($a && $b) {
13+
- return true;
14+
+if (!$a) {
15+
+ return;
16+
}
17+
+if (!$b) {
18+
+ return;
19+
+}
20+
+return true;
21+
----------- end diff -----------
22+
23+
Applied rules:
24+
* ChangeAndIfToEarlyReturnRector
25+
26+
27+
[OK] 1 file would have changed (dry-run) by Rector
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1 file with changes
2+
===================
3+
4+
1) src/Source.php:1
5+
6+
---------- begin diff ----------
7+
@@ @@
8+
9+
$a = true;
10+
$b = true;
11+
-
12+
-if ($a && $b) {
13+
- return true;
14+
+if (!$a) {
15+
+ return;
16+
}
17+
+if (!$b) {
18+
+ return;
19+
+}
20+
+return true;
21+
----------- end diff -----------
22+
23+
Applied rules:
24+
* ChangeAndIfToEarlyReturnRector
25+
26+
27+
[OK] 1 file would have changed (dry-run) by Rector

e2e/consecutive-changes-with-cache/expected-output-1.diff renamed to e2e/consecutive-changes-with-cache/expected-output-3.diff

File renamed without changes.

e2e/consecutive-changes-with-cache/expected-output-2.diff renamed to e2e/consecutive-changes-with-cache/expected-output-4.diff

File renamed without changes.

e2e/consecutive-changes-with-cache/rector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
return static function (RectorConfig $rectorConfig): void {
1111
$rectorConfig->cacheClass(FileCacheStorage::class);
1212

13+
if (getenv('RECTOR_DISABLE_PARALLEL') === 'true') {
14+
$rectorConfig->disableParallel();
15+
}
16+
1317
$rectorConfig->paths([
1418
__DIR__ . '/src',
1519
]);

e2e/e2eTestChangingRunnerWithCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
$expectedDiff = 'expected-output.diff';
2626
}
2727

28+
if (isset($argv[3]) && $argv[3] === '--dry-run') {
29+
$e2eCommand .= ' --dry-run';
30+
}
31+
2832
exec($e2eCommand, $output, $exitCode);
2933
$output = trim(implode("\n", $output));
3034
$output = str_replace(__DIR__, '.', $output);

packages/Parallel/WorkerRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
9595

9696
if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
9797
$this->invalidateFile($file);
98-
} elseif (! $configuration->isDryRun()) {
98+
} elseif (! $configuration->isDryRun() || $errorAndFileDiffs[Bridge::FILE_DIFFS] === []) {
9999
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
100100
}
101101
} catch (Throwable $throwable) {

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ parameters:
5656
- tests/DependencyInjection/config
5757

5858
ignoreErrors:
59+
- '#Cognitive complexity for "Rector\\Parallel\\WorkerRunner\:\:run\(\)" is 12, keep it under 11#'
60+
- '#Cognitive complexity for "Rector\\Core\\Application\\ApplicationFileProcessor\:\:processFiles\(\)" is 13, keep it under 11#'
5961
- '#Cognitive complexity for "Rector\\Php80\\NodeResolver\\SwitchExprsResolver\:\:resolve\(\)" is (.*?), keep it under 11#'
6062

6163
-

src/Application/ApplicationFileProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function processFiles(array $files, Configuration $configuration): array
133133

134134
if ($systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
135135
$this->changedFilesDetector->invalidateFile($file->getFilePath());
136-
} elseif (! $configuration->isDryRun()) {
136+
} elseif (! $configuration->isDryRun() || $systemErrorsAndFileDiffs[Bridge::FILE_DIFFS] === []) {
137137
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
138138
}
139139

0 commit comments

Comments
 (0)