VersionGuesserTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\Package\Version;
  12. use Composer\Config;
  13. use Composer\Package\Version\VersionGuesser;
  14. use Composer\Semver\VersionParser;
  15. class VersionGuesserTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function setUp()
  18. {
  19. if (!function_exists('proc_open')) {
  20. $this->markTestSkipped('proc_open() is not available');
  21. }
  22. }
  23. public function testHgGuessVersionReturnsData()
  24. {
  25. $branch = 'default';
  26. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  27. ->setMethods(array('execute'))
  28. ->disableArgumentCloning()
  29. ->disableOriginalConstructor()
  30. ->getMock()
  31. ;
  32. $self = $this;
  33. $step = 0;
  34. $executor
  35. ->expects($this->at($step))
  36. ->method('execute')
  37. ->willReturnCallback(function ($command, &$output) use ($self) {
  38. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  39. return 128;
  40. })
  41. ;
  42. ++$step;
  43. $executor
  44. ->expects($this->at($step))
  45. ->method('execute')
  46. ->willReturnCallback(function ($command, &$output) use ($self) {
  47. $self->assertEquals('git describe --exact-match --tags', $command);
  48. return 128;
  49. })
  50. ;
  51. ++$step;
  52. $executor
  53. ->expects($this->at($step))
  54. ->method('execute')
  55. ->willReturnCallback(function ($command, &$output) use ($self) {
  56. $self->assertEquals('git log --pretty="%H" -n1 HEAD', $command);
  57. return 128;
  58. })
  59. ;
  60. ++$step;
  61. $executor
  62. ->expects($this->at($step))
  63. ->method('execute')
  64. ->willReturnCallback(function ($command, &$output) use ($self, $branch) {
  65. $self->assertEquals('hg branch', $command);
  66. $output = $branch;
  67. return 0;
  68. })
  69. ;
  70. $config = new Config;
  71. $config->merge(array('repositories' => array('packagist' => false)));
  72. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  73. $versionArray = $guesser->guessVersion(array(), 'dummy/path');
  74. $this->assertEquals('dev-' . $branch, $versionArray['version']);
  75. $this->assertEmpty($versionArray['commit']);
  76. }
  77. public function testGuessVersionReturnsData()
  78. {
  79. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  80. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  81. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  82. ->setMethods(array('execute'))
  83. ->disableArgumentCloning()
  84. ->disableOriginalConstructor()
  85. ->getMock()
  86. ;
  87. $self = $this;
  88. $executor
  89. ->expects($this->at(0))
  90. ->method('execute')
  91. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  92. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  93. $output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n";
  94. return 0;
  95. })
  96. ;
  97. $config = new Config;
  98. $config->merge(array('repositories' => array('packagist' => false)));
  99. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  100. $versionArray = $guesser->guessVersion(array(), 'dummy/path');
  101. $this->assertEquals("dev-master", $versionArray['version']);
  102. $this->assertEquals($commitHash, $versionArray['commit']);
  103. }
  104. public function testGuessVersionReadsAndRespectsFeatureBranchesConfigurationForArbitraryNaming()
  105. {
  106. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  107. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  108. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  109. ->setMethods(array('execute'))
  110. ->disableArgumentCloning()
  111. ->disableOriginalConstructor()
  112. ->getMock()
  113. ;
  114. $self = $this;
  115. $executor
  116. ->expects($this->at(0))
  117. ->method('execute')
  118. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  119. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  120. $output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  121. return 0;
  122. })
  123. ;
  124. $executor
  125. ->expects($this->at(1))
  126. ->method('execute')
  127. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  128. $self->assertEquals('git rev-list arbitrary..current', $command);
  129. $output = "$anotherCommitHash\n";
  130. return 0;
  131. })
  132. ;
  133. $config = new Config;
  134. $config->merge(array('repositories' => array('packagist' => false)));
  135. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  136. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'feature-branches' => array('arbitrary')), 'dummy/path');
  137. $this->assertEquals("dev-arbitrary", $versionArray['version']);
  138. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  139. }
  140. public function testDetachedHeadBecomesDevHash()
  141. {
  142. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  143. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  144. ->setMethods(array('execute'))
  145. ->disableArgumentCloning()
  146. ->disableOriginalConstructor()
  147. ->getMock()
  148. ;
  149. $self = $this;
  150. $executor
  151. ->expects($this->at(0))
  152. ->method('execute')
  153. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  154. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  155. $output = "* (no branch) $commitHash Commit message\n";
  156. return 0;
  157. })
  158. ;
  159. $config = new Config;
  160. $config->merge(array('repositories' => array('packagist' => false)));
  161. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  162. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  163. $this->assertEquals("dev-$commitHash", $versionData['version']);
  164. }
  165. public function testDetachedFetchHeadBecomesDevHashGit2()
  166. {
  167. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  168. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  169. ->setMethods(array('execute'))
  170. ->disableArgumentCloning()
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. $self = $this;
  174. $executor
  175. ->expects($this->at(0))
  176. ->method('execute')
  177. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  178. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  179. $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
  180. return 0;
  181. });
  182. $config = new Config;
  183. $config->merge(array('repositories' => array('packagist' => false)));
  184. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  185. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  186. $this->assertEquals("dev-$commitHash", $versionData['version']);
  187. }
  188. public function testDetachedCommitHeadBecomesDevHashGit2()
  189. {
  190. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  191. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  192. ->setMethods(array('execute'))
  193. ->disableArgumentCloning()
  194. ->disableOriginalConstructor()
  195. ->getMock();
  196. $self = $this;
  197. $executor
  198. ->expects($this->at(0))
  199. ->method('execute')
  200. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  201. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  202. $output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n";
  203. return 0;
  204. });
  205. $config = new Config;
  206. $config->merge(array('repositories' => array('packagist' => false)));
  207. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  208. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  209. $this->assertEquals("dev-$commitHash", $versionData['version']);
  210. }
  211. public function testTagBecomesVersion()
  212. {
  213. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  214. ->setMethods(array('execute'))
  215. ->disableArgumentCloning()
  216. ->disableOriginalConstructor()
  217. ->getMock()
  218. ;
  219. $self = $this;
  220. $executor
  221. ->expects($this->at(0))
  222. ->method('execute')
  223. ->willReturnCallback(function ($command, &$output) use ($self) {
  224. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  225. $output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
  226. return 0;
  227. })
  228. ;
  229. $executor
  230. ->expects($this->at(1))
  231. ->method('execute')
  232. ->willReturnCallback(function ($command, &$output) use ($self) {
  233. $self->assertEquals('git describe --exact-match --tags', $command);
  234. $output = "v2.0.5-alpha2";
  235. return 0;
  236. })
  237. ;
  238. $config = new Config;
  239. $config->merge(array('repositories' => array('packagist' => false)));
  240. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  241. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  242. $this->assertEquals("2.0.5.0-alpha2", $versionData['version']);
  243. }
  244. public function testInvalidTagBecomesVersion()
  245. {
  246. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  247. ->setMethods(array('execute'))
  248. ->disableArgumentCloning()
  249. ->disableOriginalConstructor()
  250. ->getMock()
  251. ;
  252. $self = $this;
  253. $executor
  254. ->expects($this->at(0))
  255. ->method('execute')
  256. ->willReturnCallback(function ($command, &$output) use ($self) {
  257. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  258. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  259. return 0;
  260. })
  261. ;
  262. $config = new Config;
  263. $config->merge(array('repositories' => array('packagist' => false)));
  264. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  265. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  266. $this->assertEquals("dev-foo", $versionData['version']);
  267. }
  268. }