GitDownloaderTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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->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");
  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. $expectedGitCommand = $this->winCompat(sprintf("git clone --mirror 'https://example.com/composer/composer' '%s'", $cachePath));
  140. $processExecutor->expects($this->at(1))
  141. ->method('execute')
  142. ->with($this->equalTo($expectedGitCommand))
  143. ->will($this->returnCallback(function () use ($cachePath) {
  144. @mkdir($cachePath, 0777, true);
  145. return 0;
  146. }));
  147. $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));
  148. $processExecutor->expects($this->at(2))
  149. ->method('execute')
  150. ->with($this->equalTo($expectedGitCommand))
  151. ->will($this->returnValue(0));
  152. $processExecutor->expects($this->at(3))
  153. ->method('execute')
  154. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  155. ->will($this->returnValue(0));
  156. $processExecutor->expects($this->at(4))
  157. ->method('execute')
  158. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  159. ->will($this->returnValue(0));
  160. $processExecutor->expects($this->at(5))
  161. ->method('execute')
  162. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  163. ->will($this->returnValue(0));
  164. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  165. $downloader->download($packageMock, 'composerPath');
  166. @rmdir($cachePath);
  167. }
  168. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  169. {
  170. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  171. $packageMock->expects($this->any())
  172. ->method('getSourceReference')
  173. ->will($this->returnValue('ref'));
  174. $packageMock->expects($this->any())
  175. ->method('getSourceUrls')
  176. ->will($this->returnValue(array('https://github.com/mirrors/composer', 'https://github.com/composer/composer')));
  177. $packageMock->expects($this->any())
  178. ->method('getSourceUrl')
  179. ->will($this->returnValue('https://github.com/composer/composer'));
  180. $packageMock->expects($this->any())
  181. ->method('getPrettyVersion')
  182. ->will($this->returnValue('1.0.0'));
  183. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  184. $processExecutor->expects($this->at(0))
  185. ->method('execute')
  186. ->with($this->equalTo($this->winCompat('git --version')))
  187. ->will($this->returnCallback(function ($command, &$output = null) {
  188. $output = 'git version 1.0.0';
  189. return 0;
  190. }));
  191. $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");
  192. $processExecutor->expects($this->at(1))
  193. ->method('execute')
  194. ->with($this->equalTo($expectedGitCommand))
  195. ->will($this->returnValue(1));
  196. $processExecutor->expects($this->at(2))
  197. ->method('getErrorOutput')
  198. ->with()
  199. ->will($this->returnValue('Error1'));
  200. $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");
  201. $processExecutor->expects($this->at(3))
  202. ->method('execute')
  203. ->with($this->equalTo($expectedGitCommand))
  204. ->will($this->returnValue(0));
  205. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  206. $processExecutor->expects($this->at(4))
  207. ->method('execute')
  208. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  209. ->will($this->returnValue(0));
  210. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  211. $processExecutor->expects($this->at(5))
  212. ->method('execute')
  213. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  214. ->will($this->returnValue(0));
  215. $processExecutor->expects($this->at(6))
  216. ->method('execute')
  217. ->with($this->equalTo('git branch -r'))
  218. ->will($this->returnValue(0));
  219. $processExecutor->expects($this->at(7))
  220. ->method('execute')
  221. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  222. ->will($this->returnValue(0));
  223. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  224. $downloader->download($packageMock, 'composerPath');
  225. }
  226. public function pushUrlProvider()
  227. {
  228. return array(
  229. // ssh proto should use git@ all along
  230. array(array('ssh'), 'git@github.com:composer/composer', 'git@github.com:composer/composer.git'),
  231. // auto-proto uses git@ by default for push url, but not fetch
  232. array(array('https', 'ssh', 'git'), 'https://github.com/composer/composer', 'git@github.com:composer/composer.git'),
  233. // if restricted to https then push url is not overwritten to git@
  234. array(array('https'), 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  235. );
  236. }
  237. /**
  238. * @dataProvider pushUrlProvider
  239. */
  240. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocols, $url, $pushUrl)
  241. {
  242. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  243. $packageMock->expects($this->any())
  244. ->method('getSourceReference')
  245. ->will($this->returnValue('ref'));
  246. $packageMock->expects($this->any())
  247. ->method('getSourceUrls')
  248. ->will($this->returnValue(array('https://github.com/composer/composer')));
  249. $packageMock->expects($this->any())
  250. ->method('getSourceUrl')
  251. ->will($this->returnValue('https://github.com/composer/composer'));
  252. $packageMock->expects($this->any())
  253. ->method('getPrettyVersion')
  254. ->will($this->returnValue('1.0.0'));
  255. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  256. $processExecutor->expects($this->at(0))
  257. ->method('execute')
  258. ->with($this->equalTo($this->winCompat('git --version')))
  259. ->will($this->returnCallback(function ($command, &$output = null) {
  260. $output = 'git version 1.0.0';
  261. return 0;
  262. }));
  263. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer");
  264. $processExecutor->expects($this->at(1))
  265. ->method('execute')
  266. ->with($this->equalTo($expectedGitCommand))
  267. ->will($this->returnValue(0));
  268. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  269. $processExecutor->expects($this->at(2))
  270. ->method('execute')
  271. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  272. ->will($this->returnValue(0));
  273. $processExecutor->expects($this->exactly(5))
  274. ->method('execute')
  275. ->will($this->returnValue(0));
  276. $config = new Config();
  277. $config->merge(array('config' => array('github-protocols' => $protocols)));
  278. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  279. $downloader->download($packageMock, 'composerPath');
  280. }
  281. /**
  282. * @expectedException \RuntimeException
  283. */
  284. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  285. {
  286. $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");
  287. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  288. $packageMock->expects($this->any())
  289. ->method('getSourceReference')
  290. ->will($this->returnValue('ref'));
  291. $packageMock->expects($this->any())
  292. ->method('getSourceUrls')
  293. ->will($this->returnValue(array('https://example.com/composer/composer')));
  294. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  295. $processExecutor->expects($this->at(0))
  296. ->method('execute')
  297. ->with($this->equalTo($this->winCompat('git --version')))
  298. ->will($this->returnCallback(function ($command, &$output = null) {
  299. $output = 'git version 1.0.0';
  300. return 0;
  301. }));
  302. $processExecutor->expects($this->at(1))
  303. ->method('execute')
  304. ->with($this->equalTo($expectedGitCommand))
  305. ->will($this->returnValue(1));
  306. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  307. $downloader->download($packageMock, 'composerPath');
  308. }
  309. /**
  310. * @expectedException \InvalidArgumentException
  311. */
  312. public function testUpdateforPackageWithoutSourceReference()
  313. {
  314. $initialPackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  315. $sourcePackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  316. $sourcePackageMock->expects($this->once())
  317. ->method('getSourceReference')
  318. ->will($this->returnValue(null));
  319. $downloader = $this->getDownloaderMock();
  320. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  321. }
  322. public function testUpdate()
  323. {
  324. $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)");
  325. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  326. $packageMock->expects($this->any())
  327. ->method('getSourceReference')
  328. ->will($this->returnValue('ref'));
  329. $packageMock->expects($this->any())
  330. ->method('getSourceUrls')
  331. ->will($this->returnValue(array('https://github.com/composer/composer')));
  332. $packageMock->expects($this->any())
  333. ->method('getPrettyVersion')
  334. ->will($this->returnValue('1.0.0'));
  335. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  336. $processExecutor->expects($this->at(0))
  337. ->method('execute')
  338. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  339. ->will($this->returnValue(0));
  340. $processExecutor->expects($this->at(1))
  341. ->method('execute')
  342. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  343. ->will($this->returnValue(0));
  344. $processExecutor->expects($this->at(2))
  345. ->method('execute')
  346. ->with($this->equalTo($this->winCompat("git remote -v")))
  347. ->will($this->returnValue(0));
  348. $processExecutor->expects($this->at(3))
  349. ->method('execute')
  350. ->with($this->equalTo($this->winCompat("git remote -v")))
  351. ->will($this->returnValue(0));
  352. $processExecutor->expects($this->at(4))
  353. ->method('execute')
  354. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  355. ->will($this->returnValue(0));
  356. $processExecutor->expects($this->at(5))
  357. ->method('execute')
  358. ->with($this->equalTo('git branch -r'))
  359. ->will($this->returnValue(0));
  360. $processExecutor->expects($this->at(6))
  361. ->method('execute')
  362. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  363. ->will($this->returnValue(0));
  364. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  365. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  366. $downloader->update($packageMock, $packageMock, $this->workingDir);
  367. }
  368. public function testUpdateWithNewRepoUrl()
  369. {
  370. $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)");
  371. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  372. $packageMock->expects($this->any())
  373. ->method('getSourceReference')
  374. ->will($this->returnValue('ref'));
  375. $packageMock->expects($this->any())
  376. ->method('getSourceUrls')
  377. ->will($this->returnValue(array('https://github.com/composer/composer')));
  378. $packageMock->expects($this->any())
  379. ->method('getSourceUrl')
  380. ->will($this->returnValue('https://github.com/composer/composer'));
  381. $packageMock->expects($this->any())
  382. ->method('getPrettyVersion')
  383. ->will($this->returnValue('1.0.0'));
  384. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  385. $processExecutor->expects($this->at(0))
  386. ->method('execute')
  387. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  388. ->will($this->returnValue(0));
  389. $processExecutor->expects($this->at(1))
  390. ->method('execute')
  391. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  392. ->will($this->returnValue(0));
  393. $processExecutor->expects($this->at(2))
  394. ->method('execute')
  395. ->with($this->equalTo($this->winCompat("git remote -v")))
  396. ->will($this->returnCallback(function ($cmd, &$output, $cwd) {
  397. $output = 'origin https://github.com/old/url (fetch)
  398. origin https://github.com/old/url (push)
  399. composer https://github.com/old/url (fetch)
  400. composer https://github.com/old/url (push)
  401. ';
  402. return 0;
  403. }));
  404. $processExecutor->expects($this->at(3))
  405. ->method('execute')
  406. ->with($this->equalTo($this->winCompat("git remote -v")))
  407. ->will($this->returnValue(0));
  408. $processExecutor->expects($this->at(4))
  409. ->method('execute')
  410. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  411. ->will($this->returnValue(0));
  412. $processExecutor->expects($this->at(5))
  413. ->method('execute')
  414. ->with($this->equalTo('git branch -r'))
  415. ->will($this->returnValue(0));
  416. $processExecutor->expects($this->at(6))
  417. ->method('execute')
  418. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  419. ->will($this->returnValue(0));
  420. $processExecutor->expects($this->at(7))
  421. ->method('execute')
  422. ->with($this->equalTo($this->winCompat("git remote set-url origin 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  423. ->will($this->returnValue(0));
  424. $processExecutor->expects($this->at(8))
  425. ->method('execute')
  426. ->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)))
  427. ->will($this->returnValue(0));
  428. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  429. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  430. $downloader->update($packageMock, $packageMock, $this->workingDir);
  431. }
  432. /**
  433. * @group failing
  434. * @expectedException \RuntimeException
  435. */
  436. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  437. {
  438. $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)");
  439. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  440. $packageMock->expects($this->any())
  441. ->method('getSourceReference')
  442. ->will($this->returnValue('ref'));
  443. $packageMock->expects($this->any())
  444. ->method('getSourceUrls')
  445. ->will($this->returnValue(array('https://github.com/composer/composer')));
  446. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  447. $processExecutor->expects($this->at(0))
  448. ->method('execute')
  449. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  450. ->will($this->returnValue(0));
  451. $processExecutor->expects($this->at(1))
  452. ->method('execute')
  453. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  454. ->will($this->returnValue(0));
  455. $processExecutor->expects($this->at(2))
  456. ->method('execute')
  457. ->with($this->equalTo($this->winCompat("git remote -v")))
  458. ->will($this->returnValue(0));
  459. $processExecutor->expects($this->at(3))
  460. ->method('execute')
  461. ->with($this->equalTo($this->winCompat("git remote -v")))
  462. ->will($this->returnValue(0));
  463. $processExecutor->expects($this->at(4))
  464. ->method('execute')
  465. ->with($this->equalTo($expectedGitUpdateCommand))
  466. ->will($this->returnValue(1));
  467. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  468. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  469. $downloader->update($packageMock, $packageMock, $this->workingDir);
  470. }
  471. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  472. {
  473. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  474. $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)");
  475. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  476. $packageMock->expects($this->any())
  477. ->method('getSourceReference')
  478. ->will($this->returnValue('ref'));
  479. $packageMock->expects($this->any())
  480. ->method('getSourceUrls')
  481. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  482. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  483. $processExecutor->expects($this->at(0))
  484. ->method('execute')
  485. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  486. ->will($this->returnValue(0));
  487. $processExecutor->expects($this->at(1))
  488. ->method('execute')
  489. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  490. ->will($this->returnValue(0));
  491. $processExecutor->expects($this->at(2))
  492. ->method('execute')
  493. ->with($this->equalTo($this->winCompat("git remote -v")))
  494. ->will($this->returnValue(0));
  495. $processExecutor->expects($this->at(3))
  496. ->method('execute')
  497. ->with($this->equalTo($this->winCompat("git remote -v")))
  498. ->will($this->returnValue(0));
  499. $processExecutor->expects($this->at(4))
  500. ->method('execute')
  501. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  502. ->will($this->returnValue(1));
  503. $processExecutor->expects($this->at(6))
  504. ->method('execute')
  505. ->with($this->equalTo($this->winCompat("git --version")))
  506. ->will($this->returnValue(0));
  507. $processExecutor->expects($this->at(7))
  508. ->method('execute')
  509. ->with($this->equalTo($this->winCompat("git remote -v")))
  510. ->will($this->returnValue(0));
  511. $processExecutor->expects($this->at(8))
  512. ->method('execute')
  513. ->with($this->equalTo($this->winCompat("git remote -v")))
  514. ->will($this->returnValue(0));
  515. $processExecutor->expects($this->at(9))
  516. ->method('execute')
  517. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  518. ->will($this->returnValue(0));
  519. $processExecutor->expects($this->at(11))
  520. ->method('execute')
  521. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  522. ->will($this->returnValue(0));
  523. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  524. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  525. $downloader->update($packageMock, $packageMock, $this->workingDir);
  526. }
  527. public function testRemove()
  528. {
  529. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  530. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  531. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  532. $processExecutor->expects($this->any())
  533. ->method('execute')
  534. ->with($this->equalTo($expectedGitResetCommand))
  535. ->will($this->returnValue(0));
  536. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  537. $filesystem->expects($this->any())
  538. ->method('removeDirectory')
  539. ->with($this->equalTo('composerPath'))
  540. ->will($this->returnValue(true));
  541. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  542. $downloader->remove($packageMock, 'composerPath');
  543. }
  544. public function testGetInstallationSource()
  545. {
  546. $downloader = $this->getDownloaderMock();
  547. $this->assertEquals('source', $downloader->getInstallationSource());
  548. }
  549. private function winCompat($cmd)
  550. {
  551. if (Platform::isWindows()) {
  552. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  553. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  554. return str_replace('""', '', strtr($cmd, "'", '"'));
  555. }
  556. return $cmd;
  557. }
  558. }