FilesystemTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * @var Filesystem
  18. */
  19. private $fs;
  20. /**
  21. * @var string
  22. */
  23. private $workingDir;
  24. /**
  25. * @var string
  26. */
  27. private $testFile;
  28. /**
  29. * @dataProvider providePathCouplesAsCode
  30. */
  31. public function testFindShortestPathCode($a, $b, $directory, $expected)
  32. {
  33. $fs = new Filesystem;
  34. $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory));
  35. }
  36. public function setUp()
  37. {
  38. $this->fs = new Filesystem;
  39. $this->workingDir = sys_get_temp_dir() . '/composer_testdir';
  40. $this->testFile = sys_get_temp_dir() . '/composer_test_file';
  41. }
  42. public function tearDown()
  43. {
  44. if (is_dir($this->workingDir)) {
  45. $this->fs->removeDirectory($this->workingDir);
  46. }
  47. if (is_file($this->testFile)) {
  48. $this->fs->remove($this->testFile);
  49. }
  50. }
  51. public function providePathCouplesAsCode()
  52. {
  53. return array(
  54. array('/foo/bar', '/foo/bar', false, "__FILE__"),
  55. array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
  56. array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  57. array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
  58. array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  59. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  60. array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  61. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  62. array('/foo/bar', '/foo/bar', true, "__DIR__"),
  63. array('/foo/bar/', '/foo/bar', true, "__DIR__"),
  64. array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
  65. array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  66. array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
  67. array('/bin/run', '/bin/run', true, "__DIR__"),
  68. array('c:/bin/run', 'c:\\bin/run', true, "__DIR__"),
  69. array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  70. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  71. array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  72. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  73. array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
  74. array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
  75. array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
  76. array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
  77. array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
  78. array('/tmp/test/./', '/tmp/test/', true, '__DIR__'),
  79. array('/tmp/test/../vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  80. array('/tmp/test/.././vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  81. array('C:/Temp', 'c:\Temp\..\..\test', true, "dirname(__DIR__).'/test'"),
  82. array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'"),
  83. array('/foo/bar', '/foo/bar_vendor', true, "dirname(__DIR__).'/bar_vendor'"),
  84. array('/foo/bar_vendor', '/foo/bar', true, "dirname(__DIR__).'/bar'"),
  85. array('/foo/bar_vendor', '/foo/bar/src', true, "dirname(__DIR__).'/bar/src'"),
  86. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "dirname(dirname(__DIR__)).'/bar/src/lib'"),
  87. );
  88. }
  89. /**
  90. * @dataProvider providePathCouples
  91. */
  92. public function testFindShortestPath($a, $b, $expected, $directory = false)
  93. {
  94. $fs = new Filesystem;
  95. $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
  96. }
  97. public function providePathCouples()
  98. {
  99. return array(
  100. array('/foo/bar', '/foo/bar', "./bar"),
  101. array('/foo/bar', '/foo/baz', "./baz"),
  102. array('/foo/bar/', '/foo/baz', "./baz"),
  103. array('/foo/bar', '/foo/bar', "./", true),
  104. array('/foo/bar', '/foo/baz', "../baz", true),
  105. array('/foo/bar/', '/foo/baz', "../baz", true),
  106. array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
  107. array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  108. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
  109. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
  110. array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
  111. array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  112. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  113. array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  114. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  115. array('C:/Temp/test', 'C:\Temp', "./"),
  116. array('/tmp/test', '/tmp', "./"),
  117. array('C:/Temp/test/sub', 'C:\Temp', "../"),
  118. array('/tmp/test/sub', '/tmp', "../"),
  119. array('/tmp/test/sub', '/tmp', "../../", true),
  120. array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
  121. array('/tmp', '/tmp/test', "test"),
  122. array('C:/Temp', 'C:\Temp\test', "test"),
  123. array('C:/Temp', 'c:\Temp\test', "test"),
  124. array('/tmp/test/./', '/tmp/test', './', true),
  125. array('/tmp/test/../vendor', '/tmp/test', '../test', true),
  126. array('/tmp/test/.././vendor', '/tmp/test', '../test', true),
  127. array('C:/Temp', 'c:\Temp\..\..\test', "../test", true),
  128. array('C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true),
  129. array('C:/Temp/../..', 'D:\Temp\..\..\test', "d:/test", true),
  130. array('/tmp', '/tmp/../../test', '/test', true),
  131. array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true),
  132. array('/foo/bar_vendor', '/foo/bar', '../bar', true),
  133. array('/foo/bar_vendor', '/foo/bar/src', '../bar/src', true),
  134. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', '../../bar/src/lib', true),
  135. array('C:/', 'C:/foo/bar/', "foo/bar", true),
  136. );
  137. }
  138. /**
  139. * @group GH-1339
  140. */
  141. public function testRemoveDirectoryPhp()
  142. {
  143. @mkdir($this->workingDir . "/level1/level2", 0777, true);
  144. file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world");
  145. $fs = new Filesystem;
  146. $this->assertTrue($fs->removeDirectoryPhp($this->workingDir));
  147. $this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt"));
  148. }
  149. public function testFileSize()
  150. {
  151. file_put_contents($this->testFile, 'Hello');
  152. $fs = new Filesystem;
  153. $this->assertGreaterThanOrEqual(5, $fs->size($this->testFile));
  154. }
  155. public function testDirectorySize()
  156. {
  157. @mkdir($this->workingDir, 0777, true);
  158. file_put_contents($this->workingDir."/file1.txt", 'Hello');
  159. file_put_contents($this->workingDir."/file2.txt", 'World');
  160. $fs = new Filesystem;
  161. $this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir));
  162. }
  163. /**
  164. * @dataProvider provideNormalizedPaths
  165. */
  166. public function testNormalizePath($expected, $actual)
  167. {
  168. $fs = new Filesystem;
  169. $this->assertEquals($expected, $fs->normalizePath($actual));
  170. }
  171. public function provideNormalizedPaths()
  172. {
  173. return array(
  174. array('../foo', '../foo'),
  175. array('c:/foo/bar', 'c:/foo//bar'),
  176. array('C:/foo/bar', 'C:/foo/./bar'),
  177. array('C:/bar', 'C:/foo/../bar'),
  178. array('/bar', '/foo/../bar/'),
  179. array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
  180. array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
  181. array('/', '/Foo/Bar/../../../..'),
  182. array('/', '/'),
  183. array('c:/', 'c:\\'),
  184. array('../src', 'Foo/Bar/../../../src'),
  185. array('c:../b', 'c:.\\..\\a\\..\\b'),
  186. array('phar://c:../Foo', 'phar://c:../Foo'),
  187. );
  188. }
  189. /**
  190. * @link https://github.com/composer/composer/issues/3157
  191. * @requires function symlink
  192. */
  193. public function testUnlinkSymlinkedDirectory()
  194. {
  195. $basepath = $this->workingDir;
  196. $symlinked = $basepath . "/linked";
  197. @mkdir($basepath . "/real", 0777, true);
  198. touch($basepath . "/real/FILE");
  199. $result = @symlink($basepath . "/real", $symlinked);
  200. if (!$result) {
  201. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  202. }
  203. if (!is_dir($symlinked)) {
  204. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  205. }
  206. $fs = new Filesystem();
  207. $result = $fs->unlink($symlinked);
  208. $this->assertTrue($result);
  209. $this->assertFalse(file_exists($symlinked));
  210. }
  211. /**
  212. * @link https://github.com/composer/composer/issues/3144
  213. * @requires function symlink
  214. */
  215. public function testRemoveSymlinkedDirectoryWithTrailingSlash()
  216. {
  217. @mkdir($this->workingDir . "/real", 0777, true);
  218. touch($this->workingDir . "/real/FILE");
  219. $symlinked = $this->workingDir . "/linked";
  220. $symlinkedTrailingSlash = $symlinked . "/";
  221. $result = @symlink($this->workingDir . "/real", $symlinked);
  222. if (!$result) {
  223. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  224. }
  225. if (!is_dir($symlinked)) {
  226. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  227. }
  228. if (!is_dir($symlinkedTrailingSlash)) {
  229. $this->fail('Precondition assertion failed (is_dir false w trailing slash).');
  230. }
  231. $fs = new Filesystem();
  232. $result = $fs->removeDirectory($symlinkedTrailingSlash);
  233. $this->assertTrue($result);
  234. $this->assertFalse(file_exists($symlinkedTrailingSlash));
  235. $this->assertFalse(file_exists($symlinked));
  236. }
  237. }