-
Notifications
You must be signed in to change notification settings - Fork 242
/
Stryker.ts
151 lines (138 loc) · 6.04 KB
/
Stryker.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
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
145
146
147
148
149
150
151
import { getLogger } from 'stryker-api/logging';
import { Config } from 'stryker-api/config';
import { StrykerOptions, MutatorDescriptor } from 'stryker-api/core';
import { MutantResult } from 'stryker-api/report';
import { Mutant } from 'stryker-api/mutant';
import MutantTestMatcher from './MutantTestMatcher';
import InputFileResolver from './input/InputFileResolver';
import ScoreResultCalculator from './ScoreResultCalculator';
import { isPromise } from './utils/objectUtils';
import { TempFolder } from './utils/TempFolder';
import MutatorFacade from './MutatorFacade';
import InitialTestExecutor, { InitialTestRunResult } from './process/InitialTestExecutor';
import MutationTestExecutor from './process/MutationTestExecutor';
import InputFileCollection from './input/InputFileCollection';
import LogConfigurator from './logging/LogConfigurator';
import { Injector } from 'typed-inject';
import { TranspilerFacade } from './transpiler/TranspilerFacade';
import { coreTokens, MainContext, PluginCreator, buildMainInjector } from './di';
import { commonTokens, PluginKind } from 'stryker-api/plugin';
export default class Stryker {
private readonly log = getLogger(Stryker.name);
private readonly injector: Injector<MainContext>;
private get reporter() {
return this.injector.resolve(coreTokens.reporter);
}
private get config(): Readonly<Config> {
return this.injector.resolve(commonTokens.config);
}
private get timer() {
return this.injector.resolve(coreTokens.timer);
}
/**
* The Stryker mutation tester.
* @constructor
* @param {Object} [cliOptions] - Optional options.
*/
constructor(cliOptions: Partial<StrykerOptions>) {
LogConfigurator.configureMainProcess(cliOptions.logLevel, cliOptions.fileLogLevel, cliOptions.allowConsoleColors);
this.injector = buildMainInjector(cliOptions);
// Log level may have changed
const options = this.config;
LogConfigurator.configureMainProcess(options.logLevel, options.fileLogLevel, options.allowConsoleColors);
}
public async runMutationTest(): Promise<MutantResult[]> {
const loggingContext = await LogConfigurator.configureLoggingServer(this.config.logLevel, this.config.fileLogLevel, this.config.allowConsoleColors);
this.timer.reset();
const inputFiles = await this.injector.injectClass(InputFileResolver).resolve();
if (inputFiles.files.length) {
TempFolder.instance().initialize();
const initialTestRunProcess = this.injector
.provideValue(coreTokens.inputFiles, inputFiles)
.provideValue(coreTokens.loggingContext, loggingContext)
.provideValue(commonTokens.produceSourceMaps, this.config.coverageAnalysis !== 'off')
.provideFactory(coreTokens.pluginCreatorTranspiler, PluginCreator.createFactory(PluginKind.Transpiler))
.provideClass(coreTokens.transpiler, TranspilerFacade)
.injectClass(InitialTestExecutor);
const initialTestRunResult = await initialTestRunProcess.run();
const testableMutants = await this.mutate(inputFiles, initialTestRunResult);
if (initialTestRunResult.runResult.tests.length && testableMutants.length) {
const mutationTestExecutor = new MutationTestExecutor(
this.config,
inputFiles.files,
this.injector.resolve(coreTokens.testFramework),
this.reporter,
initialTestRunResult.overheadTimeMS,
loggingContext);
const mutantResults = await mutationTestExecutor.run(testableMutants);
this.reportScore(mutantResults);
await this.wrapUpReporter();
await TempFolder.instance().clean();
await this.logDone();
await LogConfigurator.shutdown();
return mutantResults;
} else {
this.logRemark();
}
}
return Promise.resolve([]);
}
private mutate(input: InputFileCollection, initialTestRunResult: InitialTestRunResult) {
const mutator = new MutatorFacade(this.config);
const allMutants = mutator.mutate(input.filesToMutate);
const includedMutants = this.removeExcludedMutants(allMutants);
this.logMutantCount(includedMutants.length, allMutants.length);
const mutantRunResultMatcher = new MutantTestMatcher(
includedMutants,
input.filesToMutate,
initialTestRunResult.runResult,
initialTestRunResult.sourceMapper,
initialTestRunResult.coverageMaps,
this.config,
this.reporter);
return mutantRunResultMatcher.matchWithMutants();
}
private logMutantCount(includedMutantCount: number, totalMutantCount: number) {
let mutantCountMessage;
if (includedMutantCount) {
mutantCountMessage = `${includedMutantCount} Mutant(s) generated`;
} else {
mutantCountMessage = `It\'s a mutant-free world, nothing to test.`;
}
const numberExcluded = totalMutantCount - includedMutantCount;
if (numberExcluded) {
mutantCountMessage += ` (${numberExcluded} Mutant(s) excluded)`;
}
this.log.info(mutantCountMessage);
}
private removeExcludedMutants(mutants: ReadonlyArray<Mutant>): ReadonlyArray<Mutant> {
if (typeof this.config.mutator === 'string') {
return mutants;
} else {
const mutatorDescriptor = this.config.mutator as MutatorDescriptor;
return mutants.filter(mutant => mutatorDescriptor.excludedMutations.indexOf(mutant.mutatorName) === -1);
}
}
private wrapUpReporter(): Promise<void> {
const maybePromise = this.reporter.wrapUp();
if (isPromise(maybePromise)) {
return maybePromise;
} else {
return Promise.resolve();
}
}
private logDone() {
this.log.info('Done in %s.', this.timer.humanReadableElapsed());
}
private logRemark() {
if (!this.log.isTraceEnabled()) {
this.log.info('Trouble figuring out what went wrong? Try `npx stryker run --fileLogLevel trace --logLevel debug` to get some more info.');
}
}
private reportScore(mutantResults: MutantResult[]) {
const calculator = new ScoreResultCalculator();
const score = calculator.calculate(mutantResults);
this.reporter.onScoreCalculated(score);
calculator.determineExitCode(score, this.config.thresholds);
}
}