forked from stryker-mutator/stryker-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-reporter.ts
67 lines (54 loc) · 2.62 KB
/
html-reporter.ts
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
import * as path from 'path';
import { StrykerOptions } from '@stryker-mutator/api/core';
import { Logger } from '@stryker-mutator/api/logging';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { mutationTestReportSchema, Reporter } from '@stryker-mutator/api/report';
import fileUrl = require('file-url');
import * as ReporterUtil from '../reporter-util';
import { bindMutationTestReport } from './templates/bind-mutation-test-report';
const DEFAULT_BASE_FOLDER = path.normalize('reports/mutation/html');
export const RESOURCES_DIR_NAME = 'strykerResources';
export default class HtmlReporter implements Reporter {
private _baseDir!: string;
private mainPromise: Promise<void> | undefined;
constructor(private readonly options: StrykerOptions, private readonly log: Logger) {}
public static readonly inject = tokens(commonTokens.options, commonTokens.logger);
public onMutationTestReportReady(report: mutationTestReportSchema.MutationTestResult) {
this.mainPromise = this.generateReport(report);
}
public wrapUp() {
return this.mainPromise;
}
private async generateReport(report: mutationTestReportSchema.MutationTestResult) {
const indexFileName = path.resolve(this.baseDir, 'index.html');
await this.cleanBaseFolder();
await Promise.all([
ReporterUtil.copyFile(
require.resolve('mutation-testing-elements/dist/mutation-test-elements.js'),
path.resolve(this.baseDir, 'mutation-test-elements.js')
),
ReporterUtil.copyFile(path.resolve(__dirname, 'templates', 'stryker-80x80.png'), path.resolve(this.baseDir, 'stryker-80x80.png')),
ReporterUtil.copyFile(path.resolve(__dirname, 'templates', 'index.html'), path.resolve(this.baseDir, 'index.html')),
ReporterUtil.writeFile(path.resolve(this.baseDir, 'bind-mutation-test-report.js'), bindMutationTestReport(report)),
]);
this.log.info(`Your report can be found at: ${fileUrl(indexFileName)}`);
}
private get baseDir(): string {
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;
}
private async cleanBaseFolder(): Promise<void> {
await ReporterUtil.deleteDir(this.baseDir);
await ReporterUtil.mkdir(this.baseDir);
}
}