Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 58cc28c

Browse files
authored
Merge pull request #76 from philomather/master
Fix handling of windows filepaths
2 parents 379b528 + dc73606 commit 58cc28c

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/Service/FileGroupFinder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static function findLockFiles(Finder $finder, array $dependencyFileForma
8181
*
8282
* @return FileGroup[]
8383
*/
84-
private static function createFileGroups(array $lockFiles, Finder $finder, array $dependencyFileFormats, string $searchDirectory): array
84+
public static function createFileGroups(array $lockFiles, Finder $finder, array $dependencyFileFormats, string $searchDirectory): array
8585
{
8686
$fileGroups = [];
8787
foreach ($finder as $file) {
@@ -92,7 +92,10 @@ private static function createFileGroups(array $lockFiles, Finder $finder, array
9292
foreach ($lockFileRegexes as $lockFileRegex) {
9393
foreach ($lockFiles as $key => $lockFile) {
9494
$quotedLockfilePath = \preg_quote($file->getPath(), '/');
95-
if (\preg_match("/$quotedLockfilePath\/$lockFileRegex/", $key) === 1) {
95+
if (
96+
\preg_match("/{$quotedLockfilePath}\/{$lockFileRegex}/", $key) === 1 ||
97+
\preg_match("/{$quotedLockfilePath}\\\\{$lockFileRegex}/", $key) === 1
98+
) {
9699
$fileGroup->addLockFile($lockFile);
97100
unset($lockFiles[$key]);
98101
break;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Tests\Service;
4+
5+
use App\Model\DependencyFileFormat;
6+
use App\Service\FileGroupFinder;
7+
use App\Tests\Command\FindAndUploadFilesCommandTest;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Finder\Finder;
10+
use Symfony\Component\Finder\SplFileInfo;
11+
12+
class CreateFileGroupsTest extends TestCase
13+
{
14+
public function testCreateFileGroups(): void
15+
{
16+
$iterator = [
17+
new SplFileInfo('package.json', 'app', 'app/package.json'),
18+
new SplFileInfo('package-lock.json', 'app', 'app/package-lock.json'),
19+
];
20+
$iterator = new \ArrayIterator($iterator);
21+
22+
$finderMock = $this->getMockBuilder(Finder::class)->disableOriginalConstructor()->getMock();
23+
$finderMock->expects($this->once())->method('getIterator')->willReturn($iterator);
24+
$dependencyFileFormats = DependencyFileFormat::make(
25+
\json_decode(FindAndUploadFilesCommandTest::FORMATS_JSON_STRING, true)
26+
);
27+
28+
$lockFiles = ['app/package-lock.json' => new SplFileInfo('package-lock.json', 'app', 'app/package-lock.json')];
29+
$fileGroups = FileGroupFinder::createFileGroups($lockFiles, $finderMock, $dependencyFileFormats, '.');
30+
31+
$this->assertCount(1, $fileGroups);
32+
$fileGroup = $fileGroups[0];
33+
$this->assertInstanceOf(\SplFileInfo::class, $fileGroup->getDependencyFile());
34+
$this->assertNotEmpty($fileGroup->getLockFiles());
35+
$this->assertCount(1, $fileGroup->getLockFiles());
36+
$lockFile = $fileGroup->getLockFiles()[0];
37+
$this->assertInstanceOf(\SplFileInfo::class, $lockFile);
38+
}
39+
40+
public function testCreateFileGroupsOnWindows(): void
41+
{
42+
$iterator = [
43+
new SplFileInfo('package.json', 'app', 'app\package.json'),
44+
new SplFileInfo('package-lock.json', 'app', 'app\package-lock.json'),
45+
];
46+
$iterator = new \ArrayIterator($iterator);
47+
48+
$finderMock = $this->getMockBuilder(Finder::class)->disableOriginalConstructor()->getMock();
49+
$finderMock->expects($this->once())->method('getIterator')->willReturn($iterator);
50+
$dependencyFileFormats = DependencyFileFormat::make(
51+
\json_decode(FindAndUploadFilesCommandTest::FORMATS_JSON_STRING, true)
52+
);
53+
54+
$lockFiles = ['app\package-lock.json' => new SplFileInfo('package-lock.json', 'app', 'app\package-lock.json')];
55+
$fileGroups = FileGroupFinder::createFileGroups($lockFiles, $finderMock, $dependencyFileFormats, '.');
56+
57+
$this->assertCount(1, $fileGroups);
58+
$fileGroup = $fileGroups[0];
59+
$this->assertInstanceOf(\SplFileInfo::class, $fileGroup->getDependencyFile());
60+
$this->assertNotEmpty($fileGroup->getLockFiles());
61+
$this->assertCount(1, $fileGroup->getLockFiles());
62+
$lockFile = $fileGroup->getLockFiles()[0];
63+
$this->assertInstanceOf(\SplFileInfo::class, $lockFile);
64+
}
65+
}

0 commit comments

Comments
 (0)