Skip to content

Commit

Permalink
fix(testing): commit message length (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera authored Aug 5, 2020
1 parent b14aaf2 commit 2b2e1e0
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/commit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ const { execSync } = require('child_process');
* TypeScope is required and should be in the form of "<type>([scope]):" or "<type>:"
*
* IssueNumber is conditional and should be in the form of "issues/[issue number]"
* Exceptions are available for chore and fix type scopes, and "build" type scope "scopes".
* Exceptions are available for chore and fix type scopes, and "build" scope descriptions.
* i.e. "chore([scope])", "fix([scope])", "<type>(build):"
* An exception for having a PR reference at the end of the description is also allowed,
* i.e. "<description> (#10)"
*
* Minimal description is required, can be in the form of "<string>"
*
* Message lengths should not be greater than 65 characters. Appended PR numbers in the
* form of (#<number>) are ignored i.e. <message> (#10)
*/
describe('Commit Message', () => {
it('should have consistently formatted commit messages on pull requests', () => {
Expand Down Expand Up @@ -43,21 +46,28 @@ describe('Commit Message', () => {
(/:/.test(message) && message.split(/:/)) || message.split(/\s/);

const [hash, typeScope = ''] = hashTypeScope.split(/\s/);
const [issueNumber, ...description] = issueNumberDescription
.join(' ')
.trim()
.split(/\s/g);
const [issueNumber, ...description] = issueNumberDescription.join(' ').trim().split(/\s/g);

const updatedTypeScope = (typeScope && `${typeScope}:`) || '';
const updatedDescription = description.join(' ');
const [
updatedMessage,
remainingMessage = ''
] = `${updatedTypeScope} ${issueNumber} ${updatedDescription}`.split(/\(#\d{1,5}\)/);

return {
trimmedMessage:
(remainingMessage.trim().length === 0 && updatedMessage.trim()) ||
`${updatedTypeScope} ${issueNumber} ${updatedDescription}`,
hash,
typeScope: (typeScope && `${typeScope}:`) || '',
typeScope: updatedTypeScope,
issueNumber,
description: description.join(' ')
description: updatedDescription
};
});

const messagesList = messages.map(message => {
const { typeScope = null, issueNumber = null, description = null } = message;
const { trimmedMessage = null, typeScope = null, issueNumber = null, description = null } = message;

const issueNumberException =
/(^chore\([\d\D]+\))|(^fix\([\d\D]+\))|(^[\d\D]+\(build\))/.test(typeScope) ||
Expand All @@ -76,10 +86,14 @@ describe('Commit Message', () => {
(issueNumberException && !description && issueNumber && 'valid') ||
'INVALID: description';

// <type>([scope]): issues/<number> <description>
return `${typeScope}<${typeScopeValid}> ${issueNumber}<${issueNumberValid}> ${description}<${descriptionValid}>`;
const lengthValid =
(trimmedMessage && trimmedMessage.length <= 65 && 'valid') ||
`INVALID: message length (${trimmedMessage && trimmedMessage.length} > 65)`;

// <type>([scope]): issues/<number> <description> <messageLength>
return `${typeScope}<${typeScopeValid}> ${issueNumber}<${issueNumberValid}> ${description}<${descriptionValid}><${lengthValid}>`;
});

expect(messagesList.filter(value => !/<valid>[\d\D]*<valid>[\d\D]*<valid>/.test(value))).toEqual([]);
expect(messagesList.filter(value => !/<valid>[\d\D]*<valid>[\d\D]*<valid><valid>/.test(value))).toEqual([]);
});
});

0 comments on commit 2b2e1e0

Please sign in to comment.