VersionGuesserTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 PHPUnit\Framework\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("9999999-dev", $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("9999999-dev", $versionArray['version']);
  104. $this->assertEquals("dev-master", $versionArray['pretty_version']);
  105. $this->assertEquals($commitHash, $versionArray['commit']);
  106. }
  107. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming()
  108. {
  109. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  110. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  111. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  112. ->setMethods(array('execute'))
  113. ->disableArgumentCloning()
  114. ->disableOriginalConstructor()
  115. ->getMock()
  116. ;
  117. $self = $this;
  118. $executor
  119. ->expects($this->at(0))
  120. ->method('execute')
  121. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  122. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  123. $output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  124. return 0;
  125. })
  126. ;
  127. $executor
  128. ->expects($this->at(1))
  129. ->method('execute')
  130. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  131. $self->assertEquals('git rev-list arbitrary..current', $command);
  132. $output = "$anotherCommitHash\n";
  133. return 0;
  134. })
  135. ;
  136. $config = new Config;
  137. $config->merge(array('repositories' => array('packagist' => false)));
  138. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  139. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('arbitrary')), 'dummy/path');
  140. $this->assertEquals("dev-arbitrary", $versionArray['version']);
  141. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  142. }
  143. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex()
  144. {
  145. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  146. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  147. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  148. ->setMethods(array('execute'))
  149. ->disableArgumentCloning()
  150. ->disableOriginalConstructor()
  151. ->getMock()
  152. ;
  153. $self = $this;
  154. $executor
  155. ->expects($this->at(0))
  156. ->method('execute')
  157. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  158. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  159. $output = " latest-testing $commitHash Commit message\n* current $anotherCommitHash Another message\n";
  160. return 0;
  161. })
  162. ;
  163. $executor
  164. ->expects($this->at(1))
  165. ->method('execute')
  166. ->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
  167. $self->assertEquals('git rev-list latest-testing..current', $command);
  168. $output = "$anotherCommitHash\n";
  169. return 0;
  170. })
  171. ;
  172. $config = new Config;
  173. $config->merge(array('repositories' => array('packagist' => false)));
  174. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  175. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  176. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  177. $this->assertEquals($anotherCommitHash, $versionArray['commit']);
  178. }
  179. public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch()
  180. {
  181. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  182. $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  183. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  184. ->setMethods(array('execute'))
  185. ->disableArgumentCloning()
  186. ->disableOriginalConstructor()
  187. ->getMock()
  188. ;
  189. $self = $this;
  190. $executor
  191. ->expects($this->at(0))
  192. ->method('execute')
  193. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
  194. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  195. $output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n";
  196. return 0;
  197. })
  198. ;
  199. $config = new Config;
  200. $config->merge(array('repositories' => array('packagist' => false)));
  201. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  202. $versionArray = $guesser->guessVersion(array('version' => 'self.version', 'non-feature-branches' => array('latest-.*')), 'dummy/path');
  203. $this->assertEquals("dev-latest-testing", $versionArray['version']);
  204. $this->assertEquals($commitHash, $versionArray['commit']);
  205. }
  206. public function testDetachedHeadBecomesDevHash()
  207. {
  208. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  209. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  210. ->setMethods(array('execute'))
  211. ->disableArgumentCloning()
  212. ->disableOriginalConstructor()
  213. ->getMock()
  214. ;
  215. $self = $this;
  216. $executor
  217. ->expects($this->at(0))
  218. ->method('execute')
  219. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  220. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  221. $output = "* (no branch) $commitHash Commit message\n";
  222. return 0;
  223. })
  224. ;
  225. $config = new Config;
  226. $config->merge(array('repositories' => array('packagist' => false)));
  227. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  228. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  229. $this->assertEquals("dev-$commitHash", $versionData['version']);
  230. }
  231. public function testDetachedFetchHeadBecomesDevHashGit2()
  232. {
  233. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  234. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  235. ->setMethods(array('execute'))
  236. ->disableArgumentCloning()
  237. ->disableOriginalConstructor()
  238. ->getMock();
  239. $self = $this;
  240. $executor
  241. ->expects($this->at(0))
  242. ->method('execute')
  243. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  244. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  245. $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
  246. return 0;
  247. });
  248. $config = new Config;
  249. $config->merge(array('repositories' => array('packagist' => false)));
  250. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  251. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  252. $this->assertEquals("dev-$commitHash", $versionData['version']);
  253. }
  254. public function testDetachedCommitHeadBecomesDevHashGit2()
  255. {
  256. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  257. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  258. ->setMethods(array('execute'))
  259. ->disableArgumentCloning()
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $self = $this;
  263. $executor
  264. ->expects($this->at(0))
  265. ->method('execute')
  266. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  267. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  268. $output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n";
  269. return 0;
  270. });
  271. $config = new Config;
  272. $config->merge(array('repositories' => array('packagist' => false)));
  273. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  274. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  275. $this->assertEquals("dev-$commitHash", $versionData['version']);
  276. }
  277. public function testTagBecomesVersion()
  278. {
  279. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  280. ->setMethods(array('execute'))
  281. ->disableArgumentCloning()
  282. ->disableOriginalConstructor()
  283. ->getMock()
  284. ;
  285. $self = $this;
  286. $executor
  287. ->expects($this->at(0))
  288. ->method('execute')
  289. ->willReturnCallback(function ($command, &$output) use ($self) {
  290. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  291. $output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
  292. return 0;
  293. })
  294. ;
  295. $executor
  296. ->expects($this->at(1))
  297. ->method('execute')
  298. ->willReturnCallback(function ($command, &$output) use ($self) {
  299. $self->assertEquals('git describe --exact-match --tags', $command);
  300. $output = "v2.0.5-alpha2";
  301. return 0;
  302. })
  303. ;
  304. $config = new Config;
  305. $config->merge(array('repositories' => array('packagist' => false)));
  306. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  307. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  308. $this->assertEquals("2.0.5.0-alpha2", $versionData['version']);
  309. }
  310. public function testTagBecomesPrettyVersion()
  311. {
  312. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  313. ->setMethods(array('execute'))
  314. ->disableArgumentCloning()
  315. ->disableOriginalConstructor()
  316. ->getMock()
  317. ;
  318. $self = $this;
  319. $executor
  320. ->expects($this->at(0))
  321. ->method('execute')
  322. ->willReturnCallback(function ($command, &$output) use ($self) {
  323. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  324. $output = "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n";
  325. return 0;
  326. })
  327. ;
  328. $executor
  329. ->expects($this->at(1))
  330. ->method('execute')
  331. ->willReturnCallback(function ($command, &$output) use ($self) {
  332. $self->assertEquals('git describe --exact-match --tags', $command);
  333. $output = '1.0.0';
  334. return 0;
  335. })
  336. ;
  337. $config = new Config;
  338. $config->merge(array('repositories' => array('packagist' => false)));
  339. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  340. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  341. $this->assertEquals('1.0.0.0', $versionData['version']);
  342. $this->assertEquals('1.0.0', $versionData['pretty_version']);
  343. }
  344. public function testInvalidTagBecomesVersion()
  345. {
  346. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  347. ->setMethods(array('execute'))
  348. ->disableArgumentCloning()
  349. ->disableOriginalConstructor()
  350. ->getMock()
  351. ;
  352. $self = $this;
  353. $executor
  354. ->expects($this->at(0))
  355. ->method('execute')
  356. ->willReturnCallback(function ($command, &$output) use ($self) {
  357. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  358. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  359. return 0;
  360. })
  361. ;
  362. $config = new Config;
  363. $config->merge(array('repositories' => array('packagist' => false)));
  364. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  365. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  366. $this->assertEquals("dev-foo", $versionData['version']);
  367. }
  368. public function testNumericBranchesShowNicely()
  369. {
  370. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  371. ->setMethods(array('execute'))
  372. ->disableArgumentCloning()
  373. ->disableOriginalConstructor()
  374. ->getMock()
  375. ;
  376. $self = $this;
  377. $executor
  378. ->expects($this->at(0))
  379. ->method('execute')
  380. ->willReturnCallback(function ($command, &$output) use ($self) {
  381. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  382. $output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  383. return 0;
  384. })
  385. ;
  386. $config = new Config;
  387. $config->merge(array('repositories' => array('packagist' => false)));
  388. $guesser = new VersionGuesser($config, $executor, new VersionParser());
  389. $versionData = $guesser->guessVersion(array(), 'dummy/path');
  390. $this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
  391. $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
  392. }
  393. }