CommitMessageTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Tests the commit message validator
  5. */
  6. class CommitMessageTest extends \PHPUnit_Framework_TestCase {
  7. public function assertInvalidCommitMessages(array $msgs) {
  8. $msg = new CommitMessage();
  9. foreach ($msgs as $text) {
  10. $msg->setMsg($text);
  11. $this->assertFalse($msg->isValidFormat(), $text);
  12. }
  13. }
  14. public function testRejectsMessagesWithoutSummary() {
  15. $this->assertInvalidCommitMessages(array(
  16. 'chore(test):',
  17. 'chore(test): ',
  18. "chore(test):\n",
  19. ));
  20. }
  21. public function testRejectsMessagesWithoutType() {
  22. $this->assertInvalidCommitMessages(array(
  23. 'A bad commit message',
  24. ));
  25. }
  26. public function testRejectsMessagesWithoutComponent() {
  27. $this->assertInvalidCommitMessages(array(
  28. 'chore: Summary',
  29. 'chore(): Summary',
  30. 'chore(test):Summary',
  31. ));
  32. }
  33. public function assertIgnoreCommitMessages(array $ignored) {
  34. foreach ($ignored as $msg) {
  35. $msg = new CommitMessage($msg);
  36. $this->assertTrue($msg->shouldIgnore(), $msg);
  37. }
  38. }
  39. public function testShouldIgnoreMerges() {
  40. $this->assertIgnoreCommitMessages(array(
  41. 'Merge pull request',
  42. 'Merge abc123 into def456',
  43. "Merge pull request abc123\nBut has other stuff, too",
  44. 'Merge release 1.8.18 into master.',
  45. ));
  46. }
  47. public function testShouldIgnoreReverts() {
  48. $this->assertIgnoreCommitMessages(array(
  49. 'Revert "fix(amd): removed elgg_require_js for backwards compatibility"
  50. This reverts commit 76584089bee2b3246c736edb6b250e149acf906f.
  51. Conflicts:
  52. engine/lib/views.php'
  53. ));
  54. }
  55. public function testCanParseMessagesWithoutBody() {
  56. $text = "chore(test): Summary";
  57. $msg = new CommitMessage($text);
  58. $this->assertTrue($msg->isValidFormat());
  59. $this->assertSame('chore', $msg->getPart('type'));
  60. $this->assertSame('test', $msg->getPart('component'));
  61. $this->assertSame('Summary', $msg->getPart('summary'));
  62. $this->assertSame('', $msg->getPart('body'));
  63. }
  64. public function testCanParseMessagesWithOneLineBody() {
  65. $text = "chore(test): Summary\nOptional body";
  66. $msg = new CommitMessage($text);
  67. $this->assertTrue($msg->isValidFormat());
  68. $this->assertSame('chore', $msg->getPart('type'));
  69. $this->assertSame('test', $msg->getPart('component'));
  70. $this->assertSame('Summary', $msg->getPart('summary'));
  71. $this->assertSame('Optional body', $msg->getPart('body'));
  72. }
  73. public function testCanParseMessagesWithAnExtendedBody() {
  74. $title = "chore(test): Summary";
  75. $body = <<<___MSG
  76. Optional body
  77. Fixes #123, #456
  78. Refs #789
  79. ___MSG;
  80. $text = "$title\n$body";
  81. $msg = new CommitMessage($text);
  82. $this->assertTrue($msg->isValidFormat());
  83. $this->assertSame('chore', $msg->getPart('type'));
  84. $this->assertSame('test', $msg->getPart('component'));
  85. $this->assertSame('Summary', $msg->getPart('summary'));
  86. $this->assertSame($body, $msg->getPart('body'));
  87. }
  88. public function testIsValidLineLengthRejectsLinesOverTheMaxLineLength() {
  89. $text = "chore(test): But with long line";
  90. $msg = new CommitMessage();
  91. $msg->setMaxLineLength(15);
  92. $msg->setMsg($text);
  93. $this->assertFalse($msg->isValidLineLength());
  94. }
  95. public function testFindLengthyLinesFindsLinesOverTheMaxLineLength() {
  96. $text = "This text is 33 characters long.";
  97. $this->assertSame(array(1), CommitMessage::findLengthyLines($text, 30));
  98. $text2 = 'This line is only 22.';
  99. $this->assertSame(array(), CommitMessage::findLengthyLines($text2, 30));
  100. $text3 = <<<___TEXT
  101. This has multiple lines.
  102. Some of which are not really very long at all.
  103. Some are.
  104. ___TEXT;
  105. $this->assertSame(array(2), CommitMessage::findLengthyLines($text3, 30));
  106. }
  107. public function testGetLengthyLinesFindsLinesOverTheMaxLineLength() {
  108. $text =<<<___MSG
  109. chore(test): But with long line
  110. The long line is down here. This line is much longer than the other.
  111. And this one is short.
  112. But here we go again with another long line.
  113. ___MSG;
  114. $msg = new CommitMessage();
  115. $msg->setMaxLineLength(40);
  116. $msg->setMsg($text);
  117. $this->assertSame(array(3, 5), $msg->getLengthyLines());
  118. }
  119. public function testIsValidTypeReturnsTrueForValidTypes() {
  120. $types = CommitMessage::getValidTypes();
  121. foreach ($types as $type) {
  122. $msg = new CommitMessage("{$type}(component): Summary");
  123. $this->assertTrue($msg->isValidType(), "Invalid type `$type`.");
  124. }
  125. }
  126. public function testRemovesComments() {
  127. $text = <<<___TEXT
  128. These are lines of text
  129. # this is a comment
  130. # and another one.
  131. And more text
  132. ___TEXT;
  133. $expected = "These are lines of text\nAnd more text";
  134. $this->assertSame($expected, CommitMessage::removeComments($text));
  135. }
  136. }