ArchivableFilesFinderTest.php 6.6 KB

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