Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #12 from cmalard/master
Browse files Browse the repository at this point in the history
feat(validateMessage): display original message when it is not valid
  • Loading branch information
Kent C. Dodds committed Feb 4, 2016
2 parents cd5aaca + 10e3766 commit 3b7d25f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
44 changes: 21 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ var validateMessage = function(message) {
var match = PATTERN.exec(message);

if (!match) {
error('does not match "<type>(<scope>): <subject>" ! was: ' + message);
return failure();
}

var firstLine = match[1];
var type = match[2];
var scope = match[4];
var subject = match[5];

if (firstLine.length > MAX_LENGTH) {
error('is longer than %d characters !', MAX_LENGTH);
error('does not match "<type>(<scope>): <subject>" !');
isValid = false;
}
} else {
var firstLine = match[1];
var type = match[2];
var scope = match[4];
var subject = match[5];

if (firstLine.length > MAX_LENGTH) {
error('is longer than %d characters !', MAX_LENGTH);
isValid = false;
}

if (TYPES !== '*' && TYPES.indexOf(type) === -1) {
error('"%s" is not allowed type !', type);
return failure();
if (TYPES !== '*' && TYPES.indexOf(type) === -1) {
error('"%s" is not allowed type !', type);
isValid = false;
}
}

// Some more ideas, do want anything like this ?
Expand All @@ -71,11 +71,14 @@ var validateMessage = function(message) {
// - auto correct typos in type ?
// - store incorrect messages, so that we can learn

return isValid ? true : failure();
isValid = isValid || config.warnOnFail;

function failure() {
return config.warnOnFail ? true : false;
// Display original message when it is not valid, otherwise it will be lost
if (!isValid && message) {
console.log(message);
}

return isValid;
};


Expand Down Expand Up @@ -110,11 +113,6 @@ if (process.argv.join('').indexOf('mocha') === -1) {
});
}

function getTypes() {
var defaultTypes = ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore', 'revert'];
return config.types || defaultTypes;
}

function getConfig() {
var pkgFile = findup.sync(process.cwd(), 'package.json');
var pkg = JSON.parse(fs.readFileSync(resolve(pkgFile, 'package.json')));
Expand Down
20 changes: 17 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ describe('validate-commit-msg.js', function() {
expect(m.validateMessage('revert(foo): something')).to.equal(VALID);
expect(m.validateMessage('custom(baz): something')).to.equal(VALID);
expect(errors).to.deep.equal([]);
expect(logs).to.deep.equal([]);
});


it('should validate 100 characters length', function() {
var msg = "fix($compile): something super mega extra giga tera long, maybe even longer and longer and longer... ";
var msg = 'fix($compile): something super mega extra giga tera long, maybe even longer and longer and longer... ';

expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors).to.deep.equal(['INVALID COMMIT MSG: is longer than 100 characters !']);
expect(logs).to.deep.equal([msg]);
});

it('should work fine with a bigger body', function() {
Expand All @@ -68,37 +70,49 @@ describe('validate-commit-msg.js', function() {
'Closes #14',
'BREAKING CHANGE: Something is totally broken :-('
].join('\n');

expect(m.validateMessage(message)).to.equal(VALID);
expect(errors).to.deep.equal([]);
expect(logs).to.deep.equal([]);
});


it('should validate "<type>(<scope>): <subject>" format', function() {
var msg = 'not correct format';

expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors).to.deep.equal(['INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" ! was: not correct format']);
expect(errors).to.deep.equal(['INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !']);
expect(logs).to.deep.equal([msg]);
});


it('should validate type', function() {
expect(m.validateMessage('weird($filter): something')).to.equal(INVALID);
var msg = 'weird($filter): something';

expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors).to.deep.equal(['INVALID COMMIT MSG: "weird" is not allowed type !']);
expect(logs).to.deep.equal([msg]);
});


it('should allow empty scope', function() {
expect(m.validateMessage('fix: blablabla')).to.equal(VALID);
expect(errors).to.deep.equal([]);
expect(logs).to.deep.equal([]);
});


it('should allow dot in scope', function() {
expect(m.validateMessage('chore(mocks.$httpBackend): something')).to.equal(VALID);
expect(errors).to.deep.equal([]);
expect(logs).to.deep.equal([]);
});


it('should ignore msg prefixed with "WIP "', function() {
expect(m.validateMessage('WIP stuff')).to.equal(VALID);
expect(errors).to.deep.equal([]);
expect(logs).to.not.deep.equal([]);
});

it('should handle undefined message"', function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.0.0-development",
"scripts": {
"commit": "git-cz",
"check-coverage": "istanbul check-coverage --statements 96 --branches 81 --functions 87 --lines 96",
"check-coverage": "istanbul check-coverage --statements 100 --branches 90 --functions 100 --lines 100",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"test:watch": "istanbul cover -x *.test.js _mocha -- -R spec -w index.test.js",
"test": "istanbul cover -x *.test.js _mocha -- -R spec index.test.js",
Expand Down

0 comments on commit 3b7d25f

Please sign in to comment.