From efd5fb3deb353728ef0fc16aec29fccdb78d4ffd Mon Sep 17 00:00:00 2001 From: Christian Scheb Date: Sun, 23 Aug 2015 20:33:10 +0200 Subject: [PATCH] Only normalize relative paths --- src/Graveyard.php | 3 +-- src/Tracing/PathNormalizer.php | 1 + tests/GraveyardTest.php | 6 ++--- tests/Tracing/PathNormalizerTest.php | 38 ++++++++++++++++------------ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Graveyard.php b/src/Graveyard.php index ff3615c..f2930fa 100644 --- a/src/Graveyard.php +++ b/src/Graveyard.php @@ -34,7 +34,7 @@ public function __construct(array $handlers = array(), $sourceDir = null) */ public function setSourceDir($sourceDir) { - $this->sourceDir = PathNormalizer::normalizeDirectorySeparator($sourceDir); + $this->sourceDir = $sourceDir; } /** @@ -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); } } diff --git a/src/Tracing/PathNormalizer.php b/src/Tracing/PathNormalizer.php index 9e66eeb..226dcb8 100644 --- a/src/Tracing/PathNormalizer.php +++ b/src/Tracing/PathNormalizer.php @@ -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); } diff --git a/tests/GraveyardTest.php b/tests/GraveyardTest.php index fae63fa..a234fda 100644 --- a/tests/GraveyardTest.php +++ b/tests/GraveyardTest.php @@ -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); } diff --git a/tests/Tracing/PathNormalizerTest.php b/tests/Tracing/PathNormalizerTest.php index 02cd265..c1b2312 100644 --- a/tests/Tracing/PathNormalizerTest.php +++ b/tests/Tracing/PathNormalizerTest.php @@ -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), + ); } }