-
Notifications
You must be signed in to change notification settings - Fork 14
/
report-html.js
98 lines (87 loc) · 2.96 KB
/
report-html.js
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
import CoverageData from '../services/coverage-data';
import Core from '../services/core';
import fs from 'fs';
import path from 'path';
import ReportCommon from './report-common';
import Log from './../context/log';
const Report = Npm.require('istanbul-lib-report'),
ReportImpl = Npm.require('istanbul-reports');
export default class {
constructor(res, options) {
this.res = res;
this.options = options;
this.prefix = options.prefix;
this.options.subdir = this.options.path;
this.opts = this.generateOpts();
this.report = ReportImpl.create('html', this.opts);
}
generateOpts() {
const outputPath = this.options.path;
return {
verbose: this.options.verbose,
linkMapper: {
getPath: function (node) {
/* istanbul ignore else */
if (typeof node === 'string') {
return node;
}
var filePath = node.getQualifiedName();
if (node.isSummary()) {
filePath = path.join(outputPath, 'index.html');
} else {
filePath = path.join(outputPath, filePath + '.html');
}
return filePath;
},
relativePath: function (source, target) {
return this.getPath(target);
},
assetPath: function (node, name) {
return path.join(outputPath, name);
}
}
};
}
generate() {
const folderPath = this.options.path;
this.copyStatic();
var coverage = Core.getCoverageObject();
/* istanbul ignore else */
if (!(coverage && Object.keys(coverage).length > 0)) {
this.res.statusCode = 500;
return this.res.end('{"type":"failed", "message": "No coverage information have been collected"}');
}
var root = CoverageData.getTreeReport(coverage);
let filepath = path.join(folderPath, 'index.html');
this.report.onSummary(root, ReportCommon.getContext(filepath));
const childrens = root.getChildren();
const report = this.report;
// Todo : use future
childrens.forEach(function (child) {
var filepath = path.join(folderPath, child.getRelativeName() + '.html');
Log.info('Creating a new html report', filepath);
let fileReport = CoverageData.getFileReport(coverage, child.getRelativeName());
report.onDetail(fileReport, ReportCommon.getContext(filepath));
});
this.res.end('{"type":"success"}');
}
copyStatic() {
ReportCommon.checkDirectory(this.options.path);
this.report.onStart(null, this.getFolderContext(this.options.path));
}
getFolderContext(folderpath) {
var context = Report.createContext();
Object.defineProperty(context, 'writer', {
value: {
copyFile: function (sourcePath, destPath) {
// fix no asset while using test runner
// do not use async - nothing is awaiting us
const data = fs.readFileSync(sourcePath);
let p = path.join(folderpath, destPath);
fs.writeFileSync(p, data);
}
}
});
return context;
}
}