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

Commit

Permalink
feat(autoFix): fix the commit message case instead of ignoring case
Browse files Browse the repository at this point in the history
Close #105
  • Loading branch information
cmalard authored and Garbee committed Jul 29, 2017
1 parent 888617b commit 2e048fb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vcmrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"custom"
],
"warnOnFail": false,
"autoFix": false
"autoFix": true
}
14 changes: 10 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
'use strict';

var fs = require('fs');
var validateMessage = require('../index');

var getGitFolder = require('./getGitFolder');
var validateMessage = require('../index');

// hacky start if not run by mocha :-D
// istanbul ignore next
Expand All @@ -42,6 +43,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
};

var getCommit = function() {
var file;
var fileContent;
var gitDirectory;

Expand All @@ -53,6 +55,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
// these info might change ahead
message: commitMsgFileOrText,
errorLog: commitErrorLogPath || null,
file: null,
};

// On running the validation over a text instead of git files such as COMMIT_EDITMSG and GITGUI_EDITMSG
Expand All @@ -62,16 +65,19 @@ if (process.argv.join('').indexOf('mocha') === -1) {

// Try to load commit from a path passed as argument
if (commitMsgFileOrText) {
fileContent = getFileContent(gitDirectory + '/' + commitMsgFileOrText);
file = gitDirectory + '/' + commitMsgFileOrText;
fileContent = getFileContent(file);
}

// If no file or message is available then try to load it from the default commit file
if (!fileContent && !commitMsgFileOrText) {
fileContent = getFileContent(gitDirectory + '/COMMIT_EDITMSG');
file = gitDirectory + '/COMMIT_EDITMSG';
fileContent = getFileContent(file);
}

// Could resolve the content from a file
if (fileContent) {
commit.file = file;
commit.message = fileContent;
}

Expand All @@ -85,7 +91,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
};

var validate = function (commit) {
if (!validateMessage(commit.message) && commit.errorLog) {
if (!validateMessage(commit.message, commit.file) && commit.errorLog) {
fs.appendFileSync(commit.errorLog, commit.message + '\n');
process.exit(1);
} else {
Expand Down
22 changes: 17 additions & 5 deletions lib/validateMessage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

var util = require('util');
var fs = require('fs');
var semverRegex = require('semver-regex');
var util = require('util');

var getConfig = require('./config').getConfig;

var config = getConfig();
Expand All @@ -21,8 +23,9 @@ var error = function() {

exports.config = config;

exports.validateMessage = function validateMessage(raw) {
exports.validateMessage = function validateMessage(raw, sourceFile) {
var types = config.types = config.types || 'conventional-commit-types';
var AUTO_FIX = config.autoFix && sourceFile;

// resolve types from a module
if (typeof types === 'string' && types !== '*') {
Expand Down Expand Up @@ -72,8 +75,7 @@ exports.validateMessage = function validateMessage(raw) {
isValid = false;
}

// If should auto fix type then do it here
if (config.autoFix) {
if (AUTO_FIX) {
type = lowercase(type);
}

Expand All @@ -84,7 +86,7 @@ exports.validateMessage = function validateMessage(raw) {

isValid = validateScope(isValid, scope);

if (config.autoFix) {
if (AUTO_FIX) {
subject = lowercaseFirstLetter(subject);
}

Expand All @@ -104,6 +106,16 @@ exports.validateMessage = function validateMessage(raw) {
isValid = isValid || config.warnOnFail;

if (isValid) { // exit early and skip messaging logics
if (AUTO_FIX && !squashing) {
var scopeFixed = scope ? '(' + scope + ')' : '';
var firstLineFixed = type + scopeFixed + ': ' + subject;

if (firstLine !== firstLineFixed) {
var rawFixed = raw.replace(firstLine, firstLineFixed);
fs.writeFileSync(sourceFile, rawFixed);
}
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"custom"
],
"warnOnFail": false,
"autoFix": false
"autoFix": true
}
},
"dependencies": {
Expand Down
32 changes: 25 additions & 7 deletions test/validateMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('validate-commit-msg.js', function() {
var originalLog, originalError;
var errors = [];
var logs = [];
var writeFileSyncStub;

var VALID = true;
var INVALID = false;
Expand All @@ -27,6 +28,7 @@ describe('validate-commit-msg.js', function() {

sinon.spy(console, 'error');
sinon.spy(console, 'log');
writeFileSyncStub = sinon.stub(fs, 'writeFileSync');

function fakeError() {
var msg = format.apply(null, arguments);
Expand All @@ -40,6 +42,9 @@ describe('validate-commit-msg.js', function() {
});

afterEach(function() {
console.log = originalLog;
console.error = originalError;
fs.writeFileSync.restore();
m.config.autoFix = false;
});

Expand Down Expand Up @@ -292,33 +297,46 @@ describe('validate-commit-msg.js', function() {
});

it('should validate subject against subjectPattern if provided', function() {
var msg = 'chore(build): A something Z';
var subjectPatternBackup = m.config.subjectPattern;
m.config.subjectPattern = /^A.*Z$/;

var msg = 'chore(build): A something Z';
expect(m.validateMessage(msg)).to.equal(VALID);

msg = 'chore(build): something';
expect(m.validateMessage(msg)).to.equal(INVALID);

expect(errors).to.deep.equal(['INVALID COMMIT MSG: subject does not match subject pattern!']);
expect(logs).to.deep.equal([msg]);

m.config.subjectPattern = subjectPatternBackup;
});

it('should lowercase type when autoFix is true and make it valid', function() {
m.config.autoFix = true;
m.config.subjectPattern = /^a.*Z$/;
var msg = 'Chore(build): A something Z';
expect(m.validateMessage(msg)).to.equal(VALID);
expect(m.validateMessage(msg, 'file')).to.equal(VALID);
});

it('should show invalid when autoFix is false and type starts with capital letter', function() {
var msg = 'Chore(build): A something Z';
expect(m.validateMessage(msg)).to.equal(INVALID);
});
});

afterEach(function() {
console.log = originalLog;
console.error = originalError;
it('should update the file provided when autoFix is true and there are changes', function() {
m.config.autoFix = true;
var msg = 'Chore(build): A something Z';
var msgValid = 'chore(build): a something Z';
m.validateMessage(msg, 'file');
expect(writeFileSyncStub.calledWith('file', msgValid)).to.equal(VALID);
});

it('should not update the file provided when autoFix is true and there are no changes', function() {
m.config.autoFix = true;
var msg = 'chore(build): a something Z';
m.validateMessage(msg, 'file');
expect(writeFileSyncStub.called).to.equal(INVALID);
});
});

});

0 comments on commit 2e048fb

Please sign in to comment.