FilesystemTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/baz', true, "dirname(__DIR__).'/baz'"),
  37. array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  38. array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
  39. array('/bin/run', '/bin/run', true, "__DIR__"),
  40. array('c:/bin/run', 'c:\\bin/run', true, "__DIR__"),
  41. array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  42. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  43. array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  44. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  45. array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
  46. array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
  47. array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
  48. array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
  49. array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
  50. );
  51. }
  52. /**
  53. * @dataProvider providePathCouples
  54. */
  55. public function testFindShortestPath($a, $b, $expected, $directory = false)
  56. {
  57. $fs = new Filesystem;
  58. $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
  59. }
  60. public function providePathCouples()
  61. {
  62. return array(
  63. array('/foo/bar', '/foo/bar', "./bar"),
  64. array('/foo/bar', '/foo/baz', "./baz"),
  65. array('/foo/bar/', '/foo/baz', "./baz"),
  66. array('/foo/bar', '/foo/bar', "./", true),
  67. array('/foo/bar', '/foo/baz', "../baz", true),
  68. array('/foo/bar/', '/foo/baz', "../baz", true),
  69. array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
  70. array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  71. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
  72. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
  73. array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
  74. array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  75. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  76. array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  77. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  78. array('C:/Temp/test', 'C:\Temp', "./"),
  79. array('/tmp/test', '/tmp', "./"),
  80. array('C:/Temp/test/sub', 'C:\Temp', "../"),
  81. array('/tmp/test/sub', '/tmp', "../"),
  82. array('/tmp/test/sub', '/tmp', "../../", true),
  83. array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
  84. array('/tmp', '/tmp/test', "test"),
  85. array('C:/Temp', 'C:\Temp\test', "test"),
  86. array('C:/Temp', 'c:\Temp\test', "test"),
  87. );
  88. }
  89. /**
  90. * @group GH-1339
  91. */
  92. public function testRemoveDirectoryPhp()
  93. {
  94. $tmp = sys_get_temp_dir();
  95. @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true);
  96. file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
  97. $fs = new Filesystem;
  98. $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir"));
  99. $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt"));
  100. }
  101. public function testFileSize()
  102. {
  103. $tmp = sys_get_temp_dir();
  104. file_put_contents("$tmp/composer_test_file", 'Hello');
  105. $fs = new Filesystem;
  106. $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file"));
  107. }
  108. public function testDirectorySize()
  109. {
  110. $tmp = sys_get_temp_dir();
  111. @mkdir("$tmp/composer_testdir", 0777, true);
  112. file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello');
  113. file_put_contents("$tmp/composer_testdir/file2.txt", 'World');
  114. $fs = new Filesystem;
  115. $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir"));
  116. }
  117. }