Skip to content

Commit

Permalink
always make paths absolute before processing files (#6293)
Browse files Browse the repository at this point in the history
* always make paths absolute before processing files

* Update src/FileSystem/FilesFinder.php

* Update src/FileSystem/FilePathHelper.php

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
Kanti and samsonasik committed Sep 8, 2024
1 parent a4545b6 commit 393423f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes = [], b

// filtering files in files collection
$filteredFilePaths = $this->fileAndDirectoryFilter->filterFiles($filesAndDirectories);
$filteredFilePaths = array_map(fn (string $filePath): string => realpath($filePath), $filteredFilePaths);
$filteredFilePaths = array_filter(
$filteredFilePaths,
fn (string $filePath): bool => ! $this->pathSkipper->shouldSkip($filePath)
Expand Down
20 changes: 20 additions & 0 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public function test(): void
$this->assertCount(0, $foundFiles);
}

#[DataProvider('alwaysReturnsAbsolutePathDataProvider')]
public function testAlwaysReturnsAbsolutePath(string $relativePath): void
{
$absolutePath = str_replace('/', DIRECTORY_SEPARATOR, getcwd() . '/' . $relativePath);
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$absolutePath], ['php']);
$this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if absolute is given');

$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$relativePath], ['php']);
$this->assertStringStartsWith($absolutePath, $foundFiles[0], 'should return absolute path if relative is given');
}

/**
* @return Iterator<array<string>>
*/
public static function alwaysReturnsAbsolutePathDataProvider(): Iterator
{
yield 'directory given' => ['tests/FileSystem/FilesFinder/Source/'];
yield 'file given' => ['tests/FileSystem/FilesFinder/Source/SomeFile.php'];
}

public function testWithFollowingBrokenSymlinks(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, [__DIR__ . '/../SourceWithBrokenSymlinks/folder1']);
Expand Down

0 comments on commit 393423f

Please sign in to comment.