Skip to content

Commit

Permalink
feat(core): Implement footer-contains rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nhodges committed Sep 19, 2017
1 parent 56e1f7c commit f45ddad
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions @commitlint/core/src/library/ensure-contains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default (value, regex) => {
if (value === undefined) {
return false;
}
if (!regex instanceof RegExp) {
return false;
}
return regex.test(value);
};
17 changes: 17 additions & 0 deletions @commitlint/core/src/library/ensure-contains.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'ava';
import ensure from './ensure-contains';

test('false for no params', t => {
const actual = ensure();
t.is(actual, false);
});

test('true for /^foo/gi against foo', t => {
const actual = ensure('foo', /^foo/gi);
t.is(actual, true);
});

test('false for /^foo/gi against notfoo', t => {
const actual = ensure('notfoo', /^foo/gi);
t.is(actual, false);
});
20 changes: 20 additions & 0 deletions @commitlint/core/src/rules/footer-contains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ensureContains from '../library/ensure-contains';
import message from '../library/message';

export default (parsed, when, value) => {
if (!parsed.scope) {
return [true, ''];
}

const negated = when === 'never';
const result = value.length === 0 || ensureContains(parsed.scope, value);

return [
negated ? !result : result,
message([
`footer content must`,
negated ? `not` : null,
`pass the regular expression: ${value.toString()}`
])
];
};
33 changes: 33 additions & 0 deletions @commitlint/core/src/rules/footer-contains.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';
import parse from '../library/parse';
import footerContains from './footer-contains';

const messages = {
empty: 'foo(bar): baz',
matched: 'foo(bar): baz\n\nbody\n\nqux',
unmatched: 'foo(bar): baz\n\nbody\n\nquux'
};

const parsed = {
empty: parse(messages.empty),
matched: parse(messages.matched),
unmatched: parse(messages.unmatched)
};

test('footer-contains with no footer should not succeed', async t => {
const [actual] = footerContains(await parsed.empty, 'always', []);
const expected = false;
t.deepEqual(actual, expected);
});

test('footer-contains with matching footer should succeed', async t => {
const [actual] = footerContains(await parsed.matched, 'never', []);
const expected = true;
t.deepEqual(actual, expected);
});

test('footer-contains with non-matching footer should not succeed', async t => {
const [actual] = footerContains(await parsed.unmatched, 'never', []);
const expected = false;
t.deepEqual(actual, expected);
});
1 change: 1 addition & 0 deletions @commitlint/core/src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
'body-max-length': require('./body-max-length'),
'body-min-length': require('./body-min-length'),
'body-tense': require('./body-tense'),
'footer-contains': require('./footer-contains'),
'footer-empty': require('./footer-empty'),
'footer-leading-blank': require('./footer-leading-blank'),
'footer-max-length': require('./footer-max-length'),
Expand Down

0 comments on commit f45ddad

Please sign in to comment.