Skip to content

Commit a8d8435

Browse files
authored
[dx] Set Option::SOURCE parameter from the resolved paths (#8424)
1 parent 854635f commit a8d8435

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎src/Configuration/ConfigurationFactory.php‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,19 @@ private function resolvePaths(InputInterface $input): array
209209
if ($commandLinePaths !== []) {
210210
// mark the run as narrowed, so unused skip reporting can be disabled to avoid false positives
211211
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, true);
212-
$this->setFilesWithoutExtensionParameter($commandLinePaths);
213-
return $commandLinePaths;
212+
$paths = $commandLinePaths;
213+
} else {
214+
// fallback to parameter
215+
$paths = SimpleParameterProvider::provideArrayParameter(Option::PATHS);
214216
}
215217

216-
// fallback to parameter
217-
$configPaths = SimpleParameterProvider::provideArrayParameter(Option::PATHS);
218-
$this->setFilesWithoutExtensionParameter($configPaths);
218+
$this->setFilesWithoutExtensionParameter($paths);
219+
220+
// extensions read the processed paths from here; without this only the test harness
221+
// sets it, so the parameter is empty in a real run
222+
SimpleParameterProvider::setParameter(Option::SOURCE, $paths);
219223

220-
return $configPaths;
224+
return $paths;
221225
}
222226

223227
/**

‎src/Configuration/Parameter/SimpleParameterProvider.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class SimpleParameterProvider
2121
* @var array<Option::*>
2222
*/
2323
private const array CACHE_IGNORED_PARAMETER_NAMES = [
24+
Option::SOURCE,
2425
Option::PARALLEL,
2526
Option::PARALLEL_JOB_SIZE,
2627
Option::PARALLEL_MAX_NUMBER_OF_PROCESSES,

‎tests/Configuration/ConfigurationFactoryTest.php‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
namespace Rector\Tests\Configuration;
66

77
use Rector\Configuration\ConfigurationFactory;
8+
use Rector\Configuration\Option;
9+
use Rector\Configuration\Parameter\SimpleParameterProvider;
10+
use Rector\Console\ProcessConfigureDecorator;
811
use Rector\FileSystem\FilesFinder;
912
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Input\ArrayInput;
1015

1116
final class ConfigurationFactoryTest extends AbstractLazyTestCase
1217
{
@@ -42,4 +47,55 @@ public function test(): void
4247
realpath($thirdFilePath),
4348
);
4449
}
50+
51+
public function testPublishesCommandLinePathsAsSourceParameter(): void
52+
{
53+
SimpleParameterProvider::setParameter(Option::PATHS, [__DIR__ . '/config']);
54+
55+
$this->make(ConfigurationFactory::class)
56+
->createFromInput($this->createInput([__DIR__ . '/Source']));
57+
58+
$this->assertSame(
59+
[__DIR__ . '/Source'],
60+
SimpleParameterProvider::provideArrayParameter(Option::SOURCE)
61+
);
62+
}
63+
64+
public function testPublishesConfiguredPathsAsSourceParameterWhenNoneGivenOnTheCommandLine(): void
65+
{
66+
SimpleParameterProvider::setParameter(Option::PATHS, [__DIR__ . '/config']);
67+
68+
$this->make(ConfigurationFactory::class)
69+
->createFromInput($this->createInput([]));
70+
71+
$this->assertSame(
72+
[__DIR__ . '/config'],
73+
SimpleParameterProvider::provideArrayParameter(Option::SOURCE)
74+
);
75+
}
76+
77+
public function testSourceParameterIsOutsideTheCacheInvalidationHash(): void
78+
{
79+
// The processed paths change per invocation. Were they hashed, running Rector on a
80+
// single file and then on the whole project would drop the cache each time.
81+
SimpleParameterProvider::setParameter(Option::SOURCE, ['src']);
82+
$hashForOnePath = SimpleParameterProvider::hashForCacheInvalidation();
83+
84+
SimpleParameterProvider::setParameter(Option::SOURCE, ['tests', 'src/Some/Other.php']);
85+
86+
$this->assertSame($hashForOnePath, SimpleParameterProvider::hashForCacheInvalidation());
87+
}
88+
89+
/**
90+
* @param string[] $paths
91+
*/
92+
private function createInput(array $paths): ArrayInput
93+
{
94+
$command = new Command('process');
95+
ProcessConfigureDecorator::decorate($command);
96+
97+
return new ArrayInput([
98+
Option::SOURCE => $paths,
99+
], $command->getDefinition());
100+
}
45101
}

0 commit comments

Comments
 (0)