1 | import { DryRunOptions, DryRunResult, MutantRunOptions, MutantRunResult, TestRunnerCapabilities } from '@stryker-mutator/api/test-runner';
|
2 |
|
3 | import { TestRunnerDecorator } from './test-runner-decorator.js';
|
4 |
|
5 | enum TestEnvironmentState {
|
6 | Pristine,
|
7 | Loaded,
|
8 | LoadedStaticMutant,
|
9 | }
|
10 |
|
11 | export class ReloadEnvironmentDecorator extends TestRunnerDecorator {
|
12 | private _capabilities?: TestRunnerCapabilities;
|
13 | private testEnvironment = TestEnvironmentState.Pristine;
|
14 |
|
15 | public override async capabilities(): Promise<TestRunnerCapabilities> {
|
16 | if (!this._capabilities) {
|
17 | this._capabilities = await super.capabilities();
|
18 | }
|
19 | return this._capabilities;
|
20 | }
|
21 |
|
22 | public override async dryRun(options: DryRunOptions): Promise<DryRunResult> {
|
23 | this.testEnvironment = TestEnvironmentState.Loaded;
|
24 | return super.dryRun(options);
|
25 | }
|
26 |
|
27 | public override async mutantRun(options: MutantRunOptions): Promise<MutantRunResult> {
|
28 | let newState: TestEnvironmentState;
|
29 | if (options.reloadEnvironment) {
|
30 | newState = TestEnvironmentState.LoadedStaticMutant;
|
31 |
|
32 |
|
33 | options.reloadEnvironment = this.testEnvironment !== TestEnvironmentState.Pristine;
|
34 |
|
35 | if (options.reloadEnvironment && !(await this.testRunnerIsCapableOfReload())) {
|
36 | await this.recover();
|
37 | options.reloadEnvironment = false;
|
38 | }
|
39 | } else {
|
40 |
|
41 | newState = TestEnvironmentState.Loaded;
|
42 | if (this.testEnvironment === TestEnvironmentState.LoadedStaticMutant) {
|
43 |
|
44 | if (await this.testRunnerIsCapableOfReload()) {
|
45 | options.reloadEnvironment = true;
|
46 | } else {
|
47 |
|
48 | await this.recover();
|
49 | }
|
50 | }
|
51 | }
|
52 | const result = await super.mutantRun(options);
|
53 | this.testEnvironment = newState;
|
54 | return result;
|
55 | }
|
56 |
|
57 | private async testRunnerIsCapableOfReload() {
|
58 | return (await this.capabilities()).reloadEnvironment;
|
59 | }
|
60 | }
|