Skip to content

Commit

Permalink
Read new options from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Dec 4, 2022
1 parent 6d11dbc commit e5c5eb8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ function validateCommentConfig(configObject, templateVariables) {
);
}

const allowedOnCreateValues = ['create', 'nothing'];
if (configObject.comment['on-create'] === undefined || configObject.comment['on-create'] === null) {
configMap.set('onCreate', allowedOnCreateValues[0]);
} else if (allowedOnCreateValues.includes(configObject.comment['on-create'])) {
configMap.set('onCreate', configObject.comment['on-create']);
} else {
throw Error(
`found unexpected value '${configObject.comment['on-create']}' under key '.comment.on-create' (should be one of: ${allowedOnCreateValues.join(', ')})`,
);
}

const allowedOnUpdateValues = ['recreate', 'edit', 'nothing'];
if (configObject.comment['on-update'] === undefined || configObject.comment['on-update'] === null) {
configMap.set('onUpdate', allowedOnUpdateValues[0]);
Expand Down
38 changes: 38 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('validateCommentConfig', () => {
test('valid config', () => {
const input = {
comment: {
'on-create': 'nothing',
'on-update': 'nothing',
header: 'hello',
footer: 'bye',
Expand All @@ -91,6 +92,7 @@ describe('validateCommentConfig', () => {
};

const output = new Map([
['onCreate', 'nothing'],
['onUpdate', 'nothing'],
['header', 'hello'],
['footer', 'bye'],
Expand All @@ -113,6 +115,7 @@ describe('validateCommentConfig', () => {
};

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'nothing'],
['header', 'hello'],
['footer', 'bye'],
Expand All @@ -122,6 +125,27 @@ describe('validateCommentConfig', () => {
expect(config.validateCommentConfig(input)).toEqual(output);
});

test('on-create can be missing', () => {
const input = {
comment: {
'on-update': 'edit',
header: 'hi',
footer: 'bye',
snippets: [snippet2Object],
},
};

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'edit'],
['header', 'hi'],
['footer', 'bye'],
['snippets', [snippet2Map]],
]);

expect(config.validateCommentConfig(input)).toEqual(output);
});

test('header can be missing', () => {
const input = {
comment: {
Expand All @@ -132,6 +156,7 @@ describe('validateCommentConfig', () => {
};

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'edit'],
['header', undefined],
['footer', 'bye'],
Expand All @@ -151,6 +176,7 @@ describe('validateCommentConfig', () => {
};

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'edit'],
['header', 'hello'],
['footer', undefined],
Expand Down Expand Up @@ -190,6 +216,16 @@ describe('validateCommentConfig', () => {
expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value 'whatever' under key '\.comment\.on-update' \(should be one of: recreate, edit, nothing\)/);
});

test('on-create must be one of known values', () => {
const input = {
comment: {
'on-create': 'sup',
},
};

expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value 'sup' under key '\.comment\.on-create' \(should be one of: create, nothing\)/);
});

test('glob-options is optional', () => {
const input = {
comment: {
Expand All @@ -202,6 +238,7 @@ describe('validateCommentConfig', () => {
};

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'recreate'],
['header', undefined],
['footer', undefined],
Expand Down Expand Up @@ -298,6 +335,7 @@ describe('validateCommentConfig', () => {
const expected = new Map([
['footer', undefined],
['header', undefined],
['onCreate', 'create'],
['onUpdate', 'recreate'],
['snippets', snippets],
]);
Expand Down

0 comments on commit e5c5eb8

Please sign in to comment.