GitDownloaderTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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\Test\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->skipIfNotExecutable('git');
  26. $this->fs = new Filesystem;
  27. $this->workingDir = $this->getUniqueTmpDirectory();
  28. }
  29. protected function tearDown()
  30. {
  31. if (is_dir($this->workingDir)) {
  32. $this->fs->removeDirectory($this->workingDir);
  33. }
  34. // reset the static version cache
  35. $refl = new \ReflectionProperty('Composer\Util\Git', 'version');
  36. $refl->setAccessible(true);
  37. $refl->setValue(null, null);
  38. }
  39. protected function setupConfig($config = null)
  40. {
  41. if (!$config) {
  42. $config = new Config();
  43. }
  44. if (!$config->has('home')) {
  45. $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
  46. $config->merge(array('config' => array('home' => $tmpDir)));
  47. }
  48. return $config;
  49. }
  50. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  51. {
  52. $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  53. $executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  54. $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  55. $config = $this->setupConfig($config);
  56. return new GitDownloader($io, $config, $executor, $filesystem);
  57. }
  58. /**
  59. * @expectedException \InvalidArgumentException
  60. */
  61. public function testDownloadForPackageWithoutSourceReference()
  62. {
  63. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  64. $packageMock->expects($this->once())
  65. ->method('getSourceReference')
  66. ->will($this->returnValue(null));
  67. $downloader = $this->getDownloaderMock();
  68. $downloader->download($packageMock, '/path');
  69. }
  70. public function testDownload()
  71. {
  72. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  73. $packageMock->expects($this->any())
  74. ->method('getSourceReference')
  75. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  76. $packageMock->expects($this->any())
  77. ->method('getSourceUrls')
  78. ->will($this->returnValue(array('https://example.com/composer/composer')));
  79. $packageMock->expects($this->any())
  80. ->method('getSourceUrl')
  81. ->will($this->returnValue('https://example.com/composer/composer'));
  82. $packageMock->expects($this->any())
  83. ->method('getPrettyVersion')
  84. ->will($this->returnValue('dev-master'));
  85. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  86. $processExecutor->expects($this->at(0))
  87. ->method('execute')
  88. ->with($this->equalTo($this->winCompat('git --version')))
  89. ->will($this->returnCallback(function ($command, &$output = null) {
  90. $output = 'git version 1.0.0';
  91. return 0;
  92. }));
  93. $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 && git remote set-url origin 'https://example.com/composer/composer' && git remote set-url composer 'https://example.com/composer/composer'");
  94. $processExecutor->expects($this->at(1))
  95. ->method('execute')
  96. ->with($this->equalTo($expectedGitCommand))
  97. ->will($this->returnValue(0));
  98. $processExecutor->expects($this->at(2))
  99. ->method('execute')
  100. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  101. ->will($this->returnValue(0));
  102. $processExecutor->expects($this->at(3))
  103. ->method('execute')
  104. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  105. ->will($this->returnValue(0));
  106. $processExecutor->expects($this->at(4))
  107. ->method('execute')
  108. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  109. ->will($this->returnValue(0));
  110. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  111. $downloader->download($packageMock, 'composerPath');
  112. }
  113. public function testDownloadWithCache()
  114. {
  115. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  116. $packageMock->expects($this->any())
  117. ->method('getSourceReference')
  118. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  119. $packageMock->expects($this->any())
  120. ->method('getSourceUrls')
  121. ->will($this->returnValue(array('https://example.com/composer/composer')));
  122. $packageMock->expects($this->any())
  123. ->method('getSourceUrl')
  124. ->will($this->returnValue('https://example.com/composer/composer'));
  125. $packageMock->expects($this->any())
  126. ->method('getPrettyVersion')
  127. ->will($this->returnValue('dev-master'));
  128. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  129. $processExecutor->expects($this->at(0))
  130. ->method('execute')
  131. ->with($this->equalTo($this->winCompat('git --version')))
  132. ->will($this->returnCallback(function ($command, &$output = null) {
  133. $output = 'git version 2.3.1';
  134. return 0;
  135. }));
  136. $config = new Config;
  137. $this->setupConfig($config);
  138. $cachePath = $config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', 'https://example.com/composer/composer').'/';
  139. $filesystem = new \Composer\Util\Filesystem;
  140. $filesystem->removeDirectory($cachePath);
  141. $expectedGitCommand = $this->winCompat(sprintf("git clone --mirror 'https://example.com/composer/composer' '%s'", $cachePath));
  142. $processExecutor->expects($this->at(1))
  143. ->method('execute')
  144. ->with($this->equalTo($expectedGitCommand))
  145. ->will($this->returnCallback(function () use ($cachePath) {
  146. @mkdir($cachePath, 0777, true);
  147. return 0;
  148. }));
  149. $processExecutor->expects($this->at(2))
  150. ->method('execute')
  151. ->with($this->equalTo('git rev-parse --git-dir'), $this->anything(), $this->equalTo($this->winCompat($cachePath)))
  152. ->will($this->returnCallback(function ($command, &$output = null) {
  153. $output = '.';
  154. return 0;
  155. }));
  156. $processExecutor->expects($this->at(3))
  157. ->method('execute')
  158. ->with($this->equalTo($this->winCompat('git rev-parse --quiet --verify \'1234567890123456789012345678901234567890^{commit}\'')), $this->equalTo(null), $this->equalTo($this->winCompat($cachePath)))
  159. ->will($this->returnValue(0));
  160. $expectedGitCommand = $this->winCompat(sprintf("git clone --no-checkout '%1\$s' 'composerPath' --dissociate --reference '%1\$s' && cd 'composerPath' && git remote set-url origin 'https://example.com/composer/composer' && git remote add composer 'https://example.com/composer/composer'", $cachePath));
  161. $processExecutor->expects($this->at(4))
  162. ->method('execute')
  163. ->with($this->equalTo($expectedGitCommand))
  164. ->will($this->returnValue(0));
  165. $processExecutor->expects($this->at(5))
  166. ->method('execute')
  167. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  168. ->will($this->returnValue(0));
  169. $processExecutor->expects($this->at(6))
  170. ->method('execute')
  171. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  172. ->will($this->returnValue(0));
  173. $processExecutor->expects($this->at(7))
  174. ->method('execute')
  175. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  176. ->will($this->returnValue(0));
  177. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  178. $downloader->download($packageMock, 'composerPath');
  179. @rmdir($cachePath);
  180. }
  181. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  182. {
  183. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  184. $packageMock->expects($this->any())
  185. ->method('getSourceReference')
  186. ->will($this->returnValue('ref'));
  187. $packageMock->expects($this->any())
  188. ->method('getSourceUrls')
  189. ->will($this->returnValue(array('https://github.com/mirrors/composer', 'https://github.com/composer/composer')));
  190. $packageMock->expects($this->any())
  191. ->method('getSourceUrl')
  192. ->will($this->returnValue('https://github.com/composer/composer'));
  193. $packageMock->expects($this->any())
  194. ->method('getPrettyVersion')
  195. ->will($this->returnValue('1.0.0'));
  196. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  197. $processExecutor->expects($this->at(0))
  198. ->method('execute')
  199. ->with($this->equalTo($this->winCompat('git --version')))
  200. ->will($this->returnCallback(function ($command, &$output = null) {
  201. $output = 'git version 1.0.0';
  202. return 0;
  203. }));
  204. $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 && git remote set-url origin 'https://github.com/mirrors/composer' && git remote set-url composer 'https://github.com/mirrors/composer'");
  205. $processExecutor->expects($this->at(1))
  206. ->method('execute')
  207. ->with($this->equalTo($expectedGitCommand))
  208. ->will($this->returnValue(1));
  209. $processExecutor->expects($this->at(2))
  210. ->method('getErrorOutput')
  211. ->with()
  212. ->will($this->returnValue('Error1'));
  213. $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 && git remote set-url origin 'git@github.com:mirrors/composer' && git remote set-url composer 'git@github.com:mirrors/composer'");
  214. $processExecutor->expects($this->at(3))
  215. ->method('execute')
  216. ->with($this->equalTo($expectedGitCommand))
  217. ->will($this->returnValue(0));
  218. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  219. $processExecutor->expects($this->at(4))
  220. ->method('execute')
  221. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  222. ->will($this->returnValue(0));
  223. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  224. $processExecutor->expects($this->at(5))
  225. ->method('execute')
  226. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  227. ->will($this->returnValue(0));
  228. $processExecutor->expects($this->at(6))
  229. ->method('execute')
  230. ->with($this->equalTo('git branch -r'))
  231. ->will($this->returnValue(0));
  232. $processExecutor->expects($this->at(7))
  233. ->method('execute')
  234. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  235. ->will($this->returnValue(0));
  236. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  237. $downloader->download($packageMock, 'composerPath');
  238. }
  239. public function pushUrlProvider()
  240. {
  241. return array(
  242. // ssh proto should use git@ all along
  243. array(array('ssh'), 'git@github.com:composer/composer', 'git@github.com:composer/composer.git'),
  244. // auto-proto uses git@ by default for push url, but not fetch
  245. array(array('https', 'ssh', 'git'), 'https://github.com/composer/composer', 'git@github.com:composer/composer.git'),
  246. // if restricted to https then push url is not overwritten to git@
  247. array(array('https'), 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  248. );
  249. }
  250. /**
  251. * @dataProvider pushUrlProvider
  252. */
  253. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocols, $url, $pushUrl)
  254. {
  255. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  256. $packageMock->expects($this->any())
  257. ->method('getSourceReference')
  258. ->will($this->returnValue('ref'));
  259. $packageMock->expects($this->any())
  260. ->method('getSourceUrls')
  261. ->will($this->returnValue(array('https://github.com/composer/composer')));
  262. $packageMock->expects($this->any())
  263. ->method('getSourceUrl')
  264. ->will($this->returnValue('https://github.com/composer/composer'));
  265. $packageMock->expects($this->any())
  266. ->method('getPrettyVersion')
  267. ->will($this->returnValue('1.0.0'));
  268. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  269. $processExecutor->expects($this->at(0))
  270. ->method('execute')
  271. ->with($this->equalTo($this->winCompat('git --version')))
  272. ->will($this->returnCallback(function ($command, &$output = null) {
  273. $output = 'git version 1.0.0';
  274. return 0;
  275. }));
  276. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer && git remote set-url origin '{$url}' && git remote set-url composer '{$url}'");
  277. $processExecutor->expects($this->at(1))
  278. ->method('execute')
  279. ->with($this->equalTo($expectedGitCommand))
  280. ->will($this->returnValue(0));
  281. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  282. $processExecutor->expects($this->at(2))
  283. ->method('execute')
  284. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  285. ->will($this->returnValue(0));
  286. $processExecutor->expects($this->exactly(5))
  287. ->method('execute')
  288. ->will($this->returnValue(0));
  289. $config = new Config();
  290. $config->merge(array('config' => array('github-protocols' => $protocols)));
  291. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  292. $downloader->download($packageMock, 'composerPath');
  293. }
  294. /**
  295. * @expectedException \RuntimeException
  296. */
  297. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  298. {
  299. $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 && git remote set-url origin 'https://example.com/composer/composer' && git remote set-url composer 'https://example.com/composer/composer'");
  300. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  301. $packageMock->expects($this->any())
  302. ->method('getSourceReference')
  303. ->will($this->returnValue('ref'));
  304. $packageMock->expects($this->any())
  305. ->method('getSourceUrls')
  306. ->will($this->returnValue(array('https://example.com/composer/composer')));
  307. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  308. $processExecutor->expects($this->at(0))
  309. ->method('execute')
  310. ->with($this->equalTo($this->winCompat('git --version')))
  311. ->will($this->returnCallback(function ($command, &$output = null) {
  312. $output = 'git version 1.0.0';
  313. return 0;
  314. }));
  315. $processExecutor->expects($this->at(1))
  316. ->method('execute')
  317. ->with($this->equalTo($expectedGitCommand))
  318. ->will($this->returnValue(1));
  319. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  320. $downloader->download($packageMock, 'composerPath');
  321. }
  322. /**
  323. * @expectedException \InvalidArgumentException
  324. */
  325. public function testUpdateforPackageWithoutSourceReference()
  326. {
  327. $initialPackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  328. $sourcePackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  329. $sourcePackageMock->expects($this->once())
  330. ->method('getSourceReference')
  331. ->will($this->returnValue(null));
  332. $downloader = $this->getDownloaderMock();
  333. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  334. }
  335. public function testUpdate()
  336. {
  337. $expectedGitUpdateCommand = $this->winCompat("(git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)) && git remote set-url composer 'https://github.com/composer/composer'");
  338. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  339. $packageMock->expects($this->any())
  340. ->method('getSourceReference')
  341. ->will($this->returnValue('ref'));
  342. $packageMock->expects($this->any())
  343. ->method('getSourceUrls')
  344. ->will($this->returnValue(array('https://github.com/composer/composer')));
  345. $packageMock->expects($this->any())
  346. ->method('getVersion')
  347. ->will($this->returnValue('1.0.0.0'));
  348. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  349. $processExecutor->expects($this->at(0))
  350. ->method('execute')
  351. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  352. ->will($this->returnValue(0));
  353. $processExecutor->expects($this->at(1))
  354. ->method('execute')
  355. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  356. ->will($this->returnValue(0));
  357. $processExecutor->expects($this->at(2))
  358. ->method('execute')
  359. ->with($this->equalTo($this->winCompat("git remote -v")))
  360. ->will($this->returnValue(0));
  361. $processExecutor->expects($this->at(3))
  362. ->method('execute')
  363. ->with($this->equalTo($this->winCompat("git remote -v")))
  364. ->will($this->returnValue(0));
  365. $processExecutor->expects($this->at(4))
  366. ->method('execute')
  367. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  368. ->will($this->returnValue(0));
  369. $processExecutor->expects($this->at(5))
  370. ->method('execute')
  371. ->with($this->equalTo('git branch -r'))
  372. ->will($this->returnValue(0));
  373. $processExecutor->expects($this->at(6))
  374. ->method('execute')
  375. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  376. ->will($this->returnValue(0));
  377. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  378. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  379. $downloader->update($packageMock, $packageMock, $this->workingDir);
  380. }
  381. public function testUpdateWithNewRepoUrl()
  382. {
  383. $expectedGitUpdateCommand = $this->winCompat("(git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)) && git remote set-url composer 'https://github.com/composer/composer'");
  384. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  385. $packageMock->expects($this->any())
  386. ->method('getSourceReference')
  387. ->will($this->returnValue('ref'));
  388. $packageMock->expects($this->any())
  389. ->method('getSourceUrls')
  390. ->will($this->returnValue(array('https://github.com/composer/composer')));
  391. $packageMock->expects($this->any())
  392. ->method('getSourceUrl')
  393. ->will($this->returnValue('https://github.com/composer/composer'));
  394. $packageMock->expects($this->any())
  395. ->method('getVersion')
  396. ->will($this->returnValue('1.0.0.0'));
  397. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  398. $processExecutor->expects($this->at(0))
  399. ->method('execute')
  400. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  401. ->will($this->returnValue(0));
  402. $processExecutor->expects($this->at(1))
  403. ->method('execute')
  404. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  405. ->will($this->returnValue(0));
  406. $processExecutor->expects($this->at(2))
  407. ->method('execute')
  408. ->with($this->equalTo($this->winCompat("git remote -v")))
  409. ->will($this->returnCallback(function ($cmd, &$output, $cwd) {
  410. $output = 'origin https://github.com/old/url (fetch)
  411. origin https://github.com/old/url (push)
  412. composer https://github.com/old/url (fetch)
  413. composer https://github.com/old/url (push)
  414. ';
  415. return 0;
  416. }));
  417. $processExecutor->expects($this->at(3))
  418. ->method('execute')
  419. ->with($this->equalTo($this->winCompat("git remote -v")))
  420. ->will($this->returnValue(0));
  421. $processExecutor->expects($this->at(4))
  422. ->method('execute')
  423. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  424. ->will($this->returnValue(0));
  425. $processExecutor->expects($this->at(5))
  426. ->method('execute')
  427. ->with($this->equalTo('git branch -r'))
  428. ->will($this->returnValue(0));
  429. $processExecutor->expects($this->at(6))
  430. ->method('execute')
  431. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  432. ->will($this->returnValue(0));
  433. $processExecutor->expects($this->at(7))
  434. ->method('execute')
  435. ->with($this->equalTo($this->winCompat("git remote set-url origin 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  436. ->will($this->returnValue(0));
  437. $processExecutor->expects($this->at(8))
  438. ->method('execute')
  439. ->with($this->equalTo($this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  440. ->will($this->returnValue(0));
  441. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  442. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  443. $downloader->update($packageMock, $packageMock, $this->workingDir);
  444. }
  445. /**
  446. * @group failing
  447. * @expectedException \RuntimeException
  448. */
  449. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  450. {
  451. $expectedGitUpdateCommand = $this->winCompat("(git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)) && git remote set-url composer 'https://github.com/composer/composer'");
  452. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  453. $packageMock->expects($this->any())
  454. ->method('getSourceReference')
  455. ->will($this->returnValue('ref'));
  456. $packageMock->expects($this->any())
  457. ->method('getSourceUrls')
  458. ->will($this->returnValue(array('https://github.com/composer/composer')));
  459. $packageMock->expects($this->any())
  460. ->method('getVersion')
  461. ->will($this->returnValue('1.0.0.0'));
  462. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  463. $processExecutor->expects($this->at(0))
  464. ->method('execute')
  465. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  466. ->will($this->returnValue(0));
  467. $processExecutor->expects($this->at(1))
  468. ->method('execute')
  469. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  470. ->will($this->returnValue(0));
  471. $processExecutor->expects($this->at(2))
  472. ->method('execute')
  473. ->with($this->equalTo($this->winCompat("git remote -v")))
  474. ->will($this->returnValue(0));
  475. $processExecutor->expects($this->at(3))
  476. ->method('execute')
  477. ->with($this->equalTo($this->winCompat("git remote -v")))
  478. ->will($this->returnValue(0));
  479. $processExecutor->expects($this->at(4))
  480. ->method('execute')
  481. ->with($this->equalTo($expectedGitUpdateCommand))
  482. ->will($this->returnValue(1));
  483. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  484. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  485. $downloader->update($packageMock, $packageMock, $this->workingDir);
  486. }
  487. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  488. {
  489. $expectedFirstGitUpdateCommand = $this->winCompat("(git remote set-url composer '' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)) && git remote set-url composer ''");
  490. $expectedSecondGitUpdateCommand = $this->winCompat("(git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)) && git remote set-url composer 'https://github.com/composer/composer'");
  491. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  492. $packageMock->expects($this->any())
  493. ->method('getSourceReference')
  494. ->will($this->returnValue('ref'));
  495. $packageMock->expects($this->any())
  496. ->method('getVersion')
  497. ->will($this->returnValue('1.0.0.0'));
  498. $packageMock->expects($this->any())
  499. ->method('getSourceUrls')
  500. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  501. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  502. $processExecutor->expects($this->at(0))
  503. ->method('execute')
  504. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  505. ->will($this->returnValue(0));
  506. $processExecutor->expects($this->at(1))
  507. ->method('execute')
  508. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  509. ->will($this->returnValue(0));
  510. $processExecutor->expects($this->at(2))
  511. ->method('execute')
  512. ->with($this->equalTo($this->winCompat("git remote -v")))
  513. ->will($this->returnValue(0));
  514. $processExecutor->expects($this->at(3))
  515. ->method('execute')
  516. ->with($this->equalTo($this->winCompat("git remote -v")))
  517. ->will($this->returnValue(0));
  518. $processExecutor->expects($this->at(4))
  519. ->method('execute')
  520. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  521. ->will($this->returnValue(1));
  522. $processExecutor->expects($this->at(6))
  523. ->method('execute')
  524. ->with($this->equalTo($this->winCompat("git --version")))
  525. ->will($this->returnValue(0));
  526. $processExecutor->expects($this->at(7))
  527. ->method('execute')
  528. ->with($this->equalTo($this->winCompat("git remote -v")))
  529. ->will($this->returnValue(0));
  530. $processExecutor->expects($this->at(8))
  531. ->method('execute')
  532. ->with($this->equalTo($this->winCompat("git remote -v")))
  533. ->will($this->returnValue(0));
  534. $processExecutor->expects($this->at(9))
  535. ->method('execute')
  536. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  537. ->will($this->returnValue(0));
  538. $processExecutor->expects($this->at(11))
  539. ->method('execute')
  540. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  541. ->will($this->returnValue(0));
  542. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  543. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  544. $downloader->update($packageMock, $packageMock, $this->workingDir);
  545. }
  546. public function testDowngradeShowsAppropriateMessage()
  547. {
  548. $oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  549. $oldPackage->expects($this->any())
  550. ->method('getVersion')
  551. ->will($this->returnValue('1.2.0.0'));
  552. $oldPackage->expects($this->any())
  553. ->method('getFullPrettyVersion')
  554. ->will($this->returnValue('1.2.0'));
  555. $oldPackage->expects($this->any())
  556. ->method('getSourceReference')
  557. ->will($this->returnValue('ref'));
  558. $oldPackage->expects($this->any())
  559. ->method('getSourceUrls')
  560. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  561. $newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  562. $newPackage->expects($this->any())
  563. ->method('getSourceReference')
  564. ->will($this->returnValue('ref'));
  565. $newPackage->expects($this->any())
  566. ->method('getSourceUrls')
  567. ->will($this->returnValue(array('https://github.com/composer/composer')));
  568. $newPackage->expects($this->any())
  569. ->method('getVersion')
  570. ->will($this->returnValue('1.0.0.0'));
  571. $newPackage->expects($this->any())
  572. ->method('getFullPrettyVersion')
  573. ->will($this->returnValue('1.0.0'));
  574. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  575. $processExecutor->expects($this->any())
  576. ->method('execute')
  577. ->will($this->returnValue(0));
  578. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  579. $ioMock->expects($this->at(0))
  580. ->method('writeError')
  581. ->with($this->stringContains('Downgrading'));
  582. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  583. $downloader = $this->getDownloaderMock($ioMock, null, $processExecutor);
  584. $downloader->update($oldPackage, $newPackage, $this->workingDir);
  585. }
  586. public function testNotUsingDowngradingWithReferences()
  587. {
  588. $oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  589. $oldPackage->expects($this->any())
  590. ->method('getVersion')
  591. ->will($this->returnValue('dev-ref'));
  592. $oldPackage->expects($this->any())
  593. ->method('getSourceReference')
  594. ->will($this->returnValue('ref'));
  595. $oldPackage->expects($this->any())
  596. ->method('getSourceUrls')
  597. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  598. $newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  599. $newPackage->expects($this->any())
  600. ->method('getSourceReference')
  601. ->will($this->returnValue('ref'));
  602. $newPackage->expects($this->any())
  603. ->method('getSourceUrls')
  604. ->will($this->returnValue(array('https://github.com/composer/composer')));
  605. $newPackage->expects($this->any())
  606. ->method('getVersion')
  607. ->will($this->returnValue('dev-ref2'));
  608. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  609. $processExecutor->expects($this->any())
  610. ->method('execute')
  611. ->will($this->returnValue(0));
  612. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  613. $ioMock->expects($this->at(0))
  614. ->method('writeError')
  615. ->with($this->stringContains('updating'));
  616. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  617. $downloader = $this->getDownloaderMock($ioMock, null, $processExecutor);
  618. $downloader->update($oldPackage, $newPackage, $this->workingDir);
  619. }
  620. public function testRemove()
  621. {
  622. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  623. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  624. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  625. $processExecutor->expects($this->any())
  626. ->method('execute')
  627. ->with($this->equalTo($expectedGitResetCommand))
  628. ->will($this->returnValue(0));
  629. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  630. $filesystem->expects($this->once())
  631. ->method('removeDirectory')
  632. ->with($this->equalTo('composerPath'))
  633. ->will($this->returnValue(true));
  634. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  635. $downloader->remove($packageMock, 'composerPath');
  636. }
  637. public function testGetInstallationSource()
  638. {
  639. $downloader = $this->getDownloaderMock();
  640. $this->assertEquals('source', $downloader->getInstallationSource());
  641. }
  642. private function winCompat($cmd)
  643. {
  644. if (Platform::isWindows()) {
  645. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  646. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  647. return strtr($cmd, "'", '"');
  648. }
  649. return $cmd;
  650. }
  651. }