This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Fix cli to work with COMMIT_EDITMSG, custom path, or text
Sometime ago I had included support to validate a commit message from a text instead of only from files. The validation from a commit message argument stopped working on adding support for GIT GUI tool: maxclaus@d850cf4 This fixing makes CLI support 3 usage ways: 1. Default usage is not passing any argument. It will automatically read from COMMIT_EDITMSG file. 2. Passing a file path argument. For instance GIT GUI stores commit msg @GITGUI_EDITMSG file. 3. Passing commit message as argument. Useful for testing quickly a commit message from CLI.
- Loading branch information
Showing
6 changed files
with
188 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ nohup.out | |
*.ignored | ||
.opt-in | ||
.opt-out | ||
|
||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict'; | ||
|
||
var spawnSync = require('child_process').spawnSync; | ||
var expect = require('chai').expect; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var rimraf = require('rimraf'); | ||
var mkdirp = require('mkdirp'); | ||
|
||
var TMP_PATH = path.join(__dirname, 'tmp/'); | ||
var GIT_TMP_PATH = path.join(TMP_PATH, '.git'); | ||
var LOGS_GIT_TMP_PATH = path.join(GIT_TMP_PATH, 'logs'); | ||
var COMMIT_EDITMSG_GIT_TMP_PATH = path.join(GIT_TMP_PATH, 'COMMIT_EDITMSG'); | ||
var GITGUI_EDITMSG_GIT_TMP_PATH = path.join(GIT_TMP_PATH, 'GITGUI_EDITMSG'); | ||
var CLI_PATH = path.join(__dirname, '../lib/cli.js'); | ||
|
||
var cliSources = [ | ||
'COMMIT_EDITMSG', | ||
'commit text argument', | ||
'commit file argument', | ||
]; | ||
|
||
function executeCliBySource(cliSource, commitMessage) { | ||
var options = {}; | ||
var args = []; | ||
switch (cliSource) { | ||
case 'COMMIT_EDITMSG': { | ||
fs.writeFileSync(COMMIT_EDITMSG_GIT_TMP_PATH, commitMessage); | ||
options.cwd = TMP_PATH; | ||
break; | ||
} | ||
case 'commit text argument': { | ||
args.push(commitMessage); | ||
break; | ||
} | ||
case 'commit file argument': { | ||
fs.writeFileSync(GITGUI_EDITMSG_GIT_TMP_PATH, commitMessage); | ||
options.cwd = TMP_PATH; | ||
args.push('GITGUI_EDITMSG'); | ||
break; | ||
} | ||
} | ||
|
||
return spawnSync(CLI_PATH, args, options); | ||
} | ||
|
||
describe('cli', function() { | ||
beforeEach(function () { | ||
rimraf.sync(GIT_TMP_PATH); | ||
mkdirp.sync(GIT_TMP_PATH); | ||
mkdirp.sync(LOGS_GIT_TMP_PATH); | ||
}); | ||
|
||
describe('all cli sources have the same behavior', function () { | ||
cliSources.forEach(function (cliSource) { | ||
describe(cliSource, function () { | ||
describe('when the commit message is valid', function () { | ||
it('should not print anything into output', function () { | ||
var commitMessage = 'chore: This a valid commit message'; | ||
var result = executeCliBySource(cliSource, commitMessage); | ||
expect(result.stdout.toString()).to.eql(''); | ||
expect(result.stderr.toString()).to.eql(''); | ||
expect(result.status).to.eql(0); | ||
}); | ||
}); | ||
|
||
// only check for COMMIT_EDITMSG because if the other cases are empty | ||
// then they will fallback to COMMIT_EDITMSG anyway | ||
if (cliSource === 'COMMIT_EDITMSG') { | ||
describe('when the commit message is empty', function () { | ||
it('should print into output the commit message is invalid', function () { | ||
var commitMessage = ''; | ||
var result = executeCliBySource(cliSource, commitMessage); | ||
expect(result.stdout.toString()).to.eql('Aborting commit due to empty commit message.\n'); | ||
expect(result.stderr.toString()).to.eql(''); | ||
expect(result.status).to.eql(0); | ||
}); | ||
}); | ||
} | ||
|
||
describe('when the commit message is not following the rules', function () { | ||
it('should print into output the commit message is invalid', function () { | ||
var commitMessage = 'my invalid commit message'; | ||
var result = executeCliBySource(cliSource, commitMessage); | ||
expect(result.stdout.toString()).to.eql([ | ||
commitMessage + '\n\n', | ||
'Please fix your commit message (and consider using http://npm.im/commitizen)\n\n' | ||
].join('')); | ||
expect(result.stderr.toString()).to.eql('INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !\n'); | ||
expect(result.status).to.eql(0); | ||
}); | ||
}); | ||
|
||
describe('when the commit message is using an invalid type', function () { | ||
it('should print into output the commit message is invalid', function () { | ||
var commitMessage = 'patch: my invalid commit message'; | ||
var result = executeCliBySource(cliSource, commitMessage); | ||
expect(result.stdout.toString()).to.eql([ | ||
commitMessage + '\n\n', | ||
'Please fix your commit message (and consider using http://npm.im/commitizen)\n\n' | ||
].join('')); | ||
expect(result.stderr.toString()).to.eql([ | ||
'INVALID COMMIT MSG: "patch" is not allowed type ! ', | ||
'Valid types are: feat, fix, docs, style, refactor, perf, test, chore, revert, custom\n' | ||
].join('')); | ||
expect(result.status).to.eql(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |