Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
feat(html-reporter): add ability to specify output path for each report
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeL authored and Sergii Paryzhskyi committed Dec 19, 2016
1 parent a1b4f34 commit b25409e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.run = () => {
program.command('test [paths...]')
.allowUnknownOption(true)
.option('-r, --reporter <reporter>', 'test reporter. Available reporters are: flat, vflat, html.', collect)
.option('--html-reporter-path <path>', 'Relative path where html reporters will be stored', collect)
.option('-s, --set <set>', 'set to run', collect)
.description('run tests')
.action((paths, options) => runGemini('test', paths, options).done());
Expand Down Expand Up @@ -96,9 +97,17 @@ function runGemini(method, paths, options) {
return gemini;
})
.then((gemini) => {
function parseReporterOptions(options) {
return options.reporter.map(function(name) {
return {
name,
path: options[`${name}ReporterPath`]
};
});
}
return gemini[method](paths, {
sets: options.set,
reporters: options.reporter || ['flat'],
reporters: parseReporterOptions(options) || [{name: 'flat'}],
grep: program.grep,
browsers: program.browser,
diff: options.diff,
Expand Down
12 changes: 9 additions & 3 deletions lib/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,23 @@ module.exports = class Gemini extends PassthroughEmitter {

function applyReporter(runner, reporter) {
if (typeof reporter === 'string') {
reporter = {name: reporter};
}
if (typeof reporter === 'object') {
const reporterPath = reporter.path;
try {
reporter = require('./reporters/' + reporter);
reporter = require('./reporters/' + reporter.name);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new GeminiError('No such reporter: ' + reporter);
throw new GeminiError('No such reporter: ' + reporter.name);
}
throw e;
}

return reporter(runner, reporterPath);
}
if (typeof reporter !== 'function') {
throw new TypeError('Reporter must be a string or a function');
throw new TypeError('Reporter must be a string, an object or a function');
}

reporter(runner);
Expand Down
10 changes: 5 additions & 5 deletions lib/reporters/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function logError(e) {
console.error(e.stack);
}

function logPathToHtmlReport() {
const reportPath = `file://${path.resolve('gemini-report/index.html')}`;
function logPathToHtmlReport(reporterPath) {
const reportPath = `file://${path.resolve((reporterPath || lib.REPORT_DIR) + '/index.html')}`;

logger.log(`Your HTML report is here: ${chalk.yellow(reportPath)}`);
}
Expand Down Expand Up @@ -122,11 +122,11 @@ function prepareImages(runner) {
});
}

module.exports = function htmlReporter(runner) {
module.exports = function htmlReporter(runner, reportPath) {
const generateReportPromise = Promise.all([prepareViewData(runner), prepareImages(runner)])
.spread(view.createHtml)
.then(view.save)
.then(logPathToHtmlReport)
.then((html) => view.save(html, reportPath))
.then(logPathToHtmlReport(reportPath))
.catch(logError);

runner.on(Events.END_RUNNER, () => generateReportPromise.thenReturn());
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/html/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ module.exports = {
* @param {String} html
* returns {Promise}
*/
save: function(html) {
return fs.mkdirsAsync(REPORT_DIR)
save: function(html, reportPath) {
return fs.mkdirsAsync(reportPath || REPORT_DIR)
.then(() => Promise.all([
fs.writeFileAsync(makeOutFilePath('index.html'), html, 'utf8'),
copyToReportDir('report.min.js'),
Expand Down

0 comments on commit b25409e

Please sign in to comment.