Skip to content

Commit

Permalink
Fixed FileHelper::resolveAbsolutePath() with empty destination path
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Feb 4, 2022
1 parent 9eacc46 commit 6745fce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.6.2] - 2022-02-04

### Fixed

- `FileHelper::resolveAbsolutePath()` with empty destination path

## [1.6.1] - 2022-01-18

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static function resolveAbsolutePath(string $srcPath, string $dstPath): ?s
{
$srcPath = self::uniformizePathSeparator($srcPath);
$dstPath = self::uniformizePathSeparator($dstPath);
$finalPath = $dstPath;
$finalPath = $dstPath ?: $srcPath;

if (substr($dstPath, 0, 1) !== '/') {
if (strlen($dstPath) > 0 && substr($dstPath, 0, 1) !== '/') {
// Complete absolute link
if (substr($dstPath, 0, 2) === './') {
$dstPath = substr($dstPath, 2);
Expand Down
14 changes: 9 additions & 5 deletions tests/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ public function absolutePathProvider(): array
['foo/bar/', '../', '/foo/'],
['foo/bar/', '..', '/foo/'],
['foo/bar/', '../qux', '/foo/qux'],
['foo/bar/', '', '/foo/bar/'],
['foo/bar', '', '/foo/bar'],
];
}

/**
* @param $src
* @param $dst
* @param $excepted
* @param $expected
*
* @dataProvider absolutePathProvider
*/
public function testResolveAbsolutePath($src, $dst, $excepted)
public function testResolveAbsolutePath($src, $dst, $expected)
{
$this->assertEquals($excepted, FileHelper::resolveAbsolutePath($src, $dst));
$this->assertEquals($expected, FileHelper::resolveAbsolutePath($src, $dst));
}

public function relativePathProvider(): array
Expand Down Expand Up @@ -106,14 +108,16 @@ public function relativePathProvider(): array
['/foo/bar/index.md', '../../', '../../'],
['/foo/bar/index.md', '..', '../'],
['/foo/bar/index.md', '../', '../'],
['/foo/bar/', '', './'],
['/foo/bar/index.md', '', './index.md'],
];
}

/**
* @dataProvider relativePathProvider
*/
public function testResolveRelativePath($src, $dst, $excepted)
public function testResolveRelativePath($src, $dst, $expected)
{
$this->assertEquals($excepted, FileHelper::resolveRelativePath($src, $dst));
$this->assertEquals($expected, FileHelper::resolveRelativePath($src, $dst));
}
}

0 comments on commit 6745fce

Please sign in to comment.