GitDownloaderTest.php 29 KB

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