GitDownloaderTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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\Downloader;
  12. use Composer\Downloader\GitDownloader;
  13. use Composer\Config;
  14. use Composer\TestCase;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\Platform;
  17. class GitDownloaderTest extends TestCase
  18. {
  19. /** @var Filesystem */
  20. private $fs;
  21. /** @var string */
  22. private $workingDir;
  23. protected function setUp()
  24. {
  25. $this->fs = new Filesystem;
  26. $this->workingDir = $this->getUniqueTmpDirectory();
  27. }
  28. protected function tearDown()
  29. {
  30. if (is_dir($this->workingDir)) {
  31. $this->fs->removeDirectory($this->workingDir);
  32. }
  33. }
  34. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  35. {
  36. $io = $io ?: $this->getMock('Composer\IO\IOInterface');
  37. $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
  38. $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
  39. if (!$config) {
  40. $config = new Config();
  41. }
  42. return new GitDownloader($io, $config, $executor, $filesystem);
  43. }
  44. /**
  45. * @expectedException \InvalidArgumentException
  46. */
  47. public function testDownloadForPackageWithoutSourceReference()
  48. {
  49. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  50. $packageMock->expects($this->once())
  51. ->method('getSourceReference')
  52. ->will($this->returnValue(null));
  53. $downloader = $this->getDownloaderMock();
  54. $downloader->download($packageMock, '/path');
  55. }
  56. public function testDownload()
  57. {
  58. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  59. $packageMock->expects($this->any())
  60. ->method('getSourceReference')
  61. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  62. $packageMock->expects($this->any())
  63. ->method('getSourceUrls')
  64. ->will($this->returnValue(array('https://example.com/composer/composer')));
  65. $packageMock->expects($this->any())
  66. ->method('getSourceUrl')
  67. ->will($this->returnValue('https://example.com/composer/composer'));
  68. $packageMock->expects($this->any())
  69. ->method('getPrettyVersion')
  70. ->will($this->returnValue('dev-master'));
  71. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  72. $expectedGitCommand = $this->winCompat("git clone --no-checkout 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://example.com/composer/composer' && git fetch composer");
  73. $processExecutor->expects($this->at(0))
  74. ->method('execute')
  75. ->with($this->equalTo($expectedGitCommand))
  76. ->will($this->returnValue(0));
  77. $processExecutor->expects($this->at(1))
  78. ->method('execute')
  79. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  80. ->will($this->returnValue(0));
  81. $processExecutor->expects($this->at(2))
  82. ->method('execute')
  83. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  84. ->will($this->returnValue(0));
  85. $processExecutor->expects($this->at(3))
  86. ->method('execute')
  87. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  88. ->will($this->returnValue(0));
  89. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  90. $downloader->download($packageMock, 'composerPath');
  91. }
  92. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  93. {
  94. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  95. $packageMock->expects($this->any())
  96. ->method('getSourceReference')
  97. ->will($this->returnValue('ref'));
  98. $packageMock->expects($this->any())
  99. ->method('getSourceUrls')
  100. ->will($this->returnValue(array('https://github.com/mirrors/composer', 'https://github.com/composer/composer')));
  101. $packageMock->expects($this->any())
  102. ->method('getSourceUrl')
  103. ->will($this->returnValue('https://github.com/composer/composer'));
  104. $packageMock->expects($this->any())
  105. ->method('getPrettyVersion')
  106. ->will($this->returnValue('1.0.0'));
  107. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  108. $expectedGitCommand = $this->winCompat("git clone --no-checkout 'https://github.com/mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://github.com/mirrors/composer' && git fetch composer");
  109. $processExecutor->expects($this->at(0))
  110. ->method('execute')
  111. ->with($this->equalTo($expectedGitCommand))
  112. ->will($this->returnValue(1));
  113. $processExecutor->expects($this->at(1))
  114. ->method('getErrorOutput')
  115. ->with()
  116. ->will($this->returnValue('Error1'));
  117. $expectedGitCommand = $this->winCompat("git clone --no-checkout 'git@github.com:mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'git@github.com:mirrors/composer' && git fetch composer");
  118. $processExecutor->expects($this->at(2))
  119. ->method('execute')
  120. ->with($this->equalTo($expectedGitCommand))
  121. ->will($this->returnValue(0));
  122. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  123. $processExecutor->expects($this->at(3))
  124. ->method('execute')
  125. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  126. ->will($this->returnValue(0));
  127. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'https://github.com/composer/composer.git'");
  128. $processExecutor->expects($this->at(4))
  129. ->method('execute')
  130. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  131. ->will($this->returnValue(0));
  132. $processExecutor->expects($this->at(5))
  133. ->method('execute')
  134. ->with($this->equalTo('git branch -r'))
  135. ->will($this->returnValue(0));
  136. $processExecutor->expects($this->at(6))
  137. ->method('execute')
  138. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  139. ->will($this->returnValue(0));
  140. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  141. $downloader->download($packageMock, 'composerPath');
  142. }
  143. public function pushUrlProvider()
  144. {
  145. return array(
  146. array('ssh', 'git@github.com:composer/composer', 'https://github.com/composer/composer.git'),
  147. array('https', 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  148. );
  149. }
  150. /**
  151. * @dataProvider pushUrlProvider
  152. */
  153. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocol, $url, $pushUrl)
  154. {
  155. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  156. $packageMock->expects($this->any())
  157. ->method('getSourceReference')
  158. ->will($this->returnValue('ref'));
  159. $packageMock->expects($this->any())
  160. ->method('getSourceUrls')
  161. ->will($this->returnValue(array('https://github.com/composer/composer')));
  162. $packageMock->expects($this->any())
  163. ->method('getSourceUrl')
  164. ->will($this->returnValue('https://github.com/composer/composer'));
  165. $packageMock->expects($this->any())
  166. ->method('getPrettyVersion')
  167. ->will($this->returnValue('1.0.0'));
  168. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  169. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer");
  170. $processExecutor->expects($this->at(0))
  171. ->method('execute')
  172. ->with($this->equalTo($expectedGitCommand))
  173. ->will($this->returnValue(0));
  174. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  175. $processExecutor->expects($this->at(1))
  176. ->method('execute')
  177. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  178. ->will($this->returnValue(0));
  179. $processExecutor->expects($this->exactly(4))
  180. ->method('execute')
  181. ->will($this->returnValue(0));
  182. $config = new Config();
  183. $config->merge(array('config' => array('github-protocols' => array($protocol))));
  184. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  185. $downloader->download($packageMock, 'composerPath');
  186. }
  187. /**
  188. * @expectedException \RuntimeException
  189. */
  190. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  191. {
  192. $expectedGitCommand = $this->winCompat("git clone --no-checkout 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://example.com/composer/composer' && git fetch composer");
  193. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  194. $packageMock->expects($this->any())
  195. ->method('getSourceReference')
  196. ->will($this->returnValue('ref'));
  197. $packageMock->expects($this->any())
  198. ->method('getSourceUrls')
  199. ->will($this->returnValue(array('https://example.com/composer/composer')));
  200. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  201. $processExecutor->expects($this->at(0))
  202. ->method('execute')
  203. ->with($this->equalTo($expectedGitCommand))
  204. ->will($this->returnValue(1));
  205. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  206. $downloader->download($packageMock, 'composerPath');
  207. }
  208. /**
  209. * @expectedException \InvalidArgumentException
  210. */
  211. public function testUpdateforPackageWithoutSourceReference()
  212. {
  213. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  214. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  215. $sourcePackageMock->expects($this->once())
  216. ->method('getSourceReference')
  217. ->will($this->returnValue(null));
  218. $downloader = $this->getDownloaderMock();
  219. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  220. }
  221. public function testUpdate()
  222. {
  223. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  224. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  225. $packageMock->expects($this->any())
  226. ->method('getSourceReference')
  227. ->will($this->returnValue('ref'));
  228. $packageMock->expects($this->any())
  229. ->method('getSourceUrls')
  230. ->will($this->returnValue(array('https://github.com/composer/composer')));
  231. $packageMock->expects($this->any())
  232. ->method('getPrettyVersion')
  233. ->will($this->returnValue('1.0.0'));
  234. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  235. $processExecutor->expects($this->at(0))
  236. ->method('execute')
  237. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  238. ->will($this->returnValue(0));
  239. $processExecutor->expects($this->at(1))
  240. ->method('execute')
  241. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  242. ->will($this->returnValue(0));
  243. $processExecutor->expects($this->at(2))
  244. ->method('execute')
  245. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  246. ->will($this->returnValue(0));
  247. $processExecutor->expects($this->at(3))
  248. ->method('execute')
  249. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  250. ->will($this->returnValue(0));
  251. $processExecutor->expects($this->at(4))
  252. ->method('execute')
  253. ->with($this->equalTo($this->winCompat("git remote -v")))
  254. ->will($this->returnValue(0));
  255. $processExecutor->expects($this->at(5))
  256. ->method('execute')
  257. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  258. ->will($this->returnValue(0));
  259. $processExecutor->expects($this->at(6))
  260. ->method('execute')
  261. ->with($this->equalTo('git branch -r'))
  262. ->will($this->returnValue(0));
  263. $processExecutor->expects($this->at(7))
  264. ->method('execute')
  265. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  266. ->will($this->returnValue(0));
  267. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  268. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  269. $downloader->update($packageMock, $packageMock, $this->workingDir);
  270. }
  271. /**
  272. * @group failing
  273. * @expectedException \RuntimeException
  274. */
  275. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  276. {
  277. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  278. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  279. $packageMock->expects($this->any())
  280. ->method('getSourceReference')
  281. ->will($this->returnValue('ref'));
  282. $packageMock->expects($this->any())
  283. ->method('getSourceUrls')
  284. ->will($this->returnValue(array('https://github.com/composer/composer')));
  285. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  286. $processExecutor->expects($this->at(0))
  287. ->method('execute')
  288. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  289. ->will($this->returnValue(0));
  290. $processExecutor->expects($this->at(1))
  291. ->method('execute')
  292. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  293. ->will($this->returnValue(0));
  294. $processExecutor->expects($this->at(2))
  295. ->method('execute')
  296. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  297. ->will($this->returnValue(0));
  298. $processExecutor->expects($this->at(3))
  299. ->method('execute')
  300. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  301. ->will($this->returnValue(0));
  302. $processExecutor->expects($this->at(4))
  303. ->method('execute')
  304. ->with($this->equalTo($this->winCompat("git remote -v")))
  305. ->will($this->returnValue(0));
  306. $processExecutor->expects($this->at(5))
  307. ->method('execute')
  308. ->with($this->equalTo($expectedGitUpdateCommand))
  309. ->will($this->returnValue(1));
  310. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  311. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  312. $downloader->update($packageMock, $packageMock, $this->workingDir);
  313. }
  314. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  315. {
  316. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '' && git fetch composer && git fetch --tags composer");
  317. $expectedSecondGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  318. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  319. $packageMock->expects($this->any())
  320. ->method('getSourceReference')
  321. ->will($this->returnValue('ref'));
  322. $packageMock->expects($this->any())
  323. ->method('getSourceUrls')
  324. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  325. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  326. $processExecutor->expects($this->at(0))
  327. ->method('execute')
  328. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  329. ->will($this->returnValue(0));
  330. $processExecutor->expects($this->at(1))
  331. ->method('execute')
  332. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  333. ->will($this->returnValue(0));
  334. $processExecutor->expects($this->at(2))
  335. ->method('execute')
  336. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  337. ->will($this->returnValue(0));
  338. $processExecutor->expects($this->at(3))
  339. ->method('execute')
  340. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  341. ->will($this->returnValue(0));
  342. $processExecutor->expects($this->at(4))
  343. ->method('execute')
  344. ->with($this->equalTo($this->winCompat("git remote -v")))
  345. ->will($this->returnValue(0));
  346. $processExecutor->expects($this->at(5))
  347. ->method('execute')
  348. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  349. ->will($this->returnValue(1));
  350. $processExecutor->expects($this->at(7))
  351. ->method('execute')
  352. ->with($this->equalTo($this->winCompat("git --version")))
  353. ->will($this->returnValue(0));
  354. $processExecutor->expects($this->at(8))
  355. ->method('execute')
  356. ->with($this->equalTo($this->winCompat("git remote -v")))
  357. ->will($this->returnValue(0));
  358. $processExecutor->expects($this->at(9))
  359. ->method('execute')
  360. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  361. ->will($this->returnValue(0));
  362. $processExecutor->expects($this->at(11))
  363. ->method('execute')
  364. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  365. ->will($this->returnValue(0));
  366. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  367. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  368. $downloader->update($packageMock, $packageMock, $this->workingDir);
  369. }
  370. public function testRemove()
  371. {
  372. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  373. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  374. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  375. $processExecutor->expects($this->any())
  376. ->method('execute')
  377. ->with($this->equalTo($expectedGitResetCommand))
  378. ->will($this->returnValue(0));
  379. $filesystem = $this->getMock('Composer\Util\Filesystem');
  380. $filesystem->expects($this->any())
  381. ->method('removeDirectory')
  382. ->with($this->equalTo('composerPath'))
  383. ->will($this->returnValue(true));
  384. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  385. $downloader->remove($packageMock, 'composerPath');
  386. }
  387. public function testGetInstallationSource()
  388. {
  389. $downloader = $this->getDownloaderMock();
  390. $this->assertEquals('source', $downloader->getInstallationSource());
  391. }
  392. private function winCompat($cmd)
  393. {
  394. if (Platform::isWindows()) {
  395. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  396. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  397. return str_replace('""', '', strtr($cmd, "'", '"'));
  398. }
  399. return $cmd;
  400. }
  401. }