VersionGuesserTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. use Composer\Test\TestCase;
  16. class VersionGuesserTest extends TestCase
  17. {
  18. public function setUp()
  19. {
  20. if (!function_exists('proc_open')) {
  21. $this->markTestSkipped('proc_open() is not available');
  22. }
  23. }
  24. public function testHgGuessVersionReturnsData()
  25. {
  26. $branch = 'default';
  27. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  28. ->setMethods(array('execute'))
  29. ->disableArgumentCloning()
  30. ->disableOriginalConstructor()
  31. ->getMock()
  32. ;
  33. $self = $this;
  34. $step = 0;
  35. $executor
  36. ->expects($this->at($step))
  37. ->method('execute')
  38. ->willReturnCallback(function ($command, &$output) use ($self) {
  39. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  40. return 128;
  41. })
  42. ;
  43. ++$step;
  44. $executor
  45. ->expects($this->at($step))
  46. ->method('execute')
  47. ->willReturnCallback(function ($command, &$output) use ($self) {
  48. $self->assertEquals('git describe --exact-match --tags', $command);
  49. return 128;
  50. })
  51. ;
  52. ++$step;
  53. $executor
  54. ->expects($this->at($step))
  55. ->method('execute')
  56. ->willReturnCallback(function ($command, &$output) use ($self) {
  57. $self->assertEquals('git log --pretty="%H" -n1 HEAD', $command);
  58. return 128;
  59. })
  60. ;
  61. ++$step;
  62. $executor
  63. ->expects($this->at($step))
  64. ->method('execute')
  65. ->willReturnCallback(function ($command, &$output) use ($self, $branch) {
  66. $self->assertEquals('hg branch', $command);
  67. $output = $branch;
  68. return 0;
  69. })
  70. ;
  71. $config = new Config;
  72. $config->merge(array('repositories' => array('packagist' => false)));
  73. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  74. $versionArray = $guesser->guessVersion(array(), 'dummy/path');
  75. $this->assertEquals("dev-".$branch, $versionArray['version']);
  76. $this->assertEquals("dev-".$branch, $versionArray['pretty_version']);
  77. $this->assertEmpty($versionArray['commit']);
  78. }
  79. public function testGuessVersionReturnsData()
  80. {
  81. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  82. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  83. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  84. ->setMethods(array('execute'))
  85. ->disableArgumentCloning()
  86. ->disableOriginalConstructor()
  87. ->getMock()
  88. ;
  89. $self = $this;
  90. $executor
  91. ->expects($this->at(0))
  92. ->method('execute')
  93. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  94. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  95. $output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n";
  96. return 0;
  97. })
  98. ;
  99. $config = new Config;
  100. $config->merge(array('repositories' => array('packagist' => false)));
  101. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  102. $versionArray = $guesser->guessVersion(array(), 'dummy/path');
  103. $this->assertEquals("dev-master", $versionArray['version']);
  104. $this->assertEquals("dev-master", $versionArray['pretty_version']);
  105. $this->assertArrayNotHasKey('feature_version', $versionArray);
  106. $this->assertArrayNotHasKey('feature_pretty_version', $versionArray);
  107. $this->assertEquals($commitHash, $versionArray['commit']);
  108. }
  109. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming()
  110. {
  111. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  112. $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
  113. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  114. ->setMethods(array('execute'))
  115. ->disableArgumentCloning()
  116. ->disableOriginalConstructor()
  117. ->getMock()
  118. ;
  119. $self = $this;
  120. $executor
  121. ->expects($this->at(0))
  122. ->method('execute')
  123. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  124. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  125. $output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  126. return 0;
  127. })
  128. ;
  129. $executor
  130. ->expects($this->at(1))
  131. ->method('execute')
  132. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  133. $self->assertEquals('git rev-list arbitrary..current', $command);
  134. $output = "$anotherCommitHash\n";
  135. return 0;
  136. })
  137. ;
  138. $config = new Config;
  139. $config->merge(array('repositories' => array('packagist' => false)));
  140. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  141. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('arbitrary')), 'dummy/path');
  142. $this->assertEquals("dev-arbitrary", $versionArray['version']);
  143. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  144. $this->assertEquals("dev-current", $versionArray['feature_version']);
  145. $this->assertEquals("dev-current", $versionArray['feature_pretty_version']);
  146. }
  147. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex()
  148. {
  149. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  150. $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
  151. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  152. ->setMethods(array('execute'))
  153. ->disableArgumentCloning()
  154. ->disableOriginalConstructor()
  155. ->getMock()
  156. ;
  157. $self = $this;
  158. $executor
  159. ->expects($this->at(0))
  160. ->method('execute')
  161. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  162. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  163. $output = " latest-testing $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  164. return 0;
  165. })
  166. ;
  167. $executor
  168. ->expects($this->at(1))
  169. ->method('execute')
  170. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  171. $self->assertEquals('git rev-list latest-testing..current', $command);
  172. $output = "$anotherCommitHash\n";
  173. return 0;
  174. })
  175. ;
  176. $config = new Config;
  177. $config->merge(array('repositories' => array('packagist' => false)));
  178. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  179. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  180. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  181. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  182. $this->assertEquals("dev-current", $versionArray['feature_version']);
  183. $this->assertEquals("dev-current", $versionArray['feature_pretty_version']);
  184. }
  185. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch()
  186. {
  187. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  188. $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea';
  189. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  190. ->setMethods(array('execute'))
  191. ->disableArgumentCloning()
  192. ->disableOriginalConstructor()
  193. ->getMock()
  194. ;
  195. $self = $this;
  196. $executor
  197. ->expects($this->at(0))
  198. ->method('execute')
  199. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  200. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  201. $output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n";
  202. return 0;
  203. })
  204. ;
  205. $config = new Config;
  206. $config->merge(array('repositories' => array('packagist' => false)));
  207. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  208. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  209. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  210. $this->assertEquals($commitHash, $versionArray['commit']);
  211. $this->assertArrayNotHasKey('feature_version', $versionArray);
  212. $this->assertArrayNotHasKey('feature_pretty_version', $versionArray);
  213. }
  214. public function testDetachedHeadBecomesDevHash()
  215. {
  216. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  217. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  218. ->setMethods(array('execute'))
  219. ->disableArgumentCloning()
  220. ->disableOriginalConstructor()
  221. ->getMock()
  222. ;
  223. $self = $this;
  224. $executor
  225. ->expects($this->at(0))
  226. ->method('execute')
  227. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  228. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  229. $output = "* (no branch) $commitHash Commit message\n";
  230. return 0;
  231. })
  232. ;
  233. $config = new Config;
  234. $config->merge(array('repositories' => array('packagist' => false)));
  235. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  236. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  237. $this->assertEquals("dev-$commitHash", $versionData['version']);
  238. }
  239. public function testDetachedFetchHeadBecomesDevHashGit2()
  240. {
  241. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  242. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  243. ->setMethods(array('execute'))
  244. ->disableArgumentCloning()
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. $self = $this;
  248. $executor
  249. ->expects($this->at(0))
  250. ->method('execute')
  251. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  252. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  253. $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
  254. return 0;
  255. });
  256. $config = new Config;
  257. $config->merge(array('repositories' => array('packagist' => false)));
  258. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  259. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  260. $this->assertEquals("dev-$commitHash", $versionData['version']);
  261. }
  262. public function testDetachedCommitHeadBecomesDevHashGit2()
  263. {
  264. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  265. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  266. ->setMethods(array('execute'))
  267. ->disableArgumentCloning()
  268. ->disableOriginalConstructor()
  269. ->getMock();
  270. $self = $this;
  271. $executor
  272. ->expects($this->at(0))
  273. ->method('execute')
  274. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  275. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  276. $output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n";
  277. return 0;
  278. });
  279. $config = new Config;
  280. $config->merge(array('repositories' => array('packagist' => false)));
  281. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  282. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  283. $this->assertEquals("dev-$commitHash", $versionData['version']);
  284. }
  285. public function testTagBecomesVersion()
  286. {
  287. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  288. ->setMethods(array('execute'))
  289. ->disableArgumentCloning()
  290. ->disableOriginalConstructor()
  291. ->getMock()
  292. ;
  293. $self = $this;
  294. $executor
  295. ->expects($this->at(0))
  296. ->method('execute')
  297. ->willReturnCallback(function ($command, &$output) use ($self) {
  298. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  299. $output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
  300. return 0;
  301. })
  302. ;
  303. $executor
  304. ->expects($this->at(1))
  305. ->method('execute')
  306. ->willReturnCallback(function ($command, &$output) use ($self) {
  307. $self->assertEquals('git describe --exact-match --tags', $command);
  308. $output = "v2.0.5-alpha2";
  309. return 0;
  310. })
  311. ;
  312. $config = new Config;
  313. $config->merge(array('repositories' => array('packagist' => false)));
  314. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  315. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  316. $this->assertEquals("2.0.5.0-alpha2", $versionData['version']);
  317. }
  318. public function testTagBecomesPrettyVersion()
  319. {
  320. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  321. ->setMethods(array('execute'))
  322. ->disableArgumentCloning()
  323. ->disableOriginalConstructor()
  324. ->getMock()
  325. ;
  326. $self = $this;
  327. $executor
  328. ->expects($this->at(0))
  329. ->method('execute')
  330. ->willReturnCallback(function ($command, &$output) use ($self) {
  331. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  332. $output = "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n";
  333. return 0;
  334. })
  335. ;
  336. $executor
  337. ->expects($this->at(1))
  338. ->method('execute')
  339. ->willReturnCallback(function ($command, &$output) use ($self) {
  340. $self->assertEquals('git describe --exact-match --tags', $command);
  341. $output = '1.0.0';
  342. return 0;
  343. })
  344. ;
  345. $config = new Config;
  346. $config->merge(array('repositories' => array('packagist' => false)));
  347. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  348. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  349. $this->assertEquals('1.0.0.0', $versionData['version']);
  350. $this->assertEquals('1.0.0', $versionData['pretty_version']);
  351. }
  352. public function testInvalidTagBecomesVersion()
  353. {
  354. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  355. ->setMethods(array('execute'))
  356. ->disableArgumentCloning()
  357. ->disableOriginalConstructor()
  358. ->getMock()
  359. ;
  360. $self = $this;
  361. $executor
  362. ->expects($this->at(0))
  363. ->method('execute')
  364. ->willReturnCallback(function ($command, &$output) use ($self) {
  365. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  366. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  367. return 0;
  368. })
  369. ;
  370. $config = new Config;
  371. $config->merge(array('repositories' => array('packagist' => false)));
  372. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  373. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  374. $this->assertEquals("dev-foo", $versionData['version']);
  375. }
  376. public function testNumericBranchesShowNicely()
  377. {
  378. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  379. ->setMethods(array('execute'))
  380. ->disableArgumentCloning()
  381. ->disableOriginalConstructor()
  382. ->getMock()
  383. ;
  384. $self = $this;
  385. $executor
  386. ->expects($this->at(0))
  387. ->method('execute')
  388. ->willReturnCallback(function ($command, &$output) use ($self) {
  389. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  390. $output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  391. return 0;
  392. })
  393. ;
  394. $config = new Config;
  395. $config->merge(array('repositories' => array('packagist' => false)));
  396. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  397. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  398. $this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
  399. $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
  400. }
  401. }