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 committed Nov 8, 2018
1 parent 14a843e commit 36d25e2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 45 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 @@ -263,7 +283,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
57 changes: 38 additions & 19 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,17 +124,20 @@ 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 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 @@ -230,8 +239,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 Expand Up @@ -264,17 +273,27 @@ test('should fail for invalid formatters from flags', async t => {
});

test('should work with absolute formatter path', async t => {
const formatterPath = path.resolve(__dirname, '../fixtures/custom-formatter/formatters/custom.js');
const formatterPath = path.resolve(
__dirname,
'../fixtures/custom-formatter/formatters/custom.js'
);
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', formatterPath], {cwd})('test: this should work');
const actual = await cli(['--format', formatterPath], {cwd})(
'test: this should work'
);

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

test('should work with relative formatter path', async t => {
const cwd = path.resolve(await git.bootstrap('fixtures/custom-formatter'), './formatters');
const actual = await cli(['--format', './custom.js'], {cwd})('test: this should work');
const cwd = path.resolve(
await git.bootstrap('fixtures/custom-formatter'),
'./formatters'
);
const actual = await cli(['--format', './custom.js'], {cwd})(
'test: this should work'
);

t.true(actual.stdout.includes('custom-formatter-ok'));
t.is(actual.code, 0);
Expand Down
11 changes: 2 additions & 9 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 @@ -55,7 +54,8 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {

// Resolve config-relative formatter module
if (typeof config.formatter === 'string') {
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
preset.formatter =
resolveFrom.silent(base, config.formatter) || config.formatter;
}

// Execute rule config functions if needed
Expand Down Expand Up @@ -99,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 36d25e2

Please sign in to comment.