Skip to content

Commit

Permalink
feat: warn on empty config (#491)
Browse files Browse the repository at this point in the history
* feat(load): display warning when config is empty #107

* fix(cli): show error if no rules defined #107

* test: remove failing marker
  • Loading branch information
escapedcat authored and marionebl committed Nov 30, 2018
1 parent 831a141 commit b6bd36e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
Empty file.
5 changes: 5 additions & 0 deletions @commitlint/cli/fixtures/default/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'type-enum': [2, 'never', ['foo']]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'type-enum': [2, 'never', ['foo']]
}
};
22 changes: 21 additions & 1 deletion @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ async function main(options) {
messages.map(message => lint(message, loaded.rules, opts))
);

if (Object.keys(loaded.rules).length === 0) {
results.push({
valid: false,
errors: [
{
level: 2,
valid: false,
name: 'empty-rules',
message: [
'Please add rules to your `commitlint.config.js`',
' - Getting started guide: https://git.io/fpUzJ',
' - Example config: https://git.io/fpUzm'
].join('\n')
}
],
warnings: [],
input: ''
});
}

const report = results.reduce(
(info, result) => {
info.valid = result.valid ? info.valid : false;
Expand Down Expand Up @@ -270,7 +290,7 @@ function selectParserOpts(parserPreset) {
}

function loadFormatter(config, flags) {
const moduleName = flags.format || config.formatter;
const moduleName = flags.format || config.formatter || '@commitlint/format';
const modulePath =
resolveFrom.silent(__dirname, moduleName) ||
resolveFrom.silent(flags.cwd, moduleName) ||
Expand Down
22 changes: 14 additions & 8 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,40 @@ const cli = (args, options) => {
};

test('should throw when called without [input]', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const cwd = await git.bootstrap('fixtures/default');
const actual = await cli([], {cwd})();
t.is(actual.code, 1);
});

test('should reprint input from stdin', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const cwd = await git.bootstrap('fixtures/default');
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('foo: bar'));
});

test('should produce no success output with --quiet flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const cwd = await git.bootstrap('fixtures/default');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});

test('should produce no success output with -q flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const cwd = await git.bootstrap('fixtures/default');
const actual = await cli(['-q'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});

test('should succeed for input from stdin without rules', async t => {
test('should fail for input from stdin without rules', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
t.is(actual.code, 1);
});

test('should succeed for input from stdin with rules', async t => {
const cwd = await git.bootstrap('fixtures/default');
const actual = await cli([], {cwd})('type: bar');
t.is(actual.code, 0);
});

Expand Down Expand Up @@ -152,7 +158,7 @@ test('should work with husky via commitlint -e %HUSKY_GIT_PARAMS%', async () =>
});

test('should allow reading of environment variables for edit file, succeeding if valid', async t => {
const cwd = await git.bootstrap();
const cwd = await git.bootstrap('fixtures/simple');
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
const actual = await cli(['--env', 'variable'], {
cwd,
Expand Down Expand Up @@ -254,8 +260,8 @@ test('should print full commit message when input from stdin fails', async t =>
});

test('should not print full commit message when input succeeds', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const message = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const cwd = await git.bootstrap('fixtures/default');
const message = 'type: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const actual = await cli([], {cwd})(message);

t.false(actual.stdout.includes(message));
Expand Down

0 comments on commit b6bd36e

Please sign in to comment.