all files / src/ SourceFileTreeNode.js

100% Statements 93/93
100% Branches 22/22
100% Functions 20/20
100% Lines 84/84
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  33× 33× 33×   200× 48×   200× 200× 48×     177× 152× 24× 24×   152×     4328× 990×   4328× 7751× 4328× 3338×     8215× 990× 989×                 37× 37× 37× 37×     37×   20× 20× 20× 32×   20× 20×     20× 20×                  
"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');
var SourceFileTreeNode = (function () {
    function SourceFileTreeNode(name) {
        this.name = name;
        this.leafs = [];
        this.children = [];
    }
    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);
        }
    };
    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 () {
        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;
        }
        this.name = this.name.replace(/:/g, '');
        this.children.forEach(function (child) { return child.normalizeNames(); });
    };
    SourceFileTreeNode.prototype.calculateModel = function (urlPrefix) {
        var totalKilled = 0, totalSurvived = 0, totalUntested = 0;
        this.children.forEach(function (child) {
            child.calculateModel("../" + urlPrefix);
            totalKilled += child.model.totalKilled;
            totalSurvived += child.model.totalSurvived;
            totalUntested += child.model.totalUntested;
        });
        this.leafs.forEach(function (leaf) {
            leaf.calculateModel(urlPrefix);
            totalKilled += leaf.model.totalKilled;
            totalSurvived += leaf.model.totalSurvived;
            totalUntested += leaf.model.totalUntested;
        });
        this.model = new HandlebarsModel_1.default(this.name, urlPrefix, this.name + "/index.html", totalKilled, totalSurvived, totalUntested);
    };
    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.UNTESTED:
                return 'O';
        }
    };
    return SourceFileTreeNode;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = SourceFileTreeNode;
//# sourceMappingURL=SourceFileTreeNode.js.map