FilesystemTest.php 7.4 KB

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