ArchivableFilesFinderTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Package\Archiver;
  12. use Composer\Package\Archiver\ArchivableFilesFinder;
  13. use Composer\Util\Filesystem;
  14. use Symfony\Component\Process\Process;
  15. /**
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $sources;
  21. protected $finder;
  22. protected function setup()
  23. {
  24. $fs = new Filesystem;
  25. $this->sources = realpath(sys_get_temp_dir()).
  26. '/composer_archiver_test'.uniqid(mt_rand(), true);
  27. $fileTree = array(
  28. 'A/prefixA.foo',
  29. 'A/prefixB.foo',
  30. 'A/prefixC.foo',
  31. 'A/prefixD.foo',
  32. 'A/prefixE.foo',
  33. 'A/prefixF.foo',
  34. 'B/sub/prefixA.foo',
  35. 'B/sub/prefixB.foo',
  36. 'B/sub/prefixC.foo',
  37. 'B/sub/prefixD.foo',
  38. 'B/sub/prefixE.foo',
  39. 'B/sub/prefixF.foo',
  40. 'toplevelA.foo',
  41. 'toplevelB.foo',
  42. 'prefixA.foo',
  43. 'prefixB.foo',
  44. 'prefixC.foo',
  45. 'prefixD.foo',
  46. 'prefixE.foo',
  47. 'prefixF.foo',
  48. );
  49. foreach ($fileTree as $relativePath) {
  50. $path = $this->sources.'/'.$relativePath;
  51. $fs->ensureDirectoryExists(dirname($path));
  52. file_put_contents($path, '');
  53. }
  54. }
  55. protected function tearDown()
  56. {
  57. $fs = new Filesystem;
  58. $fs->removeDirectory($this->sources);
  59. }
  60. public function testManualExcludes()
  61. {
  62. $excludes = array(
  63. 'prefixB.foo',
  64. '!/prefixB.foo',
  65. '/prefixA.foo',
  66. 'prefixC.*',
  67. '!*/*/*/prefixC.foo'
  68. );
  69. $this->finder = new ArchivableFilesFinder($this->sources, $excludes);
  70. $this->assertArchivableFiles(array(
  71. '/A/prefixA.foo',
  72. '/A/prefixD.foo',
  73. '/A/prefixE.foo',
  74. '/A/prefixF.foo',
  75. '/B/sub/prefixA.foo',
  76. '/B/sub/prefixC.foo',
  77. '/B/sub/prefixD.foo',
  78. '/B/sub/prefixE.foo',
  79. '/B/sub/prefixF.foo',
  80. '/prefixB.foo',
  81. '/prefixD.foo',
  82. '/prefixE.foo',
  83. '/prefixF.foo',
  84. '/toplevelA.foo',
  85. '/toplevelB.foo',
  86. ));
  87. }
  88. public function testGitExcludes()
  89. {
  90. file_put_contents($this->sources.'/.gitignore', implode("\n", array(
  91. '# gitignore rules with comments and blank lines',
  92. '',
  93. 'prefixE.foo',
  94. '# and more',
  95. '# comments',
  96. '',
  97. '!/prefixE.foo',
  98. '/prefixD.foo',
  99. 'prefixF.*',
  100. '!/*/*/prefixF.foo',
  101. '',
  102. )));
  103. // git does not currently support negative git attributes
  104. file_put_contents($this->sources.'/.gitattributes', implode("\n", array(
  105. '',
  106. '# gitattributes rules with comments and blank lines',
  107. 'prefixB.foo export-ignore',
  108. //'!/prefixB.foo export-ignore',
  109. '/prefixA.foo export-ignore',
  110. 'prefixC.* export-ignore',
  111. //'!/*/*/prefixC.foo export-ignore'
  112. )));
  113. $this->finder = new ArchivableFilesFinder($this->sources, array());
  114. $this->assertArchivableFiles($this->getArchivedFiles('git init && '.
  115. 'git add .git* && '.
  116. 'git commit -m "ignore rules" && '.
  117. 'git add . && '.
  118. 'git commit -m "init" && '.
  119. 'git archive --format=zip --prefix=archive/ -o archive.zip HEAD'
  120. ));
  121. }
  122. public function testHgExcludes()
  123. {
  124. file_put_contents($this->sources.'/.hgignore', implode("\n", array(
  125. '# hgignore rules with comments, blank lines and syntax changes',
  126. '',
  127. 'pre*A.foo',
  128. 'prefixE.foo',
  129. '# and more',
  130. '# comments',
  131. '',
  132. '^prefixD.foo',
  133. 'syntax: glob',
  134. 'prefixF.*',
  135. 'B/*',
  136. )));
  137. $this->finder = new ArchivableFilesFinder($this->sources, array());
  138. $expectedFiles = $this->getArchivedFiles('hg init && '.
  139. 'hg add && '.
  140. 'hg commit -m "init" && '.
  141. 'hg archive archive.zip'
  142. );
  143. array_shift($expectedFiles); // remove .hg_archival.txt
  144. $this->assertArchivableFiles($expectedFiles);
  145. }
  146. protected function getArchivableFiles()
  147. {
  148. $files = array();
  149. foreach ($this->finder as $file) {
  150. if (!$file->isDir()) {
  151. $files[] = preg_replace('#^'.preg_quote($this->sources, '#').'#', '', $file->getRealPath());
  152. }
  153. }
  154. sort($files);
  155. return $files;
  156. }
  157. protected function getArchivedFiles($command)
  158. {
  159. $process = new Process($command, $this->sources);
  160. $process->run();
  161. $archive = new \PharData($this->sources.'/archive.zip');
  162. $iterator = new \RecursiveIteratorIterator($archive);
  163. $files = array();
  164. foreach ($iterator as $file) {
  165. $files[] = preg_replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $file);
  166. }
  167. unlink($this->sources.'/archive.zip');
  168. return $files;
  169. }
  170. protected function assertArchivableFiles($expectedFiles)
  171. {
  172. $actualFiles = $this->getArchivableFiles();
  173. $this->assertEquals($expectedFiles, $actualFiles);
  174. }
  175. }