UNPKG

stryker-html-reporter

Version:

An html reporter for the JavaScript mutation testing framework Stryker

111 lines 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var fileUrl = require("file-url"); var path = require("path"); var util = require("./util"); var templates = require("./templates"); var Breadcrumb_1 = require("./Breadcrumb"); var plugin_1 = require("stryker-api/plugin"); var DEFAULT_BASE_FOLDER = path.normalize('reports/mutation/html'); exports.RESOURCES_DIR_NAME = 'strykerResources'; var HtmlReporter = /** @class */ (function () { function HtmlReporter(options, log) { this.options = options; this.log = log; } HtmlReporter.prototype.onAllSourceFilesRead = function (files) { this.files = files; }; HtmlReporter.prototype.onAllMutantsTested = function (results) { this.mutantResults = results; }; HtmlReporter.prototype.onScoreCalculated = function (score) { this.scoreResult = score; this.mainPromise = this.generateReport(); }; HtmlReporter.prototype.wrapUp = function () { return this.mainPromise; }; HtmlReporter.prototype.generateReport = function () { var _this = this; return this.cleanBaseFolder() .then(function () { return _this.writeCommonResources(); }) .then(function () { return _this.writeReportDirectory(); }) .then(function (location) { return _this.log.info("Your report can be found at: " + fileUrl(location)); }); }; HtmlReporter.prototype.writeCommonResources = function () { var resourcesDir = path.join(__dirname, '..', 'resources'); return util.copyFolder(resourcesDir, this.resourcesDir); }; HtmlReporter.prototype.writeReportDirectory = function (scoreResult, currentDirectory, breadcrumb) { var _this = this; if (scoreResult === void 0) { scoreResult = this.scoreResult; } if (currentDirectory === void 0) { currentDirectory = this.baseDir; } if (breadcrumb === void 0) { breadcrumb = Breadcrumb_1.default.start; } var fileContent = templates.directory(scoreResult, breadcrumb, this.options.thresholds); var location = path.join(currentDirectory, 'index.html'); return util.mkdir(currentDirectory) .then(function (_) { return util.writeFile(location, fileContent); }) .then(function (_) { return _this.writeChildren(scoreResult, currentDirectory, breadcrumb); }) .then(function (_) { return location; }); }; HtmlReporter.prototype.writeChildren = function (scoreResult, currentDirectory, breadcrumb) { var _this = this; return Promise.all(scoreResult.childResults.map(function (child) { if (child.representsFile) { return _this.writeReportFile(child, currentDirectory, breadcrumb.add(child.name, util.countPathSep(child.name))); } else { return _this.writeReportDirectory(child, path.join(currentDirectory, child.name), breadcrumb.add(child.name, util.countPathSep(child.name) + 1)) .then(function (_) { return void 0; }); } })); }; HtmlReporter.prototype.writeReportFile = function (scoreResult, baseDir, breadcrumb) { if (scoreResult.representsFile) { var fileContent = templates.sourceFile(scoreResult, this.findFile(scoreResult.path), this.findMutants(scoreResult.path), breadcrumb, this.options.thresholds); return util.writeFile(path.join(baseDir, scoreResult.name + ".html"), fileContent); } else { return Promise.resolve(); // not a report file } }; HtmlReporter.prototype.findFile = function (filePath) { return this.files.find(function (file) { return file.path === filePath; }); }; HtmlReporter.prototype.findMutants = function (filePath) { return this.mutantResults.filter(function (mutant) { return mutant.sourceFilePath === filePath; }); }; Object.defineProperty(HtmlReporter.prototype, "resourcesDir", { get: function () { return path.join(this.baseDir, exports.RESOURCES_DIR_NAME); }, enumerable: true, configurable: true }); Object.defineProperty(HtmlReporter.prototype, "baseDir", { get: function () { if (!this._baseDir) { if (this.options.htmlReporter && this.options.htmlReporter.baseDir) { this._baseDir = this.options.htmlReporter.baseDir; this.log.debug("Using configured output folder " + this._baseDir); } else { this.log.debug("No base folder configuration found (using configuration: htmlReporter: { baseDir: 'output/folder' }), using default " + DEFAULT_BASE_FOLDER); this._baseDir = DEFAULT_BASE_FOLDER; } } return this._baseDir; }, enumerable: true, configurable: true }); HtmlReporter.prototype.cleanBaseFolder = function () { var _this = this; return util.deleteDir(this.baseDir).then(function () { return util.mkdir(_this.baseDir); }); }; HtmlReporter.inject = plugin_1.tokens(plugin_1.commonTokens.options, plugin_1.commonTokens.logger); return HtmlReporter; }()); exports.default = HtmlReporter; //# sourceMappingURL=HtmlReporter.js.map