PageRenderTime 62ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/composer/composer/tests/Composer/Test/Util/FilesystemTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 240 lines | 186 code | 27 blank | 27 comment | 5 complexity | 457677600591183f842e2374ab7ebd67 MD5 | raw file
  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. );
  109. }
  110. /**
  111. * @group GH-1339
  112. */
  113. public function testRemoveDirectoryPhp()
  114. {
  115. $tmp = sys_get_temp_dir();
  116. @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true);
  117. file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
  118. $fs = new Filesystem;
  119. $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir"));
  120. $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt"));
  121. }
  122. public function testFileSize()
  123. {
  124. $tmp = sys_get_temp_dir();
  125. file_put_contents("$tmp/composer_test_file", 'Hello');
  126. $fs = new Filesystem;
  127. $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file"));
  128. }
  129. public function testDirectorySize()
  130. {
  131. $tmp = sys_get_temp_dir();
  132. @mkdir("$tmp/composer_testdir", 0777, true);
  133. file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello');
  134. file_put_contents("$tmp/composer_testdir/file2.txt", 'World');
  135. $fs = new Filesystem;
  136. $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir"));
  137. }
  138. /**
  139. * @dataProvider provideNormalizedPaths
  140. */
  141. public function testNormalizePath($expected, $actual)
  142. {
  143. $fs = new Filesystem;
  144. $this->assertEquals($expected, $fs->normalizePath($actual));
  145. }
  146. public function provideNormalizedPaths()
  147. {
  148. return array(
  149. array('../foo', '../foo'),
  150. array('c:/foo/bar', 'c:/foo//bar'),
  151. array('C:/foo/bar', 'C:/foo/./bar'),
  152. array('C:/bar', 'C:/foo/../bar'),
  153. array('/bar', '/foo/../bar/'),
  154. array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
  155. array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
  156. array('/', '/Foo/Bar/../../../..'),
  157. array('/', '/'),
  158. array('c:/', 'c:\\'),
  159. array('../src', 'Foo/Bar/../../../src'),
  160. array('c:../b', 'c:.\\..\\a\\..\\b'),
  161. array('phar://c:../Foo', 'phar://c:../Foo'),
  162. );
  163. }
  164. /**
  165. * @link https://github.com/composer/composer/issues/3157
  166. */
  167. public function testUnlinkSymlinkedDirectory()
  168. {
  169. $tmp = sys_get_temp_dir();
  170. $basepath = $tmp . "/composer_testdir";
  171. $symlinked = $basepath . "/linked";
  172. @mkdir($basepath . "/real", 0777, true);
  173. touch($basepath . "/real/FILE");
  174. $result = @symlink($basepath . "/real", $symlinked);
  175. if (!$result) {
  176. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  177. }
  178. if (!is_dir($symlinked)) {
  179. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  180. }
  181. $fs = new Filesystem();
  182. $result = $fs->unlink($symlinked);
  183. $this->assertTrue($result);
  184. $this->assertFalse(file_exists($symlinked));
  185. }
  186. /**
  187. * @link https://github.com/composer/composer/issues/3144
  188. */
  189. public function testRemoveSymlinkedDirectoryWithTrailingSlash()
  190. {
  191. $tmp = sys_get_temp_dir();
  192. $basepath = $tmp . "/composer_testdir";
  193. @mkdir($basepath . "/real", 0777, true);
  194. touch($basepath . "/real/FILE");
  195. $symlinked = $basepath . "/linked";
  196. $symlinkedTrailingSlash = $symlinked . "/";
  197. $result = @symlink($basepath . "/real", $symlinked);
  198. if (!$result) {
  199. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  200. }
  201. if (!is_dir($symlinked)) {
  202. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  203. }
  204. if (!is_dir($symlinkedTrailingSlash)) {
  205. $this->fail('Precondition assertion failed (is_dir false w trailing slash).');
  206. }
  207. $fs = new Filesystem();
  208. $result = $fs->removeDirectory($symlinkedTrailingSlash);
  209. $this->assertTrue($result);
  210. $this->assertFalse(file_exists($symlinkedTrailingSlash));
  211. $this->assertFalse(file_exists($symlinked));
  212. }
  213. }