all files / src/ SourceFileTreeNode.js

100% Statements 98/98
100% Branches 25/25
100% Functions 20/20
100% Lines 89/89
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              46× 46× 46×             221× 59×   221× 221× 59×     188× 162× 35× 35×   162×                 5052× 1143×   5052× 8907× 5052× 3909×     9586× 1143× 1142×                     14× 14×   14× 42× 42× 42× 42× 42× 42×   14×   36×   21× 21× 21× 33×   21× 21× 10× 10×   10×   21× 21×                    
"use strict";
var report_1 = require('stryker-api/report');
var log4js = require('log4js');
var path = require('path');
var util = require('./util');
var fs = require('fs');
var HandlebarsModel_1 = require('./HandlebarsModel');
var SourceFileTreeLeaf_1 = require('./SourceFileTreeLeaf');
var log = log4js.getLogger('HtmlReporter');
/**
 * Represents a node (directory) which can contain source files which in turn include mutant results
 */
var SourceFileTreeNode = (function () {
    /**
     * @param name - The name of the directory which this node represents
     */
    function SourceFileTreeNode(name) {
        this.name = name;
        this.leafs = [];
        this.children = [];
    }
    /**
     * Adds a source file to this node or one of its children
     * @param file - The file to add
     * @param pathComponents - The path components of the file. Leave empty, should only be used for recursive calls.
     */
    SourceFileTreeNode.prototype.addSourceFile = function (file, pathComponents) {
        if (!pathComponents) {
            pathComponents = file.path.split(path.sep);
        }
        var nextPathComponent = pathComponents.shift() || '';
        if (!pathComponents.length) {
            this.leafs.push(new SourceFileTreeLeaf_1.default(file));
        }
        else {
            var nodeToAddTo = this.children.filter(function (node) { return node.name === nextPathComponent; }).pop();
            if (!nodeToAddTo) {
                nodeToAddTo = new SourceFileTreeNode(nextPathComponent);
                this.children.push(nodeToAddTo);
            }
            nodeToAddTo.addSourceFile(file, pathComponents);
        }
    };
    /**
     * Adds a mutant result to the correct leaf in this node or one of the children
     * Warning: All source files need to be added before mutant results should be added
     * @param result - the mutant result to add
     * @param pathComponents - The path components of the result. Leave empty, should only be used for recursive calls.
     */
    SourceFileTreeNode.prototype.addMutantResult = function (result, pathComponents) {
        if (!pathComponents) {
            pathComponents = result.sourceFilePath.split(path.sep);
        }
        var nextPathComponent = pathComponents.shift();
        var childNode = this.children.filter(function (n) { return n.name === nextPathComponent; }).pop();
        if (childNode) {
            childNode.addMutantResult(result, pathComponents);
        }
        else {
            var leaf = this.leafs.filter(function (leaf) { return leaf.file.path === result.sourceFilePath; }).pop();
            if (leaf) {
                leaf.results.push(result);
            }
            else {
                log.warn("Reported a mutant result for \"" + result.sourceFilePath + "\" but could not find source code for a file with that name. Skipping the result. Result was " + JSON.stringify(result) + ".");
            }
        }
    };
    SourceFileTreeNode.prototype.normalizeNames = function () {
        // Merge together empty container nodes
        while (this.leafs.length === 0 && this.children.length === 1) {
            this.name = path.join(this.name, this.children[0].name);
            this.leafs = this.children[0].leafs;
            this.children = this.children[0].children;
        }
        // Remove illegal path characters
        this.name = this.name.replace(/:/g, '');
        // Recursively do the same
        this.children.forEach(function (child) { return child.normalizeNames(); });
    };
    SourceFileTreeNode.prototype.calculateModel = function (urlPrefix) {
        var totalKilled = 0, totalSurvived = 0, totalTimedOut = 0, totalNoCoverage = 0, totalErrors = 0;
        this.children.forEach(function (child) {
            child.calculateModel("../" + urlPrefix);
            totalKilled += child.model.totalKilled;
            totalTimedOut += child.model.totalTimedOut;
            totalSurvived += child.model.totalSurvived;
            totalNoCoverage += child.model.totalNoCoverage;
            totalErrors += child.model.totalErrors;
        });
        this.leafs.forEach(function (leaf) {
            leaf.calculateModel(urlPrefix);
            totalKilled += leaf.model.totalKilled;
            totalSurvived += leaf.model.totalSurvived;
            totalTimedOut += leaf.model.totalTimedOut;
            totalNoCoverage += leaf.model.totalNoCoverage;
            totalErrors += leaf.model.totalErrors;
        });
        this.model = new HandlebarsModel_1.default(this.name, urlPrefix, this.name + "/index.html", totalKilled, totalTimedOut, totalSurvived, totalNoCoverage, totalErrors);
    };
    SourceFileTreeNode.prototype.writeReportNodeRecursive = function (directory) {
        util.mkdirRecursiveSync(directory);
        fs.writeFileSync(path.join(directory, 'index.html'), util.nodeTemplate(this));
        this.children.forEach(function (child) { return child.writeReportNodeRecursive(path.join(directory, child.name)); });
        this.leafs.forEach(function (leaf) { return leaf.writeFileReport(directory); });
    };
    SourceFileTreeNode.prototype.toString = function (offset) {
        if (offset === void 0) { offset = 0; }
        var prefix = '';
        for (var i = 0; i < offset; i++) {
            prefix += '.';
        }
        var str = "" + prefix + this.name + "\n";
        this.leafs.forEach(function (l) {
            str += prefix + "./" + l.name;
            if (l.results.length) {
                str += ' [';
                l.results.forEach(function (m) { return str += SourceFileTreeNode.mutantStatusToString(m.status); });
                str += ']';
            }
            str += '\n';
        });
        this.children.forEach(function (n) { return str += n.toString(offset + 1); });
        return str;
    };
    SourceFileTreeNode.mutantStatusToString = function (status) {
        switch (status) {
            case report_1.MutantStatus.Killed:
                return '.';
            case report_1.MutantStatus.Survived:
                return 'S';
            case report_1.MutantStatus.TimedOut:
                return 'T';
            case report_1.MutantStatus.NoCoverage:
                return 'O';
            case report_1.MutantStatus.Error:
                return 'E';
        }
    };
    return SourceFileTreeNode;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = SourceFileTreeNode;
//# sourceMappingURL=SourceFileTreeNode.js.map