FilesystemTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\Filesystem;
  13. use Composer\TestCase;
  14. class FilesystemTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider providePathCouplesAsCode
  18. */
  19. public function testFindShortestPathCode($a, $b, $directory, $expected)
  20. {
  21. $fs = new Filesystem;
  22. $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory));
  23. }
  24. public function providePathCouplesAsCode()
  25. {
  26. return array(
  27. array('/foo/bar', '/foo/bar', false, "__FILE__"),
  28. array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
  29. array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  30. array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
  31. array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  32. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  33. array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  34. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  35. array('/foo/bar', '/foo/bar', true, "__DIR__"),
  36. array('/foo/bar/', '/foo/bar', true, "__DIR__"),
  37. array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
  38. array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  39. array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
  40. array('/bin/run', '/bin/run', true, "__DIR__"),
  41. array('c:/bin/run', 'c:\\bin/run', true, "__DIR__"),
  42. array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  43. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  44. array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  45. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  46. array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
  47. array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
  48. array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
  49. array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
  50. array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
  51. array('/tmp/test/./', '/tmp/test/', true, '__DIR__'),
  52. array('/tmp/test/../vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  53. array('/tmp/test/.././vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  54. array('C:/Temp', 'c:\Temp\..\..\test', true, "dirname(__DIR__).'/test'"),
  55. array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'"),
  56. array('/foo/bar', '/foo/bar_vendor', true, "dirname(__DIR__).'/bar_vendor'"),
  57. array('/foo/bar_vendor', '/foo/bar', true, "dirname(__DIR__).'/bar'"),
  58. array('/foo/bar_vendor', '/foo/bar/src', true, "dirname(__DIR__).'/bar/src'"),
  59. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "dirname(dirname(__DIR__)).'/bar/src/lib'"),
  60. );
  61. }
  62. /**
  63. * @dataProvider providePathCouples
  64. */
  65. public function testFindShortestPath($a, $b, $expected, $directory = false)
  66. {
  67. $fs = new Filesystem;
  68. $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
  69. }
  70. public function providePathCouples()
  71. {
  72. return array(
  73. array('/foo/bar', '/foo/bar', "./bar"),
  74. array('/foo/bar', '/foo/baz', "./baz"),
  75. array('/foo/bar/', '/foo/baz', "./baz"),
  76. array('/foo/bar', '/foo/bar', "./", true),
  77. array('/foo/bar', '/foo/baz', "../baz", true),
  78. array('/foo/bar/', '/foo/baz', "../baz", true),
  79. array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
  80. array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  81. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
  82. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
  83. array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
  84. array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  85. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  86. array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  87. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  88. array('C:/Temp/test', 'C:\Temp', "./"),
  89. array('/tmp/test', '/tmp', "./"),
  90. array('C:/Temp/test/sub', 'C:\Temp', "../"),
  91. array('/tmp/test/sub', '/tmp', "../"),
  92. array('/tmp/test/sub', '/tmp', "../../", true),
  93. array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
  94. array('/tmp', '/tmp/test', "test"),
  95. array('C:/Temp', 'C:\Temp\test', "test"),
  96. array('C:/Temp', 'c:\Temp\test', "test"),
  97. array('/tmp/test/./', '/tmp/test', './', true),
  98. array('/tmp/test/../vendor', '/tmp/test', '../test', true),
  99. array('/tmp/test/.././vendor', '/tmp/test', '../test', true),
  100. array('C:/Temp', 'c:\Temp\..\..\test', "../test", true),
  101. array('C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true),
  102. array('C:/Temp/../..', 'D:\Temp\..\..\test', "d:/test", true),
  103. array('/tmp', '/tmp/../../test', '/test', true),
  104. array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true),
  105. array('/foo/bar_vendor', '/foo/bar', '../bar', true),
  106. array('/foo/bar_vendor', '/foo/bar/src', '../bar/src', true),
  107. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', '../../bar/src/lib', true),
  108. array('C:/', 'C:/foo/bar/', "foo/bar", true),
  109. );
  110. }
  111. /**
  112. * @group GH-1339
  113. */
  114. public function testRemoveDirectoryPhp()
  115. {
  116. $tmp = sys_get_temp_dir();
  117. @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true);
  118. file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
  119. $fs = new Filesystem;
  120. $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir"));
  121. $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt"));
  122. }
  123. public function testFileSize()
  124. {
  125. $tmp = sys_get_temp_dir();
  126. file_put_contents("$tmp/composer_test_file", 'Hello');
  127. $fs = new Filesystem;
  128. $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file"));
  129. }
  130. public function testDirectorySize()
  131. {
  132. $tmp = sys_get_temp_dir();
  133. @mkdir("$tmp/composer_testdir", 0777, true);
  134. file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello');
  135. file_put_contents("$tmp/composer_testdir/file2.txt", 'World');
  136. $fs = new Filesystem;
  137. $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir"));
  138. }
  139. /**
  140. * @dataProvider provideNormalizedPaths
  141. */
  142. public function testNormalizePath($expected, $actual)
  143. {
  144. $fs = new Filesystem;
  145. $this->assertEquals($expected, $fs->normalizePath($actual));
  146. }
  147. public function provideNormalizedPaths()
  148. {
  149. return array(
  150. array('../foo', '../foo'),
  151. array('c:/foo/bar', 'c:/foo//bar'),
  152. array('C:/foo/bar', 'C:/foo/./bar'),
  153. array('C:/bar', 'C:/foo/../bar'),
  154. array('/bar', '/foo/../bar/'),
  155. array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
  156. array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
  157. array('/', '/Foo/Bar/../../../..'),
  158. array('/', '/'),
  159. array('c:/', 'c:\\'),
  160. array('../src', 'Foo/Bar/../../../src'),
  161. array('c:../b', 'c:.\\..\\a\\..\\b'),
  162. array('phar://c:../Foo', 'phar://c:../Foo'),
  163. );
  164. }
  165. /**
  166. * @link https://github.com/composer/composer/issues/3157
  167. * @requires function symlink
  168. */
  169. public function testUnlinkSymlinkedDirectory()
  170. {
  171. $tmp = sys_get_temp_dir();
  172. $basepath = $tmp . "/composer_testdir";
  173. $symlinked = $basepath . "/linked";
  174. @mkdir($basepath . "/real", 0777, true);
  175. touch($basepath . "/real/FILE");
  176. $result = @symlink($basepath . "/real", $symlinked);
  177. if (!$result) {
  178. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  179. }
  180. if (!is_dir($symlinked)) {
  181. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  182. }
  183. $fs = new Filesystem();
  184. $result = $fs->unlink($symlinked);
  185. $this->assertTrue($result);
  186. $this->assertFalse(file_exists($symlinked));
  187. }
  188. /**
  189. * @link https://github.com/composer/composer/issues/3144
  190. * @requires function symlink
  191. */
  192. public function testRemoveSymlinkedDirectoryWithTrailingSlash()
  193. {
  194. $tmp = sys_get_temp_dir();
  195. $basepath = $tmp . "/composer_testdir";
  196. @mkdir($basepath . "/real", 0777, true);
  197. touch($basepath . "/real/FILE");
  198. $symlinked = $basepath . "/linked";
  199. $symlinkedTrailingSlash = $symlinked . "/";
  200. $result = @symlink($basepath . "/real", $symlinked);
  201. if (!$result) {
  202. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  203. }
  204. if (!is_dir($symlinked)) {
  205. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  206. }
  207. if (!is_dir($symlinkedTrailingSlash)) {
  208. $this->fail('Precondition assertion failed (is_dir false w trailing slash).');
  209. }
  210. $fs = new Filesystem();
  211. $result = $fs->removeDirectory($symlinkedTrailingSlash);
  212. $this->assertTrue($result);
  213. $this->assertFalse(file_exists($symlinkedTrailingSlash));
  214. $this->assertFalse(file_exists($symlinked));
  215. }
  216. }