PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/composer/composer/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php

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