Skip to content

Commit b6bd36e

Browse files
escapedcatmarionebl
authored andcommitted
feat: warn on empty config (#491)
* feat(load): display warning when config is empty #107 * fix(cli): show error if no rules defined #107 * test: remove failing marker
1 parent 831a141 commit b6bd36e

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

@commitlint/cli/commitlint.config.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'never', ['foo']]
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'never', ['foo']]
4+
}
5+
};

@commitlint/cli/src/cli.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ async function main(options) {
150150
messages.map(message => lint(message, loaded.rules, opts))
151151
);
152152

153+
if (Object.keys(loaded.rules).length === 0) {
154+
results.push({
155+
valid: false,
156+
errors: [
157+
{
158+
level: 2,
159+
valid: false,
160+
name: 'empty-rules',
161+
message: [
162+
'Please add rules to your `commitlint.config.js`',
163+
' - Getting started guide: https://git.io/fpUzJ',
164+
' - Example config: https://git.io/fpUzm'
165+
].join('\n')
166+
}
167+
],
168+
warnings: [],
169+
input: ''
170+
});
171+
}
172+
153173
const report = results.reduce(
154174
(info, result) => {
155175
info.valid = result.valid ? info.valid : false;
@@ -270,7 +290,7 @@ function selectParserOpts(parserPreset) {
270290
}
271291

272292
function loadFormatter(config, flags) {
273-
const moduleName = flags.format || config.formatter;
293+
const moduleName = flags.format || config.formatter || '@commitlint/format';
274294
const modulePath =
275295
resolveFrom.silent(__dirname, moduleName) ||
276296
resolveFrom.silent(flags.cwd, moduleName) ||

@commitlint/cli/src/cli.test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,40 @@ const cli = (args, options) => {
2121
};
2222

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

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

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

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

49-
test('should succeed for input from stdin without rules', async t => {
49+
test('should fail for input from stdin without rules', async t => {
5050
const cwd = await git.bootstrap('fixtures/empty');
5151
const actual = await cli([], {cwd})('foo: bar');
52+
t.is(actual.code, 1);
53+
});
54+
55+
test('should succeed for input from stdin with rules', async t => {
56+
const cwd = await git.bootstrap('fixtures/default');
57+
const actual = await cli([], {cwd})('type: bar');
5258
t.is(actual.code, 0);
5359
});
5460

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

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

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

261267
t.false(actual.stdout.includes(message));

0 commit comments

Comments
 (0)