GitDownloaderTest.php 29 KB

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