GitDownloaderTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 'git://github.com/mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'git://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. $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");
  114. $processExecutor->expects($this->at(2))
  115. ->method('execute')
  116. ->with($this->equalTo($expectedGitCommand))
  117. ->will($this->returnValue(0));
  118. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  119. $processExecutor->expects($this->at(3))
  120. ->method('execute')
  121. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  122. ->will($this->returnValue(0));
  123. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  124. $processExecutor->expects($this->at(4))
  125. ->method('execute')
  126. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  127. ->will($this->returnValue(0));
  128. $processExecutor->expects($this->at(5))
  129. ->method('execute')
  130. ->with($this->equalTo('git branch -r'))
  131. ->will($this->returnValue(0));
  132. $processExecutor->expects($this->at(6))
  133. ->method('execute')
  134. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  135. ->will($this->returnValue(0));
  136. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  137. $downloader->download($packageMock, 'composerPath');
  138. }
  139. public function pushUrlProvider()
  140. {
  141. return array(
  142. array('git', 'git@github.com:composer/composer.git'),
  143. array('https', 'https://github.com/composer/composer.git'),
  144. );
  145. }
  146. /**
  147. * @dataProvider pushUrlProvider
  148. */
  149. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocol, $pushUrl)
  150. {
  151. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  152. $packageMock->expects($this->any())
  153. ->method('getSourceReference')
  154. ->will($this->returnValue('ref'));
  155. $packageMock->expects($this->any())
  156. ->method('getSourceUrls')
  157. ->will($this->returnValue(array('https://github.com/composer/composer')));
  158. $packageMock->expects($this->any())
  159. ->method('getSourceUrl')
  160. ->will($this->returnValue('https://github.com/composer/composer'));
  161. $packageMock->expects($this->any())
  162. ->method('getPrettyVersion')
  163. ->will($this->returnValue('1.0.0'));
  164. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  165. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$protocol}://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer '{$protocol}://github.com/composer/composer' && git fetch composer");
  166. $processExecutor->expects($this->at(0))
  167. ->method('execute')
  168. ->with($this->equalTo($expectedGitCommand))
  169. ->will($this->returnValue(0));
  170. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  171. $processExecutor->expects($this->at(1))
  172. ->method('execute')
  173. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  174. ->will($this->returnValue(0));
  175. $processExecutor->expects($this->exactly(4))
  176. ->method('execute')
  177. ->will($this->returnValue(0));
  178. $config = new Config();
  179. $config->merge(array('config' => array('github-protocols' => array($protocol))));
  180. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  181. $downloader->download($packageMock, 'composerPath');
  182. }
  183. /**
  184. * @expectedException \RuntimeException
  185. */
  186. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  187. {
  188. $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");
  189. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  190. $packageMock->expects($this->any())
  191. ->method('getSourceReference')
  192. ->will($this->returnValue('ref'));
  193. $packageMock->expects($this->any())
  194. ->method('getSourceUrls')
  195. ->will($this->returnValue(array('https://example.com/composer/composer')));
  196. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  197. $processExecutor->expects($this->at(0))
  198. ->method('execute')
  199. ->with($this->equalTo($expectedGitCommand))
  200. ->will($this->returnValue(1));
  201. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  202. $downloader->download($packageMock, 'composerPath');
  203. }
  204. /**
  205. * @expectedException \InvalidArgumentException
  206. */
  207. public function testUpdateforPackageWithoutSourceReference()
  208. {
  209. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  210. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  211. $sourcePackageMock->expects($this->once())
  212. ->method('getSourceReference')
  213. ->will($this->returnValue(null));
  214. $downloader = $this->getDownloaderMock();
  215. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  216. }
  217. public function testUpdate()
  218. {
  219. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  220. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  221. $packageMock->expects($this->any())
  222. ->method('getSourceReference')
  223. ->will($this->returnValue('ref'));
  224. $packageMock->expects($this->any())
  225. ->method('getSourceUrls')
  226. ->will($this->returnValue(array('https://github.com/composer/composer')));
  227. $packageMock->expects($this->any())
  228. ->method('getPrettyVersion')
  229. ->will($this->returnValue('1.0.0'));
  230. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  231. $processExecutor->expects($this->at(0))
  232. ->method('execute')
  233. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  234. ->will($this->returnValue(0));
  235. $processExecutor->expects($this->at(1))
  236. ->method('execute')
  237. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  238. ->will($this->returnValue(0));
  239. $processExecutor->expects($this->at(2))
  240. ->method('execute')
  241. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  242. ->will($this->returnValue(0));
  243. $processExecutor->expects($this->at(3))
  244. ->method('execute')
  245. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  246. ->will($this->returnValue(0));
  247. $processExecutor->expects($this->at(4))
  248. ->method('execute')
  249. ->with($this->equalTo($this->winCompat("git remote -v")))
  250. ->will($this->returnValue(0));
  251. $processExecutor->expects($this->at(5))
  252. ->method('execute')
  253. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  254. ->will($this->returnValue(0));
  255. $processExecutor->expects($this->at(6))
  256. ->method('execute')
  257. ->with($this->equalTo('git branch -r'))
  258. ->will($this->returnValue(0));
  259. $processExecutor->expects($this->at(7))
  260. ->method('execute')
  261. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  262. ->will($this->returnValue(0));
  263. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  264. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  265. $downloader->update($packageMock, $packageMock, $this->workingDir);
  266. }
  267. /**
  268. * @group failing
  269. * @expectedException \RuntimeException
  270. */
  271. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  272. {
  273. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  274. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  275. $packageMock->expects($this->any())
  276. ->method('getSourceReference')
  277. ->will($this->returnValue('ref'));
  278. $packageMock->expects($this->any())
  279. ->method('getSourceUrls')
  280. ->will($this->returnValue(array('https://github.com/composer/composer')));
  281. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  282. $processExecutor->expects($this->at(0))
  283. ->method('execute')
  284. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  285. ->will($this->returnValue(0));
  286. $processExecutor->expects($this->at(1))
  287. ->method('execute')
  288. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  289. ->will($this->returnValue(0));
  290. $processExecutor->expects($this->at(2))
  291. ->method('execute')
  292. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  293. ->will($this->returnValue(0));
  294. $processExecutor->expects($this->at(3))
  295. ->method('execute')
  296. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  297. ->will($this->returnValue(0));
  298. $processExecutor->expects($this->at(4))
  299. ->method('execute')
  300. ->with($this->equalTo($this->winCompat("git remote -v")))
  301. ->will($this->returnValue(0));
  302. $processExecutor->expects($this->at(5))
  303. ->method('execute')
  304. ->with($this->equalTo($expectedGitUpdateCommand))
  305. ->will($this->returnValue(1));
  306. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  307. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  308. $downloader->update($packageMock, $packageMock, $this->workingDir);
  309. }
  310. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  311. {
  312. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '' && git fetch composer && git fetch --tags composer");
  313. $expectedSecondGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  314. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  315. $packageMock->expects($this->any())
  316. ->method('getSourceReference')
  317. ->will($this->returnValue('ref'));
  318. $packageMock->expects($this->any())
  319. ->method('getSourceUrls')
  320. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  321. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  322. $processExecutor->expects($this->at(0))
  323. ->method('execute')
  324. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  325. ->will($this->returnValue(0));
  326. $processExecutor->expects($this->at(1))
  327. ->method('execute')
  328. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  329. ->will($this->returnValue(0));
  330. $processExecutor->expects($this->at(2))
  331. ->method('execute')
  332. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  333. ->will($this->returnValue(0));
  334. $processExecutor->expects($this->at(3))
  335. ->method('execute')
  336. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  337. ->will($this->returnValue(0));
  338. $processExecutor->expects($this->at(4))
  339. ->method('execute')
  340. ->with($this->equalTo($this->winCompat("git remote -v")))
  341. ->will($this->returnValue(0));
  342. $processExecutor->expects($this->at(5))
  343. ->method('execute')
  344. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  345. ->will($this->returnValue(1));
  346. $processExecutor->expects($this->at(7))
  347. ->method('execute')
  348. ->with($this->equalTo($this->winCompat("git --version")))
  349. ->will($this->returnValue(0));
  350. $processExecutor->expects($this->at(8))
  351. ->method('execute')
  352. ->with($this->equalTo($this->winCompat("git remote -v")))
  353. ->will($this->returnValue(0));
  354. $processExecutor->expects($this->at(9))
  355. ->method('execute')
  356. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  357. ->will($this->returnValue(0));
  358. $processExecutor->expects($this->at(11))
  359. ->method('execute')
  360. ->with($this->equalTo('git branch -r'))
  361. ->will($this->returnValue(0));
  362. $processExecutor->expects($this->at(13))
  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 strtr($cmd, "'", '"');
  398. }
  399. return $cmd;
  400. }
  401. }