GitDownloaderTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 'git@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. // ssh proto should use git@ all along
  147. array(array('ssh'), 'git@github.com:composer/composer', 'git@github.com:composer/composer.git'),
  148. // auto-proto uses git@ by default for push url, but not fetch
  149. array(array('https', 'ssh', 'git'), 'https://github.com/composer/composer', 'git@github.com:composer/composer.git'),
  150. // if restricted to https then push url is not overwritten to git@
  151. array(array('https'), 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  152. );
  153. }
  154. /**
  155. * @dataProvider pushUrlProvider
  156. */
  157. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocols, $url, $pushUrl)
  158. {
  159. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  160. $packageMock->expects($this->any())
  161. ->method('getSourceReference')
  162. ->will($this->returnValue('ref'));
  163. $packageMock->expects($this->any())
  164. ->method('getSourceUrls')
  165. ->will($this->returnValue(array('https://github.com/composer/composer')));
  166. $packageMock->expects($this->any())
  167. ->method('getSourceUrl')
  168. ->will($this->returnValue('https://github.com/composer/composer'));
  169. $packageMock->expects($this->any())
  170. ->method('getPrettyVersion')
  171. ->will($this->returnValue('1.0.0'));
  172. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  173. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer");
  174. $processExecutor->expects($this->at(0))
  175. ->method('execute')
  176. ->with($this->equalTo($expectedGitCommand))
  177. ->will($this->returnValue(0));
  178. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  179. $processExecutor->expects($this->at(1))
  180. ->method('execute')
  181. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  182. ->will($this->returnValue(0));
  183. $processExecutor->expects($this->exactly(4))
  184. ->method('execute')
  185. ->will($this->returnValue(0));
  186. $config = new Config();
  187. $config->merge(array('config' => array('github-protocols' => $protocols)));
  188. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  189. $downloader->download($packageMock, 'composerPath');
  190. }
  191. /**
  192. * @expectedException \RuntimeException
  193. */
  194. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  195. {
  196. $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");
  197. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  198. $packageMock->expects($this->any())
  199. ->method('getSourceReference')
  200. ->will($this->returnValue('ref'));
  201. $packageMock->expects($this->any())
  202. ->method('getSourceUrls')
  203. ->will($this->returnValue(array('https://example.com/composer/composer')));
  204. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  205. $processExecutor->expects($this->at(0))
  206. ->method('execute')
  207. ->with($this->equalTo($expectedGitCommand))
  208. ->will($this->returnValue(1));
  209. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  210. $downloader->download($packageMock, 'composerPath');
  211. }
  212. /**
  213. * @expectedException \InvalidArgumentException
  214. */
  215. public function testUpdateforPackageWithoutSourceReference()
  216. {
  217. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  218. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  219. $sourcePackageMock->expects($this->once())
  220. ->method('getSourceReference')
  221. ->will($this->returnValue(null));
  222. $downloader = $this->getDownloaderMock();
  223. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  224. }
  225. public function testUpdate()
  226. {
  227. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  228. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  229. $packageMock->expects($this->any())
  230. ->method('getSourceReference')
  231. ->will($this->returnValue('ref'));
  232. $packageMock->expects($this->any())
  233. ->method('getSourceUrls')
  234. ->will($this->returnValue(array('https://github.com/composer/composer')));
  235. $packageMock->expects($this->any())
  236. ->method('getPrettyVersion')
  237. ->will($this->returnValue('1.0.0'));
  238. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  239. $processExecutor->expects($this->at(0))
  240. ->method('execute')
  241. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  242. ->will($this->returnValue(0));
  243. $processExecutor->expects($this->at(1))
  244. ->method('execute')
  245. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  246. ->will($this->returnValue(0));
  247. $processExecutor->expects($this->at(2))
  248. ->method('execute')
  249. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  250. ->will($this->returnValue(0));
  251. $processExecutor->expects($this->at(3))
  252. ->method('execute')
  253. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  254. ->will($this->returnValue(0));
  255. $processExecutor->expects($this->at(4))
  256. ->method('execute')
  257. ->with($this->equalTo($this->winCompat("git remote -v")))
  258. ->will($this->returnValue(0));
  259. $processExecutor->expects($this->at(5))
  260. ->method('execute')
  261. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  262. ->will($this->returnValue(0));
  263. $processExecutor->expects($this->at(6))
  264. ->method('execute')
  265. ->with($this->equalTo('git branch -r'))
  266. ->will($this->returnValue(0));
  267. $processExecutor->expects($this->at(7))
  268. ->method('execute')
  269. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  270. ->will($this->returnValue(0));
  271. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  272. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  273. $downloader->update($packageMock, $packageMock, $this->workingDir);
  274. }
  275. /**
  276. * @group failing
  277. * @expectedException \RuntimeException
  278. */
  279. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  280. {
  281. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  282. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  283. $packageMock->expects($this->any())
  284. ->method('getSourceReference')
  285. ->will($this->returnValue('ref'));
  286. $packageMock->expects($this->any())
  287. ->method('getSourceUrls')
  288. ->will($this->returnValue(array('https://github.com/composer/composer')));
  289. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  290. $processExecutor->expects($this->at(0))
  291. ->method('execute')
  292. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  293. ->will($this->returnValue(0));
  294. $processExecutor->expects($this->at(1))
  295. ->method('execute')
  296. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  297. ->will($this->returnValue(0));
  298. $processExecutor->expects($this->at(2))
  299. ->method('execute')
  300. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  301. ->will($this->returnValue(0));
  302. $processExecutor->expects($this->at(3))
  303. ->method('execute')
  304. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  305. ->will($this->returnValue(0));
  306. $processExecutor->expects($this->at(4))
  307. ->method('execute')
  308. ->with($this->equalTo($this->winCompat("git remote -v")))
  309. ->will($this->returnValue(0));
  310. $processExecutor->expects($this->at(5))
  311. ->method('execute')
  312. ->with($this->equalTo($expectedGitUpdateCommand))
  313. ->will($this->returnValue(1));
  314. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  315. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  316. $downloader->update($packageMock, $packageMock, $this->workingDir);
  317. }
  318. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  319. {
  320. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '' && git fetch composer && git fetch --tags composer");
  321. $expectedSecondGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  322. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  323. $packageMock->expects($this->any())
  324. ->method('getSourceReference')
  325. ->will($this->returnValue('ref'));
  326. $packageMock->expects($this->any())
  327. ->method('getSourceUrls')
  328. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  329. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  330. $processExecutor->expects($this->at(0))
  331. ->method('execute')
  332. ->with($this->equalTo($this->winCompat("git rev-parse --abbrev-ref HEAD")))
  333. ->will($this->returnValue(0));
  334. $processExecutor->expects($this->at(1))
  335. ->method('execute')
  336. ->with($this->equalTo($this->winCompat("git rev-parse --verify composer/")))
  337. ->will($this->returnValue(0));
  338. $processExecutor->expects($this->at(2))
  339. ->method('execute')
  340. ->with($this->equalTo($this->winCompat("git diff --name-status composer/...")))
  341. ->will($this->returnValue(0));
  342. $processExecutor->expects($this->at(3))
  343. ->method('execute')
  344. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  345. ->will($this->returnValue(0));
  346. $processExecutor->expects($this->at(4))
  347. ->method('execute')
  348. ->with($this->equalTo($this->winCompat("git remote -v")))
  349. ->will($this->returnValue(0));
  350. $processExecutor->expects($this->at(5))
  351. ->method('execute')
  352. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  353. ->will($this->returnValue(1));
  354. $processExecutor->expects($this->at(7))
  355. ->method('execute')
  356. ->with($this->equalTo($this->winCompat("git --version")))
  357. ->will($this->returnValue(0));
  358. $processExecutor->expects($this->at(8))
  359. ->method('execute')
  360. ->with($this->equalTo($this->winCompat("git remote -v")))
  361. ->will($this->returnValue(0));
  362. $processExecutor->expects($this->at(9))
  363. ->method('execute')
  364. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  365. ->will($this->returnValue(0));
  366. $processExecutor->expects($this->at(11))
  367. ->method('execute')
  368. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  369. ->will($this->returnValue(0));
  370. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  371. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  372. $downloader->update($packageMock, $packageMock, $this->workingDir);
  373. }
  374. public function testRemove()
  375. {
  376. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  377. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  378. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  379. $processExecutor->expects($this->any())
  380. ->method('execute')
  381. ->with($this->equalTo($expectedGitResetCommand))
  382. ->will($this->returnValue(0));
  383. $filesystem = $this->getMock('Composer\Util\Filesystem');
  384. $filesystem->expects($this->any())
  385. ->method('removeDirectory')
  386. ->with($this->equalTo('composerPath'))
  387. ->will($this->returnValue(true));
  388. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  389. $downloader->remove($packageMock, 'composerPath');
  390. }
  391. public function testGetInstallationSource()
  392. {
  393. $downloader = $this->getDownloaderMock();
  394. $this->assertEquals('source', $downloader->getInstallationSource());
  395. }
  396. private function winCompat($cmd)
  397. {
  398. if (Platform::isWindows()) {
  399. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  400. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  401. return str_replace('""', '', strtr($cmd, "'", '"'));
  402. }
  403. return $cmd;
  404. }
  405. }