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