VcsRepositoryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Repository;
  12. use Composer\Test\TestCase;
  13. use Symfony\Component\Process\ExecutableFinder;
  14. use Composer\Package\Dumper\ArrayDumper;
  15. use Composer\Repository\VcsRepository;
  16. use Composer\Util\Filesystem;
  17. use Composer\Util\ProcessExecutor;
  18. use Composer\IO\NullIO;
  19. use Composer\Config;
  20. /**
  21. * @group slow
  22. */
  23. class VcsRepositoryTest extends TestCase
  24. {
  25. private static $composerHome;
  26. private static $gitRepo;
  27. private $skipped;
  28. protected function initialize()
  29. {
  30. $locator = new ExecutableFinder();
  31. if (!$locator->find('git')) {
  32. $this->skipped = 'This test needs a git binary in the PATH to be able to run';
  33. return;
  34. }
  35. $oldCwd = getcwd();
  36. self::$composerHome = $this->getUniqueTmpDirectory();
  37. self::$gitRepo = $this->getUniqueTmpDirectory();
  38. if (!@chdir(self::$gitRepo)) {
  39. $this->skipped = 'Could not create and move into the temp git repo '.self::$gitRepo;
  40. return;
  41. }
  42. // init
  43. $process = new ProcessExecutor;
  44. $exec = function ($command) use ($process) {
  45. $cwd = getcwd();
  46. if ($process->execute($command, $output, $cwd) !== 0) {
  47. throw new \RuntimeException('Failed to execute '.$command.': '.$process->getErrorOutput());
  48. }
  49. };
  50. $exec('git init');
  51. $exec('git config user.email composertest@example.org');
  52. $exec('git config user.name ComposerTest');
  53. $exec('git config commit.gpgsign false');
  54. touch('foo');
  55. $exec('git add foo');
  56. $exec('git commit -m init');
  57. // non-composed tag & branch
  58. $exec('git tag 0.5.0');
  59. $exec('git branch oldbranch');
  60. // add composed tag & master branch
  61. $composer = array('name' => 'a/b');
  62. file_put_contents('composer.json', json_encode($composer));
  63. $exec('git add composer.json');
  64. $exec('git commit -m addcomposer');
  65. $exec('git tag 0.6.0');
  66. // add feature-a branch
  67. $exec('git checkout -b feature/a-1.0-B');
  68. file_put_contents('foo', 'bar feature');
  69. $exec('git add foo');
  70. $exec('git commit -m change-a');
  71. // add version to composer.json
  72. $exec('git checkout master');
  73. $composer['version'] = '1.0.0';
  74. file_put_contents('composer.json', json_encode($composer));
  75. $exec('git add composer.json');
  76. $exec('git commit -m addversion');
  77. // create tag with wrong version in it
  78. $exec('git tag 0.9.0');
  79. // create tag with correct version in it
  80. $exec('git tag 1.0.0');
  81. // add feature-b branch
  82. $exec('git checkout -b feature-b');
  83. file_put_contents('foo', 'baz feature');
  84. $exec('git add foo');
  85. $exec('git commit -m change-b');
  86. // add 1.0 branch
  87. $exec('git checkout master');
  88. $exec('git branch 1.0');
  89. // add 1.0.x branch
  90. $exec('git branch 1.1.x');
  91. // update master to 2.0
  92. $composer['version'] = '2.0.0';
  93. file_put_contents('composer.json', json_encode($composer));
  94. $exec('git add composer.json');
  95. $exec('git commit -m bump-version');
  96. chdir($oldCwd);
  97. }
  98. public function setUp()
  99. {
  100. if (!self::$gitRepo) {
  101. $this->initialize();
  102. }
  103. if ($this->skipped) {
  104. $this->markTestSkipped($this->skipped);
  105. }
  106. }
  107. public static function tearDownAfterClass()
  108. {
  109. $fs = new Filesystem;
  110. $fs->removeDirectory(self::$composerHome);
  111. $fs->removeDirectory(self::$gitRepo);
  112. }
  113. public function testLoadVersions()
  114. {
  115. $expected = array(
  116. '0.6.0' => true,
  117. '1.0.0' => true,
  118. '1.0.x-dev' => true,
  119. '1.1.x-dev' => true,
  120. 'dev-feature-b' => true,
  121. 'dev-feature/a-1.0-B' => true,
  122. 'dev-master' => true,
  123. '9999999-dev' => true, // alias of dev-master
  124. );
  125. $config = new Config();
  126. $config->merge(array(
  127. 'config' => array(
  128. 'home' => self::$composerHome,
  129. ),
  130. ));
  131. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
  132. $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, $config, $httpDownloader);
  133. $packages = $repo->getPackages();
  134. $dumper = new ArrayDumper();
  135. foreach ($packages as $package) {
  136. if (isset($expected[$package->getPrettyVersion()])) {
  137. unset($expected[$package->getPrettyVersion()]);
  138. } else {
  139. $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
  140. }
  141. }
  142. $this->assertEmpty($expected, 'Missing versions: '.implode(', ', array_keys($expected)));
  143. }
  144. }