PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

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