build(aio): capture and log errors and warnings when post-processing HTML

This commit is contained in:
Peter Bacon Darwin 2017-04-28 11:34:01 +01:00 committed by Matias Niemelä
parent 9945ce2259
commit 64335d3521
3 changed files with 38 additions and 5 deletions

View File

@ -1,11 +1,14 @@
const processorFactory = require('../../post-process-package/processors/post-process-html');
var testPackage = require('../../helpers/test-package');
var Dgeni = require('dgeni');
const plugin = require('./autolink-headings');
describe('autolink-headings postprocessor', () => {
let processor;
beforeEach(() => {
processor = processorFactory();
const dgeni = new Dgeni([testPackage('angular-base-package')]);
const injector = dgeni.configureInjector();
processor = injector.get('postProcessHtml');
processor.docTypes = ['a'];
processor.plugins = [plugin];
});

View File

@ -17,7 +17,7 @@ const rehype = require('rehype');
* @property plugins {Function[]} the rehype plugins that will modify the HAST.
*
*/
module.exports = function postProcessHtml() {
module.exports = function postProcessHtml(log, createDocMessage) {
return {
$runAfter: ['docs-rendered'],
$runBefore: ['writing-files'],
@ -30,8 +30,18 @@ module.exports = function postProcessHtml() {
docs
.filter(doc => this.docTypes.indexOf(doc.docType) !== -1)
.forEach(doc =>
doc.renderedContent = engine.processSync(doc.renderedContent).contents);
.forEach(doc => {
const vFile = engine.processSync(doc.renderedContent);
vFile.messages.forEach(m => {
const message = createDocMessage(m.message, doc);
if (m.fatal) {
throw new Error(message);
} else {
log.warn(message);
}
});
doc.renderedContent = vFile.contents;
});
}
};
};

View File

@ -59,4 +59,24 @@ describe('postProcessHtml', function() {
processor.$process(docs);
expect(elements).toEqual(['A1', 'B1']);
});
it('should report non-fatal errors', () => {
const log = injector.get('log');
const addWarning = (ast, file) => {
file.message('There was a problem');
};
processor.plugins = [() => addWarning];
processor.$process([{ docType: 'a', renderedContent: '' }]);
expect(log.warn).toHaveBeenCalled();
});
it('should throw on fatal errors', () => {
const log = injector.get('log');
const addError = (ast, file) => {
file.fail('There was an error');
};
processor.plugins = [() => addError];
expect(() => processor.$process([{ docType: 'a', renderedContent: '' }])).toThrow();
expect(log.error).not.toHaveBeenCalled();
});
});