1 | import { Checker, CheckResult } from '@stryker-mutator/api/check';
|
2 | import { StrykerOptions, Mutant } from '@stryker-mutator/api/core';
|
3 | import { PluginKind, tokens, commonTokens } from '@stryker-mutator/api/plugin';
|
4 | import { StrykerError } from '@stryker-mutator/util';
|
5 |
|
6 | import { coreTokens, PluginCreator } from '../di/index.js';
|
7 |
|
8 | import { CheckerResource } from './checker-resource.js';
|
9 |
|
10 | export class CheckerWorker implements CheckerResource {
|
11 | private readonly innerCheckers: Map<string, Checker>;
|
12 |
|
13 | public static inject = tokens(commonTokens.options, coreTokens.pluginCreator);
|
14 | constructor(options: StrykerOptions, pluginCreator: PluginCreator) {
|
15 | this.innerCheckers = new Map(options.checkers.map((name) => [name, pluginCreator.create(PluginKind.Checker, name)]));
|
16 | }
|
17 | public async init(): Promise<void> {
|
18 | for await (const [name, checker] of this.innerCheckers.entries()) {
|
19 | try {
|
20 | await checker.init();
|
21 | } catch (error: unknown) {
|
22 | throw new StrykerError(`An error occurred during initialization of the "${name}" checker`, error);
|
23 | }
|
24 | }
|
25 | }
|
26 | public async check(checkerName: string, mutants: Mutant[]): Promise<Record<string, CheckResult>> {
|
27 | return this.perform(checkerName, (checker) => checker.check(mutants));
|
28 | }
|
29 |
|
30 | public async group(checkerName: string, mutants: Mutant[]): Promise<string[][]> {
|
31 | return this.perform(
|
32 | checkerName,
|
33 | (checker) =>
|
34 | checker.group?.(mutants) ??
|
35 |
|
36 | mutants.map(({ id }) => [id])
|
37 | );
|
38 | }
|
39 |
|
40 | private perform<T>(checkerName: string, act: (checker: Checker) => T) {
|
41 | const checker = this.innerCheckers.get(checkerName);
|
42 | if (checker) {
|
43 | return act(checker);
|
44 | } else {
|
45 | throw new Error(`Checker ${checkerName} does not exist!`);
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
\ | No newline at end of file |