Skip to content

Commit

Permalink
minor #4954 Config - Trim path (julienfalque)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.15 branch.

Discussion
----------

Config - Trim path

Commits
-------

dffffff Trim path
  • Loading branch information
SpacePossum committed May 23, 2020
2 parents de12ef2 + dffffff commit 75f49dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,11 @@ public function getPath()
$this->path = $this->options['path'];
} else {
$this->path = array_map(
static function ($path) use ($cwd, $filesystem) {
static function ($rawPath) use ($cwd, $filesystem) {
$path = trim($rawPath);

if ('' === $path) {
throw new InvalidConfigurationException('Invalid path: "".');
throw new InvalidConfigurationException("Invalid path: \"{$rawPath}\".");
}

$absolutePath = $filesystem->isAbsolutePath($path)
Expand Down
84 changes: 68 additions & 16 deletions tests/Console/ConfigurationResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,32 +322,58 @@ public function testResolveConfigFileChooseFileWithPathArrayAndConfig()
static::assertInstanceOf(\PhpCsFixer\Console\ConfigurationResolver::class, $resolver);
}

public function testResolvePathRelativeA()
/**
* @param array<int, string> $paths
* @param string $cwd
* @param array<int, string> $expectedPaths
*
* @dataProvider providePathCases
*/
public function testResolvePath(array $paths, $cwd, array $expectedPaths)
{
$resolver = $this->createConfigurationResolver(
['path' => ['Command']],
['path' => $paths],
null,
__DIR__
$cwd
);

static::assertSame([__DIR__.\DIRECTORY_SEPARATOR.'Command'], $resolver->getPath());
static::assertSame($expectedPaths, $resolver->getPath());
}

public function testResolvePathRelativeB()
public function providePathCases()
{
$resolver = $this->createConfigurationResolver(
['path' => [basename(__DIR__)]],
null,
\dirname(__DIR__)
);
yield [
['Command'],
__DIR__,
[__DIR__.\DIRECTORY_SEPARATOR.'Command'],
];

yield [
[basename(__DIR__)],
\dirname(__DIR__),
[__DIR__],
];

yield [
[' Command'],
__DIR__,
[__DIR__.\DIRECTORY_SEPARATOR.'Command'],
];

static::assertSame([__DIR__], $resolver->getPath());
yield [
['Command '],
__DIR__,
[__DIR__.\DIRECTORY_SEPARATOR.'Command'],
];
}

/**
* @param array<string> $paths
* @param string $expectedMessage
*
* @dataProvider provideEmptyPathCases
*/
public function testRejectInvalidPath(array $paths)
public function testRejectInvalidPath(array $paths, $expectedMessage)
{
$resolver = $this->createConfigurationResolver(
['path' => $paths],
Expand All @@ -356,16 +382,42 @@ public function testRejectInvalidPath(array $paths)
);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid path: "".');
$this->expectExceptionMessage($expectedMessage);

$resolver->getPath();
}

public function provideEmptyPathCases()
{
yield [['']];
yield [[__DIR__, '']];
yield [['', __DIR__]];
yield [
[''],
'Invalid path: "".',
];

yield [
[__DIR__, ''],
'Invalid path: "".',
];

yield [
['', __DIR__],
'Invalid path: "".',
];

yield [
[' '],
'Invalid path: " ".',
];

yield [
[__DIR__, ' '],
'Invalid path: " ".',
];

yield [
[' ', __DIR__],
'Invalid path: " ".',
];
}

public function testResolvePathWithFileThatIsExcludedDirectlyOverridePathMode()
Expand Down

0 comments on commit 75f49dc

Please sign in to comment.