VersionGuesserTest.php 17 KB

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