VcsRepositoryTest.php 5.0 KB

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