From 4517a2e51eb2b8c59fe33f626adf592894e4f5cf Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 3 Dec 2011 23:21:10 +0100 Subject: [PATCH] Added tests and fixed some incorrect behaviors in Filesystem --- src/Composer/Downloader/Util/Filesystem.php | 22 +++++++++--- .../Test/Downloader/Util/FilesystemTest.php | 35 +++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Composer/Downloader/Util/Filesystem.php b/src/Composer/Downloader/Util/Filesystem.php index 66e423e1d..bd8a346af 100644 --- a/src/Composer/Downloader/Util/Filesystem.php +++ b/src/Composer/Downloader/Util/Filesystem.php @@ -58,8 +58,8 @@ class Filesystem if (dirname($from) === dirname($to)) { return './'.basename($to); } - $from = strtr($from, '\\', '/'); - $to = strtr($to, '\\', '/'); + $from = rtrim(strtr($from, '\\', '/'), '/'); + $to = rtrim(strtr($to, '\\', '/'), '/'); $commonPath = dirname($to); while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) { @@ -70,6 +70,10 @@ class Filesystem return $to; } + if (strpos($from, $to) === 0) { + $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); + return str_repeat('../', $sourcePathDepth); + } $commonPath = rtrim($commonPath, '/') . '/'; $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); $commonPathCode = str_repeat('../', $sourcePathDepth); @@ -81,16 +85,17 @@ class Filesystem * * @param string $from * @param string $to + * @param Boolean $directories if true, the source/target are considered to be directories * @return string */ - public function findShortestPathCode($from, $to) + public function findShortestPathCode($from, $to, $directories = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException('from and to must be absolute paths'); } if ($from === $to) { - return '__FILE__'; + return $directories ? '__DIR__' : '__FILE__'; } $from = strtr($from, '\\', '/'); $to = strtr($to, '\\', '/'); @@ -105,7 +110,14 @@ class Filesystem } $commonPath = rtrim($commonPath, '/') . '/'; - $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); + if (strpos($to, $from) === 0) { + return '__DIR__ . '.var_export(substr($to, strlen($from)), true); + } + if (strpos($from, $to) === 0) { + $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') - 1 + $directories; + return str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); + } + $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories; $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true); } diff --git a/tests/Composer/Test/Downloader/Util/FilesystemTest.php b/tests/Composer/Test/Downloader/Util/FilesystemTest.php index f4d9af04f..592875dc2 100644 --- a/tests/Composer/Test/Downloader/Util/FilesystemTest.php +++ b/tests/Composer/Test/Downloader/Util/FilesystemTest.php @@ -20,24 +20,35 @@ class FilesystemTest extends TestCase /** * @dataProvider providePathCouplesAsCode */ - public function testFindShortestPathCode($a, $b, $expected) + public function testFindShortestPathCode($a, $b, $directory, $expected) { $fs = new Filesystem; - $this->assertEquals($expected, $fs->findShortestPathCode($a, $b)); + $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory)); } public function providePathCouplesAsCode() { return array( - array('/foo/bar', '/foo/bar', "__FILE__"), - array('/foo/bar', '/foo/baz', "__DIR__.'/baz'"), - array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), - array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), - array('/foo/bin/run', '/bar/bin/run', "'/bar/bin/run'"), - array('c:/bin/run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), - array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"), - array('c:/bin/run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"), - array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"), + array('/foo/bar', '/foo/bar', false, "__FILE__"), + array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"), + array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), + array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), + array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"), + array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), + array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), + array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"), + array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"), + array('/foo/bar', '/foo/bar', true, "__DIR__"), + array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"), + array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), + array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), + array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"), + array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), + array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), + array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"), + array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"), + array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"), + array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"), ); } @@ -62,6 +73,8 @@ class FilesystemTest extends TestCase array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), + array('C:/Temp/test', 'C:\Temp', "../"), + array('C:/Temp', 'C:\Temp\test', "test"), ); } }