build(docs-infra): log warning rather than error if content errors are not fatal (#24320)

PR Close #24320
This commit is contained in:
Pete Bacon Darwin 2018-06-06 12:22:25 +01:00 committed by Victor Berchet
parent 68d37ef0c1
commit 700e55ce14
2 changed files with 31 additions and 16 deletions

View File

@ -34,6 +34,7 @@ module.exports = function checkContentRules(log, createDocMessage) {
$runAfter: ['tags-extracted'], $runAfter: ['tags-extracted'],
$runBefore: ['processing-docs'], $runBefore: ['processing-docs'],
$process(docs) { $process(docs) {
const logMessage = this.failOnContentErrors ? log.error.bind(log) : log.warn.bind(log);
const errors = []; const errors = [];
docs.forEach(doc => { docs.forEach(doc => {
const docErrors = []; const docErrors = [];
@ -55,10 +56,10 @@ module.exports = function checkContentRules(log, createDocMessage) {
}); });
if (errors.length) { if (errors.length) {
log.error('Content contains errors'); logMessage('Content contains errors');
errors.forEach(docError => { errors.forEach(docError => {
const errors = docError.errors.join('\n '); const errors = docError.errors.join('\n ');
log.error(createDocMessage(errors + '\n ', docError.doc)); logMessage(createDocMessage(errors + '\n ', docError.doc));
}); });
if (this.failOnContentErrors) { if (this.failOnContentErrors) {
throw new Error('Stopping due to content errors.'); throw new Error('Stopping due to content errors.');

View File

@ -67,10 +67,11 @@ describe('checkContentRules processor', function() {
expect(descriptionSpy3).toHaveBeenCalledWith(docs[0], 'description', 'test doc 1'); expect(descriptionSpy3).toHaveBeenCalledWith(docs[0], 'description', 'test doc 1');
}); });
it('should log errors if the rule returns error messages', () => { it('should log warnings if the rule returns error messages and `failOnContentErrors` is false', () => {
const nameSpy1 = jasmine.createSpy('name 1').and.returnValue('name error message'); const nameSpy1 = jasmine.createSpy('name 1').and.returnValue('name error message');
const descriptionSpy1 = jasmine.createSpy('description 1').and.returnValue('description error message'); const descriptionSpy1 = jasmine.createSpy('description 1').and.returnValue('description error message');
processor.failOnContentErrors = false;
processor.docTypeRules = { processor.docTypeRules = {
'test1': { 'test1': {
name: [nameSpy1], name: [nameSpy1],
@ -85,6 +86,32 @@ describe('checkContentRules processor', function() {
processor.$process(docs); processor.$process(docs);
expect(logger.warn).toHaveBeenCalledTimes(2);
expect(logger.warn).toHaveBeenCalledWith('Content contains errors');
expect(logger.warn).toHaveBeenCalledWith(`name error message
description error message
- doc "test-1" (test1) `);
});
it('should log errors and then throw if `failOnContentErrors` is true and errors are found', () => {
const nameSpy1 = jasmine.createSpy('name 1').and.returnValue('name error message');
const descriptionSpy1 = jasmine.createSpy('description 1').and.returnValue('description error message');
processor.failOnContentErrors = true;
processor.docTypeRules = {
'test1': {
name: [nameSpy1],
description: [descriptionSpy1]
}
};
const docs = [
{ docType: 'test1', description: 'test doc 1', name: 'test-1' },
{ docType: 'test2', description: 'test doc 2', name: 'test-2' }
];
expect(() => processor.$process(docs)).toThrowError('Stopping due to content errors.');
expect(logger.error).toHaveBeenCalledTimes(2); expect(logger.error).toHaveBeenCalledTimes(2);
expect(logger.error).toHaveBeenCalledWith('Content contains errors'); expect(logger.error).toHaveBeenCalledWith('Content contains errors');
expect(logger.error).toHaveBeenCalledWith(`name error message expect(logger.error).toHaveBeenCalledWith(`name error message
@ -92,17 +119,4 @@ describe('checkContentRules processor', function() {
- doc "test-1" (test1) `); - doc "test-1" (test1) `);
}); });
it('should throw an error if `failOnContentErrors` is true and errors are found', () => {
const errorRule = jasmine.createSpy('error rule').and.returnValue('some error');
processor.docTypeRules = {
'test': { description: [errorRule] }
};
processor.failOnContentErrors = true;
const docs = [
{ docType: 'test', description: 'test doc' },
];
expect(() => processor.$process(docs)).toThrowError('Stopping due to content errors.');
});
}); });