VersionGuesserTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming()
  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', 'non-feature-branches' => array('arbitrary')), 'dummy/path');
  137. $this->assertEquals("dev-arbitrary", $versionArray['version']);
  138. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  139. }
  140. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex()
  141. {
  142. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  143. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  144. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  145. ->setMethods(array('execute'))
  146. ->disableArgumentCloning()
  147. ->disableOriginalConstructor()
  148. ->getMock()
  149. ;
  150. $self = $this;
  151. $executor
  152. ->expects($this->at(0))
  153. ->method('execute')
  154. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  155. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  156. $output = " latest-testing $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  157. return 0;
  158. })
  159. ;
  160. $executor
  161. ->expects($this->at(1))
  162. ->method('execute')
  163. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  164. $self->assertEquals('git rev-list latest-testing..current', $command);
  165. $output = "$anotherCommitHash\n";
  166. return 0;
  167. })
  168. ;
  169. $config = new Config;
  170. $config->merge(array('repositories' => array('packagist' => false)));
  171. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  172. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  173. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  174. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  175. }
  176. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch()
  177. {
  178. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  179. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  180. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  181. ->setMethods(array('execute'))
  182. ->disableArgumentCloning()
  183. ->disableOriginalConstructor()
  184. ->getMock()
  185. ;
  186. $self = $this;
  187. $executor
  188. ->expects($this->at(0))
  189. ->method('execute')
  190. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  191. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  192. $output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n";
  193. return 0;
  194. })
  195. ;
  196. $config = new Config;
  197. $config->merge(array('repositories' => array('packagist' => false)));
  198. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  199. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  200. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  201. $this->assertEquals($commitHash, $versionArray['commit']);
  202. }
  203. public function testDetachedHeadBecomesDevHash()
  204. {
  205. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  206. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  207. ->setMethods(array('execute'))
  208. ->disableArgumentCloning()
  209. ->disableOriginalConstructor()
  210. ->getMock()
  211. ;
  212. $self = $this;
  213. $executor
  214. ->expects($this->at(0))
  215. ->method('execute')
  216. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  217. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  218. $output = "* (no branch) $commitHash Commit message\n";
  219. return 0;
  220. })
  221. ;
  222. $config = new Config;
  223. $config->merge(array('repositories' => array('packagist' => false)));
  224. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  225. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  226. $this->assertEquals("dev-$commitHash", $versionData['version']);
  227. }
  228. public function testDetachedFetchHeadBecomesDevHashGit2()
  229. {
  230. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  231. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  232. ->setMethods(array('execute'))
  233. ->disableArgumentCloning()
  234. ->disableOriginalConstructor()
  235. ->getMock();
  236. $self = $this;
  237. $executor
  238. ->expects($this->at(0))
  239. ->method('execute')
  240. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  241. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  242. $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
  243. return 0;
  244. });
  245. $config = new Config;
  246. $config->merge(array('repositories' => array('packagist' => false)));
  247. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  248. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  249. $this->assertEquals("dev-$commitHash", $versionData['version']);
  250. }
  251. public function testDetachedCommitHeadBecomesDevHashGit2()
  252. {
  253. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  254. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  255. ->setMethods(array('execute'))
  256. ->disableArgumentCloning()
  257. ->disableOriginalConstructor()
  258. ->getMock();
  259. $self = $this;
  260. $executor
  261. ->expects($this->at(0))
  262. ->method('execute')
  263. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  264. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  265. $output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n";
  266. return 0;
  267. });
  268. $config = new Config;
  269. $config->merge(array('repositories' => array('packagist' => false)));
  270. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  271. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  272. $this->assertEquals("dev-$commitHash", $versionData['version']);
  273. }
  274. public function testTagBecomesVersion()
  275. {
  276. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  277. ->setMethods(array('execute'))
  278. ->disableArgumentCloning()
  279. ->disableOriginalConstructor()
  280. ->getMock()
  281. ;
  282. $self = $this;
  283. $executor
  284. ->expects($this->at(0))
  285. ->method('execute')
  286. ->willReturnCallback(function ($command, &$output) use ($self) {
  287. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  288. $output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
  289. return 0;
  290. })
  291. ;
  292. $executor
  293. ->expects($this->at(1))
  294. ->method('execute')
  295. ->willReturnCallback(function ($command, &$output) use ($self) {
  296. $self->assertEquals('git describe --exact-match --tags', $command);
  297. $output = "v2.0.5-alpha2";
  298. return 0;
  299. })
  300. ;
  301. $config = new Config;
  302. $config->merge(array('repositories' => array('packagist' => false)));
  303. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  304. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  305. $this->assertEquals("2.0.5.0-alpha2", $versionData['version']);
  306. }
  307. public function testInvalidTagBecomesVersion()
  308. {
  309. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  310. ->setMethods(array('execute'))
  311. ->disableArgumentCloning()
  312. ->disableOriginalConstructor()
  313. ->getMock()
  314. ;
  315. $self = $this;
  316. $executor
  317. ->expects($this->at(0))
  318. ->method('execute')
  319. ->willReturnCallback(function ($command, &$output) use ($self) {
  320. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  321. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  322. return 0;
  323. })
  324. ;
  325. $config = new Config;
  326. $config->merge(array('repositories' => array('packagist' => false)));
  327. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  328. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  329. $this->assertEquals("dev-foo", $versionData['version']);
  330. }
  331. }