GitDownloaderTest.php 30 KB

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