VcsRepositoryTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $oldCwd = getcwd();
  31. self::$composerHome = $this->getUniqueTmpDirectory();
  32. self::$gitRepo = $this->getUniqueTmpDirectory();
  33. $locator = new ExecutableFinder();
  34. if (!$locator->find('git')) {
  35. $this->skipped = 'This test needs a git binary in the PATH to be able to run';
  36. return;
  37. }
  38. if (!@mkdir(self::$gitRepo) || !@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. touch('foo');
  54. $exec('git add foo');
  55. $exec('git commit -m init');
  56. // non-composed tag & branch
  57. $exec('git tag 0.5.0');
  58. $exec('git branch oldbranch');
  59. // add composed tag & master branch
  60. $composer = array('name' => 'a/b');
  61. file_put_contents('composer.json', json_encode($composer));
  62. $exec('git add composer.json');
  63. $exec('git commit -m addcomposer');
  64. $exec('git tag 0.6.0');
  65. // add feature-a branch
  66. $exec('git checkout -b feature/a-1.0-B');
  67. file_put_contents('foo', 'bar feature');
  68. $exec('git add foo');
  69. $exec('git commit -m change-a');
  70. // add version to composer.json
  71. $exec('git checkout master');
  72. $composer['version'] = '1.0.0';
  73. file_put_contents('composer.json', json_encode($composer));
  74. $exec('git add composer.json');
  75. $exec('git commit -m addversion');
  76. // create tag with wrong version in it
  77. $exec('git tag 0.9.0');
  78. // create tag with correct version in it
  79. $exec('git tag 1.0.0');
  80. // add feature-b branch
  81. $exec('git checkout -b feature-b');
  82. file_put_contents('foo', 'baz feature');
  83. $exec('git add foo');
  84. $exec('git commit -m change-b');
  85. // add 1.0 branch
  86. $exec('git checkout master');
  87. $exec('git branch 1.0');
  88. // add 1.0.x branch
  89. $exec('git branch 1.1.x');
  90. // update master to 2.0
  91. $composer['version'] = '2.0.0';
  92. file_put_contents('composer.json', json_encode($composer));
  93. $exec('git add composer.json');
  94. $exec('git commit -m bump-version');
  95. chdir($oldCwd);
  96. }
  97. public function setUp()
  98. {
  99. if (!self::$gitRepo) {
  100. $this->initialize();
  101. }
  102. if ($this->skipped) {
  103. $this->markTestSkipped($this->skipped);
  104. }
  105. }
  106. public static function tearDownAfterClass()
  107. {
  108. $fs = new Filesystem;
  109. $fs->removeDirectory(self::$composerHome);
  110. $fs->removeDirectory(self::$gitRepo);
  111. }
  112. public function testLoadVersions()
  113. {
  114. $expected = array(
  115. '0.6.0' => true,
  116. '1.0.0' => true,
  117. '1.0.x-dev' => true,
  118. '1.1.x-dev' => true,
  119. 'dev-feature-b' => true,
  120. 'dev-feature/a-1.0-B' => true,
  121. 'dev-master' => true,
  122. );
  123. $config = new Config();
  124. $config->merge(array(
  125. 'config' => array(
  126. 'home' => self::$composerHome,
  127. ),
  128. ));
  129. $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, $config);
  130. $packages = $repo->getPackages();
  131. $dumper = new ArrayDumper();
  132. foreach ($packages as $package) {
  133. if (isset($expected[$package->getPrettyVersion()])) {
  134. unset($expected[$package->getPrettyVersion()]);
  135. } else {
  136. $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
  137. }
  138. }
  139. $this->assertEmpty($expected, 'Missing versions: '.implode(', ', array_keys($expected)));
  140. }
  141. }