-
Notifications
You must be signed in to change notification settings - Fork 241
/
event-recorder-reporter.ts
72 lines (56 loc) · 2.43 KB
/
event-recorder-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
68
69
70
71
72
import path from 'path';
import { promises as fsPromises } from 'fs';
import { StrykerOptions } from '@stryker-mutator/api/core';
import { Logger } from '@stryker-mutator/api/logging';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { MatchedMutant, MutantResult, mutationTestReportSchema, Reporter, SourceFile } from '@stryker-mutator/api/report';
import { cleanFolder } from '../utils/file-utils';
import { StrictReporter } from './strict-reporter';
export class EventRecorderReporter implements StrictReporter {
public static readonly inject = tokens(commonTokens.logger, commonTokens.options);
private readonly allWork: Array<Promise<void>> = [];
private readonly createBaseFolderTask: Promise<string | undefined>;
private index = 0;
constructor(private readonly log: Logger, private readonly options: StrykerOptions) {
this.createBaseFolderTask = cleanFolder(this.options.eventReporter.baseDir);
}
private writeToFile(methodName: keyof Reporter, data: any) {
const filename = path.join(this.options.eventReporter.baseDir, `${this.format(this.index++)}-${methodName}.json`);
this.log.debug(`Writing event ${methodName} to file ${filename}`);
return fsPromises.writeFile(filename, JSON.stringify(data), { encoding: 'utf8' });
}
private format(input: number) {
let str = input.toString();
for (let i = 10000; i > 1; i = i / 10) {
if (i > input) {
str = '0' + str;
}
}
return str;
}
private work(eventName: keyof Reporter, data: any) {
this.allWork.push(this.createBaseFolderTask.then(() => this.writeToFile(eventName, data)));
}
public onSourceFileRead(file: SourceFile): void {
this.work('onSourceFileRead', file);
}
public onAllSourceFilesRead(files: SourceFile[]): void {
this.work('onAllSourceFilesRead', files);
}
public onAllMutantsMatchedWithTests(results: readonly MatchedMutant[]): void {
this.work('onAllMutantsMatchedWithTests', results);
}
public onMutantTested(result: MutantResult): void {
this.work('onMutantTested', result);
}
public onMutationTestReportReady(report: mutationTestReportSchema.MutationTestResult): void {
this.work('onMutationTestReportReady', report);
}
public onAllMutantsTested(results: MutantResult[]): void {
this.work('onAllMutantsTested', results);
}
public async wrapUp(): Promise<void[]> {
await this.createBaseFolderTask;
return Promise.all(this.allWork);
}
}