FilesystemTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Test\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. );
  59. }
  60. /**
  61. * @dataProvider providePathCouples
  62. */
  63. public function testFindShortestPath($a, $b, $expected, $directory = false)
  64. {
  65. $fs = new Filesystem;
  66. $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
  67. }
  68. public function providePathCouples()
  69. {
  70. return array(
  71. array('/foo/bar', '/foo/bar', "./bar"),
  72. array('/foo/bar', '/foo/baz', "./baz"),
  73. array('/foo/bar/', '/foo/baz', "./baz"),
  74. array('/foo/bar', '/foo/bar', "./", true),
  75. array('/foo/bar', '/foo/baz', "../baz", true),
  76. array('/foo/bar/', '/foo/baz', "../baz", true),
  77. array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
  78. array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  79. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
  80. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
  81. array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
  82. array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  83. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  84. array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  85. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  86. array('C:/Temp/test', 'C:\Temp', "./"),
  87. array('/tmp/test', '/tmp', "./"),
  88. array('C:/Temp/test/sub', 'C:\Temp', "../"),
  89. array('/tmp/test/sub', '/tmp', "../"),
  90. array('/tmp/test/sub', '/tmp', "../../", true),
  91. array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
  92. array('/tmp', '/tmp/test', "test"),
  93. array('C:/Temp', 'C:\Temp\test', "test"),
  94. array('C:/Temp', 'c:\Temp\test', "test"),
  95. array('/tmp/test/./', '/tmp/test', './', true),
  96. array('/tmp/test/../vendor', '/tmp/test', '../test', true),
  97. array('/tmp/test/.././vendor', '/tmp/test', '../test', true),
  98. array('C:/Temp', 'c:\Temp\..\..\test', "../test", true),
  99. array('C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true),
  100. array('/tmp', '/tmp/../../test', '/test', true),
  101. array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true),
  102. array('/foo/bar_vendor', '/foo/bar', '../bar', true),
  103. );
  104. }
  105. /**
  106. * @group GH-1339
  107. */
  108. public function testRemoveDirectoryPhp()
  109. {
  110. $tmp = sys_get_temp_dir();
  111. @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true);
  112. file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
  113. $fs = new Filesystem;
  114. $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir"));
  115. $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt"));
  116. }
  117. public function testFileSize()
  118. {
  119. $tmp = sys_get_temp_dir();
  120. file_put_contents("$tmp/composer_test_file", 'Hello');
  121. $fs = new Filesystem;
  122. $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file"));
  123. }
  124. public function testDirectorySize()
  125. {
  126. $tmp = sys_get_temp_dir();
  127. @mkdir("$tmp/composer_testdir", 0777, true);
  128. file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello');
  129. file_put_contents("$tmp/composer_testdir/file2.txt", 'World');
  130. $fs = new Filesystem;
  131. $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir"));
  132. }
  133. /**
  134. * @dataProvider provideNormalizedPaths
  135. */
  136. public function testNormalizePath($expected, $actual)
  137. {
  138. $fs = new Filesystem;
  139. $this->assertEquals($expected, $fs->normalizePath($actual));
  140. }
  141. public function provideNormalizedPaths()
  142. {
  143. return array(
  144. array('../foo', '../foo'),
  145. array('c:/foo/bar', 'c:/foo//bar'),
  146. array('C:/foo/bar', 'C:/foo/./bar'),
  147. array('C:/bar', 'C:/foo/../bar'),
  148. array('/bar', '/foo/../bar/'),
  149. array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
  150. array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
  151. array('/', '/Foo/Bar/../../../..'),
  152. array('/', '/'),
  153. array('c:/', 'c:\\'),
  154. array('../src', 'Foo/Bar/../../../src'),
  155. array('c:../b', 'c:.\\..\\a\\..\\b'),
  156. array('phar://c:../Foo', 'phar://c:../Foo'),
  157. );
  158. }
  159. }