Skip to content

Commit 9fa959b

Browse files
authored
Fix windows CI error on ChangedFilesDetectorPortableCacheTest (#8537)
1 parent 8e989bc commit 9fa959b

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎src/Configuration/Parameter/SimpleParameterProvider.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Rector\Configuration\Option;
88
use Rector\Exception\ShouldNotHappenException;
9+
use Rector\Skipper\FileSystem\PathNormalizer;
910
use Webmozart\Assert\Assert;
1011

1112
/**
@@ -222,15 +223,23 @@ private static function relativizeProjectPaths(array $parameters, string $projec
222223
continue;
223224
}
224225

225-
if (is_string($value) && str_starts_with($value, $projectPathPrefix)) {
226-
$parameters[$key] = substr($value, strlen($projectPathPrefix));
226+
if (! is_string($value)) {
227+
continue;
228+
}
229+
230+
// compared with normalized separators: on Windows getcwd() reports backslashes,
231+
// while a config path built with "/" keeps them, although both name the same directory
232+
$normalizedValue = PathNormalizer::normalize($value);
233+
if (str_starts_with($normalizedValue, $projectPathPrefix)) {
234+
$parameters[$key] = substr($normalizedValue, strlen($projectPathPrefix));
227235
}
228236
}
229237

230238
return $parameters;
231239
}
232240

233241
/**
242+
* Normalized to "/" separators, so the prefix compares alike with config paths on every platform.
234243
* Empty when the working directory cannot be resolved, which makes the relativizing above a
235244
* no-op rather than a wrong answer.
236245
*/
@@ -241,7 +250,7 @@ private static function projectPathPrefix(): string
241250
return '';
242251
}
243252

244-
return rtrim($currentDirectory, '/') . '/';
253+
return rtrim(PathNormalizer::normalize($currentDirectory), '/') . '/';
245254
}
246255

247256
/**

‎tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,25 @@ public function testConfiguredPathsDoNotTieTheCacheToOneDirectory(): void
229229
);
230230
}
231231

232+
public function testConfiguredPathsWithEitherSeparatorHashAlike(): void
233+
{
234+
// on Windows, getcwd() reports backslashes while a path built with "/" in the config keeps
235+
// them; both name the same project, so the prefix must be stripped either way
236+
chdir($this->firstCheckoutDirectory);
237+
SimpleParameterProvider::setParameter(Option::PATHS, [$this->firstCheckoutDirectory . '/src']);
238+
$slashSeparatedHash = SimpleParameterProvider::hashForCacheInvalidation();
239+
240+
SimpleParameterProvider::setParameter(Option::PATHS, [
241+
str_replace('/', '\\', $this->firstCheckoutDirectory) . '\\src',
242+
]);
243+
244+
$this->assertSame(
245+
$slashSeparatedHash,
246+
SimpleParameterProvider::hashForCacheInvalidation(),
247+
'the same configured path hashed differently once spelled with backslashes'
248+
);
249+
}
250+
232251
private function createCheckout(string $directory): void
233252
{
234253
// identical contents in both checkouts, as two checkouts of one commit are

0 commit comments

Comments
 (0)