GitDownloaderTest.php 29 KB

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