ArchivableFilesFinderTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/subtestA.foo',
  54. 'F/subtestA.foo',
  55. 'G/subtestA.foo',
  56. 'H/subtestA.foo',
  57. 'I/J/subtestA.foo',
  58. 'K/dirJ/subtestA.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. 'parameters.yml',
  68. 'parameters.yml.dist',
  69. '!important!.txt',
  70. '!important_too!.txt',
  71. '#weirdfile',
  72. );
  73. foreach ($fileTree as $relativePath) {
  74. $path = $this->sources.'/'.$relativePath;
  75. $fs->ensureDirectoryExists(dirname($path));
  76. file_put_contents($path, '');
  77. }
  78. }
  79. protected function tearDown()
  80. {
  81. $fs = new Filesystem;
  82. $fs->removeDirectory($this->sources);
  83. }
  84. public function testManualExcludes()
  85. {
  86. $excludes = array(
  87. 'prefixB.foo',
  88. '!/prefixB.foo',
  89. '/prefixA.foo',
  90. 'prefixC.*',
  91. '!*/*/*/prefixC.foo',
  92. );
  93. $this->finder = new ArchivableFilesFinder($this->sources, $excludes);
  94. $this->assertArchivableFiles(array(
  95. '/!important!.txt',
  96. '/!important_too!.txt',
  97. '/#weirdfile',
  98. '/A/prefixA.foo',
  99. '/A/prefixD.foo',
  100. '/A/prefixE.foo',
  101. '/A/prefixF.foo',
  102. '/B/sub/prefixA.foo',
  103. '/B/sub/prefixC.foo',
  104. '/B/sub/prefixD.foo',
  105. '/B/sub/prefixE.foo',
  106. '/B/sub/prefixF.foo',
  107. '/C/prefixA.foo',
  108. '/C/prefixD.foo',
  109. '/C/prefixE.foo',
  110. '/C/prefixF.foo',
  111. '/D/prefixA',
  112. '/D/prefixB',
  113. '/D/prefixC',
  114. '/D/prefixD',
  115. '/D/prefixE',
  116. '/D/prefixF',
  117. '/E/subtestA.foo',
  118. '/F/subtestA.foo',
  119. '/G/subtestA.foo',
  120. '/H/subtestA.foo',
  121. '/I/J/subtestA.foo',
  122. '/K/dirJ/subtestA.foo',
  123. '/parameters.yml',
  124. '/parameters.yml.dist',
  125. '/prefixB.foo',
  126. '/prefixD.foo',
  127. '/prefixE.foo',
  128. '/prefixF.foo',
  129. '/toplevelA.foo',
  130. '/toplevelB.foo',
  131. ));
  132. }
  133. public function testGitExcludes()
  134. {
  135. // Ensure that git is available for testing.
  136. if (!$this->isProcessAvailable('git')) {
  137. return $this->markTestSkipped('git is not available.');
  138. }
  139. file_put_contents($this->sources.'/.gitignore', implode("\n", array(
  140. '# gitignore rules with comments and blank lines',
  141. '',
  142. 'prefixE.foo',
  143. '# and more',
  144. '# comments',
  145. '',
  146. '!/prefixE.foo',
  147. '/prefixD.foo',
  148. 'prefixF.*',
  149. '!/*/*/prefixF.foo',
  150. '',
  151. 'refixD.foo',
  152. '/C',
  153. 'D/prefixA',
  154. 'E',
  155. 'F/',
  156. 'G/*',
  157. 'H/**',
  158. 'J/',
  159. 'parameters.yml',
  160. '\!important!.txt',
  161. '\#*',
  162. )));
  163. // git does not currently support negative git attributes
  164. file_put_contents($this->sources.'/.gitattributes', implode("\n", array(
  165. '',
  166. '# gitattributes rules with comments and blank lines',
  167. 'prefixB.foo export-ignore',
  168. //'!/prefixB.foo export-ignore',
  169. '/prefixA.foo export-ignore',
  170. 'prefixC.* export-ignore',
  171. //'!/*/*/prefixC.foo export-ignore',
  172. )));
  173. $this->finder = new ArchivableFilesFinder($this->sources, array());
  174. $this->assertArchivableFiles($this->getArchivedFiles('git init && '.
  175. 'git config user.email "you@example.com" && '.
  176. 'git config user.name "Your Name" && '.
  177. 'git add .git* && '.
  178. 'git commit -m "ignore rules" && '.
  179. 'git add . && '.
  180. 'git commit -m "init" && '.
  181. 'git archive --format=zip --prefix=archive/ -o archive.zip HEAD'
  182. ));
  183. }
  184. public function testHgExcludes()
  185. {
  186. // Ensure that Mercurial is available for testing.
  187. if (!$this->isProcessAvailable('hg')) {
  188. return $this->markTestSkipped('Mercurial is not available.');
  189. }
  190. file_put_contents($this->sources.'/.hgignore', implode("\n", array(
  191. '# hgignore rules with comments, blank lines and syntax changes',
  192. '',
  193. 'pre*A.foo',
  194. 'prefixE.foo',
  195. '# and more',
  196. '# comments',
  197. '',
  198. '^prefixD.foo',
  199. 'D/prefixA',
  200. 'parameters.yml',
  201. '\!important!.txt',
  202. 'E',
  203. 'F/',
  204. 'syntax: glob',
  205. 'prefixF.*',
  206. 'B/*',
  207. 'H/**',
  208. )));
  209. $this->finder = new ArchivableFilesFinder($this->sources, array());
  210. $expectedFiles = $this->getArchivedFiles('hg init && '.
  211. 'hg add && '.
  212. 'hg commit -m "init" && '.
  213. 'hg archive archive.zip'
  214. );
  215. // Remove .hg_archival.txt from the expectedFiles
  216. $archiveKey = array_search('/.hg_archival.txt', $expectedFiles);
  217. array_splice($expectedFiles, $archiveKey, 1);
  218. $this->assertArchivableFiles($expectedFiles);
  219. }
  220. protected function getArchivableFiles()
  221. {
  222. $files = array();
  223. foreach ($this->finder as $file) {
  224. if (!$file->isDir()) {
  225. $files[] = preg_replace('#^'.preg_quote($this->sources, '#').'#', '', $this->fs->normalizePath($file->getRealPath()));
  226. }
  227. }
  228. sort($files);
  229. return $files;
  230. }
  231. protected function getArchivedFiles($command)
  232. {
  233. $process = new Process($command, $this->sources);
  234. $process->run();
  235. $archive = new \PharData($this->sources.'/archive.zip');
  236. $iterator = new \RecursiveIteratorIterator($archive);
  237. $files = array();
  238. foreach ($iterator as $file) {
  239. $files[] = preg_replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $this->fs->normalizePath($file));
  240. }
  241. unset($archive, $iterator, $file);
  242. unlink($this->sources.'/archive.zip');
  243. return $files;
  244. }
  245. protected function assertArchivableFiles($expectedFiles)
  246. {
  247. $actualFiles = $this->getArchivableFiles();
  248. $this->assertEquals($expectedFiles, $actualFiles);
  249. }
  250. /**
  251. * Check whether or not the given process is available.
  252. *
  253. * @param string $process The name of the binary to test.
  254. *
  255. * @return bool True if the process is available, false otherwise.
  256. */
  257. protected function isProcessAvailable($process)
  258. {
  259. $finder = new ExecutableFinder();
  260. return (bool) $finder->find($process);
  261. }
  262. }