Skip to content

Commit

Permalink
Only normalize relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Aug 23, 2015
1 parent cc77ca8 commit efd5fb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/Graveyard.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $handlers = array(), $sourceDir = null)
*/
public function setSourceDir($sourceDir)
{
$this->sourceDir = PathNormalizer::normalizeDirectorySeparator($sourceDir);
$this->sourceDir = $sourceDir;
}

/**
Expand Down Expand Up @@ -73,7 +73,6 @@ private function traceRelativePath(array $trace)

foreach ($trace as $key => &$frame) {
if (isset($frame['file'])) {
$frame['file'] = PathNormalizer::normalizeDirectorySeparator($frame['file']);
$frame['file'] = PathNormalizer::makeRelativeTo($frame['file'], $this->sourceDir);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Tracing/PathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static function makeRelativeTo($path, $baseDir)
{
if ($baseDir && self::startsWith($path, $baseDir)) {
$path = substr($path, strlen($baseDir));
$path = PathNormalizer::normalizeDirectorySeparator($path);
if ($path[0] === '/') {
$path = substr($path, 1);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/GraveyardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ public function tombstone_sourceDirSet_logRelativePath()
/**
* @test
*/
public function tombstone_windowsStyleSourceDirSet_logRelativePath()
public function tombstone_sourceDirNotMatchedFilePath_logAbsolutePath()
{
$this->handler
->expects($this->once())
->method('log')
->with($this->callback(function ($vampire) {
return $vampire instanceof Vampire && $vampire->getFile() === 'file1.php';
return $vampire instanceof Vampire && $vampire->getFile() === '/path/to/file1.php';
}));

$trace = TraceFixture::getTraceFixture();
$this->graveyard->setSourceDir('\\path\\to');
$this->graveyard->setSourceDir('/other/path');
$this->graveyard->tombstone('date', 'author', 'label', $trace);
}

Expand Down
38 changes: 22 additions & 16 deletions tests/Tracing/PathNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,44 @@ public function normalizeDirectorySeparator_windowsPathGiven_changeDirectorySepa

/**
* @test
* @dataProvider getBaseDirsToTest
* @dataProvider getTestCasesForRelativePath
*/
public function makeRelativeTo_pathBeginsWithBase_returnRelativePath($baseDir) {
$path = '/path/to/file.php';
public function makeRelativeTo_pathBeginsWithBase_returnRelativePath($path, $baseDir) {
$returnValue = PathNormalizer::makeRelativeTo($path, $baseDir);
$this->assertEquals('file.php', $returnValue);
$this->assertEquals('directory/file.php', $returnValue);
}

public function getBaseDirsToTest()
/**
* @return array
*/
public function getTestCasesForRelativePath()
{
return array(
array('/path/to'),
array('/path/to/'),
array('/path/to/directory/file.php', '/path/to'),
array('/path/to/directory/file.php', '/path/to/'),
array('C:\\path\\to\\directory\\file.php', 'C:\\path\\to'),
array('C:\\path\\to\\directory\\file.php', 'C:\\path\\to\\'),
);
}

/**
* @test
* @dataProvider getTestCasesForKeepingPath
*/
public function makeRelativeTo_pathHasDifferentBase_returnSamePath() {
$path = '/path/to/file.php';
$baseDir = '/other/base';
public function makeRelativeTo_pathHasDifferentBase_returnSamePath($path, $baseDir) {
$returnValue = PathNormalizer::makeRelativeTo($path, $baseDir);
$this->assertEquals('/path/to/file.php', $returnValue);
$this->assertEquals($path, $returnValue);
}

/**
* @test
* @return array
*/
public function makeRelativeTo_noBaseDirGiven_returnSamePath() {
$path = '/path/to/file.php';
$returnValue = PathNormalizer::makeRelativeTo($path, null);
$this->assertEquals('/path/to/file.php', $returnValue);
public function getTestCasesForKeepingPath() {
return array(
array('/path/to/file.php', '/other/base'),
array('/path/to/file.php', null),
array('C:\\path\\to\\file.php', 'C:\\other\\path'),
array('C:\\path\\to\\file.php', null),
);
}
}

0 comments on commit efd5fb3

Please sign in to comment.