Skip to content

Commit

Permalink
fix(rules): allow scopes to be empty so long as scope overrides exist
Browse files Browse the repository at this point in the history
fix/base-scopes-optional-if-override-scopes-exist
  • Loading branch information
Brett Uglow committed Jul 29, 2016
1 parent dbac74d commit 19b3c82
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
6 changes: 3 additions & 3 deletions config/testUnit/istanbul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ instrumentation:
root: lib/
check:
global:
statements: 80
functions: 80
statements: 90
branches: 80
lines: 80
functions: 80
lines: 90
each:
statements: 72
functions: 50
Expand Down
9 changes: 4 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ function validateMessage(firstLine, fullMsg) {
let type = match[1];
let scope = match[3];
let subject = match[4];
let allowedScopesForType = SCOPE_OVERRIDES[type] ? SCOPE_OVERRIDES[type] : [];

allowedScopesForType = allowedScopesForType.map(item => item.name).concat(SCOPES);

if (!TYPES.length) {
commitError(`No valid types defined! Check your package.json and cz-customisable rules file and define the "types" there.`);
return false;
}

if (!SCOPES.length && !allowCustomScopes) {
if (!SCOPES.length && !allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
commitError(`No valid scopes defined! Check your package.json and cz-customisable rules file and define the "scopes" there (or set allowCustomScopes to true)`);
return false;
}
Expand All @@ -122,10 +125,6 @@ function validateMessage(firstLine, fullMsg) {
return false;
}

let allowedScopesForType = SCOPE_OVERRIDES[type] ? SCOPE_OVERRIDES[type] : [];

allowedScopesForType = allowedScopesForType.map(item => item.name).concat(SCOPES);

if (!allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
commitError(`"${scope}" is not allowed scope for a "${type}" commit!\nValid "${type}" scopes: ${allowedScopesForType.sort().join(', ')}`);
return false;
Expand Down
66 changes: 63 additions & 3 deletions test/ccg.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('cz-customizable-ghooks', () => {
types: [
{value: 'feat', name: 'feat: A new feature'},
{value: 'fix', name: 'fix: A bug fix'},
{value: 'docs', name: 'docs: Documentation only changes'},
{value: 'docs', name: 'docs: Documentation only changes'}
],

scopes: [
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('cz-customizable-ghooks', () => {
};

const testData = [
{msg: 'feat(customScope): this ok', expectedResult: false},
{msg: 'feat(customScope): this not ok', expectedResult: false},
{msg: 'docs(custom): docs has an override scope', expectedResult: true},
{msg: 'fix(merge): and so does fix', expectedResult: true},
{msg: 'docs(invalidCustom): not a valid custom scope', expectedResult: false}
Expand All @@ -139,6 +139,66 @@ describe('cz-customizable-ghooks', () => {
});
});


describe('with no scopes but with scope overridesForTypes', () => {
let baseScopes = [
{name: 'merge'},
{name: 'style'},
{name: 'e2eTest'},
{name: 'unitTest'}
];
let config = {
types: [
{value: 'feat', name: 'feat: A new feature'},
{value: 'fix', name: 'fix: A bug fix'},
{value: 'docs', name: 'docs: Documentation only changes'}
],

scopeOverrides: {
fix: baseScopes,
docs: baseScopes.concat({name: 'custom'})
},
allowCustomScopes: false,
allowBreakingChanges: ['feat', 'fix'],
process: {
exit: () => {}
}
};

const testData = [
{msg: 'fix(merge): this ok', expectedResult: true},
{msg: 'docs(custom): this has an override scope', expectedResult: true},
{msg: 'feat(merge): no scopes for feature', expectedResult: false},
{msg: 'docs(invalidCustom): not a valid custom scope', expectedResult: false}
];

let consoleData = '';

beforeEach(() => {
module = rewire('../lib/index');
module.__set__({
czConfig: config,
console: {
log: (data) => consoleData += data,
error: (data) => consoleData += data
}
});
});

afterEach(() => {
consoleData = '';
});

it('should accept commit messages which match the rules in the config', () => {
testData.forEach(test => {
let lines = test.msg.split('\n');

assert.equal(module.validateMessage(lines[0], test.msg), test.expectedResult, test.msg);// + '\n' + consoleData);
});
});
});


describe('error conditions', () => {
let consoleData = '';

Expand Down Expand Up @@ -213,7 +273,7 @@ describe('cz-customizable-ghooks', () => {
});
});

it('should accept commit messages which match the rules in the config', () => {
it('should reject commit messages which do not match the rules in the config', () => {
testData.forEach(test => {
module.__set__({czConfig: test.config});

Expand Down

0 comments on commit 19b3c82

Please sign in to comment.