1 | import { CheckResult } from '@stryker-mutator/api/check';
|
2 | import { MutantRunPlan } from '@stryker-mutator/api/src/core';
|
3 |
|
4 | import { ResourceDecorator } from '../concurrent/index.js';
|
5 |
|
6 | import { CheckerResource } from './checker-resource.js';
|
7 |
|
8 | function toMap(mutantRunPlans: MutantRunPlan[]) {
|
9 | return new Map<string, MutantRunPlan>(mutantRunPlans.map((mutant) => [mutant.mutant.id, mutant]));
|
10 | }
|
11 |
|
12 | export class CheckerFacade extends ResourceDecorator<CheckerResource> {
|
13 | public async check(checkerName: string, mutantRunPlans: MutantRunPlan[]): Promise<Array<[MutantRunPlan, CheckResult]>> {
|
14 | const innerCheckerResult = Object.entries(
|
15 | await this.innerResource.check(
|
16 | checkerName,
|
17 | mutantRunPlans.map((mr) => mr.mutant)
|
18 | )
|
19 | );
|
20 |
|
21 | // Check if the checker returned all the mutants that was given
|
22 | // When a mutant is missing this will be found in the map underneath
|
23 | const mutantRunPlanMap = toMap(mutantRunPlans);
|
24 |
|
25 | const results = innerCheckerResult.map(([id, res]) => {
|
26 | const mutantRunPlan = mutantRunPlanMap.get(id);
|
27 | if (!mutantRunPlan)
|
28 | throw new Error(
|
29 | `Checker "${checkerName}" returned a check result for mutant id "${id}", but a check wasn't requested for it. Stryker asked to check mutant ids: ${mutantRunPlans
|
30 | .map(({ mutant }) => mutant.id)
|
31 | .join(',')}`
|
32 | );
|
33 | return [mutantRunPlan, res] as [MutantRunPlan, CheckResult];
|
34 | });
|
35 |
|
36 | if (mutantRunPlans.length > results.length) {
|
37 | const resultIds = new Set(results.map(([{ mutant }]) => mutant.id));
|
38 | const missingIds = mutantRunPlans.map(({ mutant }) => mutant.id).filter((id) => !resultIds.has(id));
|
39 | throw new Error(
|
40 | `Checker "${checkerName}" was missing check results for mutant ids "${missingIds.join(',')}", while Stryker asked to check them`
|
41 | );
|
42 | }
|
43 |
|
44 | return results;
|
45 | }
|
46 |
|
47 | public async group(checkerName: string, mutantRunPlans: MutantRunPlan[]): Promise<MutantRunPlan[][]> {
|
48 | const mutantIdGroups = await this.innerResource.group(
|
49 | checkerName,
|
50 | mutantRunPlans.map((mr) => mr.mutant)
|
51 | );
|
52 |
|
53 | // Check if the checker returned all the mutants that was given
|
54 | // When a mutant is missing this will be found in the map underneath
|
55 | const mutantRunPlanMap = toMap(mutantRunPlans);
|
56 | const groupedMutantIds = new Set<string>();
|
57 | const groups = mutantIdGroups.map((group) =>
|
58 | group.map((id) => {
|
59 | const mutantRunPlan = mutantRunPlanMap.get(id);
|
60 | groupedMutantIds.add(id);
|
61 | if (!mutantRunPlan)
|
62 | throw new Error(
|
63 | `Checker "${checkerName}" returned a group result for mutant id "${id}", but a group wasn't requested for it. Stryker asked to group mutant ids: ${mutantRunPlans
|
64 | .map(({ mutant }) => mutant.id)
|
65 | .join(',')}!`
|
66 | );
|
67 | return mutantRunPlan;
|
68 | })
|
69 | );
|
70 | if (mutantRunPlans.length > groupedMutantIds.size) {
|
71 | const missingIds = mutantRunPlans.map(({ mutant }) => mutant.id).filter((id) => !groupedMutantIds.has(id));
|
72 | throw new Error(
|
73 | `Checker "${checkerName}" was missing group results for mutant ids "${missingIds.join(',')}", while Stryker asked to group them!`
|
74 | );
|
75 | }
|
76 | return groups;
|
77 | }
|
78 | }
|
79 |
|
\ | No newline at end of file |