Skip to content

Commit

Permalink
fix(cli): show error if no rules defined conventional-changelog#107
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat authored and marionebl committed Nov 30, 2018
1 parent 33ff7a9 commit d3f4f03
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 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 @@ -151,6 +151,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 @@ -271,7 +291,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
39 changes: 24 additions & 15 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 @@ -118,14 +124,17 @@ test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});

test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});
test.failing(
'should work with husky via commitlint -e %GIT_PARAMS%',
async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});

await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
}
);

test('should work with husky via commitlint -e $HUSKY_GIT_PARAMS', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
Expand All @@ -152,7 +161,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 +263,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
8 changes: 0 additions & 8 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import merge from 'lodash.merge';
import mergeWith from 'lodash.mergewith';
import pick from 'lodash.pick';
import resolveFrom from 'resolve-from';
// Import chalk from 'chalk';

const w = (a, b) => (Array.isArray(b) ? b : undefined);
const valid = input =>
Expand Down Expand Up @@ -100,13 +99,6 @@ async function loadConfig(cwd, configPath) {
if (local) {
return local;
}
// Because local is `null`
// throw new Error(`123`);

console.log('NEIN DER ZWERG DAS IST JA OTTO');

// This does not show up in tests
// chalk.yellow(`⚠ ${chalk.bold('config file')} may not be empty.`);

return {};
}
16 changes: 0 additions & 16 deletions @commitlint/load/src/index.serial-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,3 @@ test.serial('default cwd option to process.cwd()', async t => {
process.chdir(before);
}
});

// Test.serial('empty cwd option to process.cwd() should throw error message', async t => {
// const cwd = await fix.bootstrap('fixtures/empty-file');
// const before = process.cwd();
// process.chdir(cwd);

// try {
// const actual = await load();
// } catch (err) {
// console.log('-------------------------')
// // t.true(err.includes('OTTO'));
// // throw err;
// } finally {
// process.chdir(before);
// }
// });

0 comments on commit d3f4f03

Please sign in to comment.