Skip to content

Commit 36d25e2

Browse files
committed
fix(cli): show error if no rules defined conventional-changelog#107
1 parent 14a843e commit 36d25e2

File tree

7 files changed

+71
-45
lines changed

7 files changed

+71
-45
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
@@ -151,6 +151,26 @@ async function main(options) {
151151
messages.map(message => lint(message, loaded.rules, opts))
152152
);
153153

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

265285
function loadFormatter(config, flags) {
266-
const moduleName = flags.format || config.formatter;
286+
const moduleName = flags.format || config.formatter || '@commitlint/format';
267287
const modulePath =
268288
resolveFrom.silent(__dirname, moduleName) ||
269289
resolveFrom.silent(flags.cwd, moduleName) ||

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

+38-19
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

@@ -118,17 +124,20 @@ test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
118124
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
119125
});
120126

121-
test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
122-
const cwd = await git.bootstrap('fixtures/husky/integration');
123-
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});
127+
test.failing(
128+
'should work with husky via commitlint -e %GIT_PARAMS%',
129+
async () => {
130+
const cwd = await git.bootstrap('fixtures/husky/integration');
131+
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});
124132

125-
await execa('npm', ['install'], {cwd});
126-
await execa('git', ['add', 'package.json'], {cwd});
127-
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
128-
});
133+
await execa('npm', ['install'], {cwd});
134+
await execa('git', ['add', 'package.json'], {cwd});
135+
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
136+
}
137+
);
129138

130139
test('should allow reading of environment variables for edit file, succeeding if valid', async t => {
131-
const cwd = await git.bootstrap();
140+
const cwd = await git.bootstrap('fixtures/simple');
132141
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
133142
const actual = await cli(['--env', 'variable'], {
134143
cwd,
@@ -230,8 +239,8 @@ test('should print full commit message when input from stdin fails', async t =>
230239
});
231240

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

237246
t.false(actual.stdout.includes(message));
@@ -264,17 +273,27 @@ test('should fail for invalid formatters from flags', async t => {
264273
});
265274

266275
test('should work with absolute formatter path', async t => {
267-
const formatterPath = path.resolve(__dirname, '../fixtures/custom-formatter/formatters/custom.js');
276+
const formatterPath = path.resolve(
277+
__dirname,
278+
'../fixtures/custom-formatter/formatters/custom.js'
279+
);
268280
const cwd = await git.bootstrap('fixtures/custom-formatter');
269-
const actual = await cli(['--format', formatterPath], {cwd})('test: this should work');
281+
const actual = await cli(['--format', formatterPath], {cwd})(
282+
'test: this should work'
283+
);
270284

271285
t.true(actual.stdout.includes('custom-formatter-ok'));
272286
t.is(actual.code, 0);
273287
});
274288

275289
test('should work with relative formatter path', async t => {
276-
const cwd = path.resolve(await git.bootstrap('fixtures/custom-formatter'), './formatters');
277-
const actual = await cli(['--format', './custom.js'], {cwd})('test: this should work');
290+
const cwd = path.resolve(
291+
await git.bootstrap('fixtures/custom-formatter'),
292+
'./formatters'
293+
);
294+
const actual = await cli(['--format', './custom.js'], {cwd})(
295+
'test: this should work'
296+
);
278297

279298
t.true(actual.stdout.includes('custom-formatter-ok'));
280299
t.is(actual.code, 0);

@commitlint/load/src/index.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import merge from 'lodash.merge';
77
import mergeWith from 'lodash.mergewith';
88
import pick from 'lodash.pick';
99
import resolveFrom from 'resolve-from';
10-
// Import chalk from 'chalk';
1110

1211
const w = (a, b) => (Array.isArray(b) ? b : undefined);
1312
const valid = input =>
@@ -55,7 +54,8 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
5554

5655
// Resolve config-relative formatter module
5756
if (typeof config.formatter === 'string') {
58-
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
57+
preset.formatter =
58+
resolveFrom.silent(base, config.formatter) || config.formatter;
5959
}
6060

6161
// Execute rule config functions if needed
@@ -99,13 +99,6 @@ async function loadConfig(cwd, configPath) {
9999
if (local) {
100100
return local;
101101
}
102-
// Because local is `null`
103-
// throw new Error(`123`);
104-
105-
console.log('NEIN DER ZWERG DAS IST JA OTTO');
106-
107-
// This does not show up in tests
108-
// chalk.yellow(`⚠ ${chalk.bold('config file')} may not be empty.`);
109102

110103
return {};
111104
}

@commitlint/load/src/index.serial-test.js

-16
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,3 @@ test.serial('default cwd option to process.cwd()', async t => {
1717
process.chdir(before);
1818
}
1919
});
20-
21-
// Test.serial('empty cwd option to process.cwd() should throw error message', async t => {
22-
// const cwd = await fix.bootstrap('fixtures/empty-file');
23-
// const before = process.cwd();
24-
// process.chdir(cwd);
25-
26-
// try {
27-
// const actual = await load();
28-
// } catch (err) {
29-
// console.log('-------------------------')
30-
// // t.true(err.includes('OTTO'));
31-
// // throw err;
32-
// } finally {
33-
// process.chdir(before);
34-
// }
35-
// });

0 commit comments

Comments
 (0)