Skip to content

Commit bf45772

Browse files
committed
Propagate preprocessor config to all event handlers
Currently, if one event handler needed implicitIntegrationFolder, they wouldn't necessarily be able to, as a lacking configuration might already have been cached by memoization.
1 parent a127f86 commit bf45772

File tree

4 files changed

+156
-106
lines changed

4 files changed

+156
-106
lines changed

lib/add-cucumber-preprocessor-plugin.ts

Lines changed: 92 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from "node:fs";
2+
import path from "node:path";
23
import { inspect } from "node:util";
34

45
import { generateMessages } from "@cucumber/gherkin";
56
import { IdGenerator, SourceMediaType } from "@cucumber/messages";
67
import parse from "@cucumber/tag-expressions";
8+
import { commonAncestorPath as ancestor } from "common-ancestor-path";
79
import { getSpecs } from "find-cypress-specs";
810
import { v4 as uuid } from "uuid";
911

@@ -20,10 +22,9 @@ import {
2022
TASK_TEST_STEP_FINISHED,
2123
TASK_TEST_STEP_STARTED,
2224
} from "./cypress-task-definitions";
23-
import { assertNever } from "./helpers/assertions";
25+
import { assertNever, ensure } from "./helpers/assertions";
2426
import debug from "./helpers/debug";
2527
import { getTags } from "./helpers/environment";
26-
import { memoize } from "./helpers/memoize";
2728
import { notNull } from "./helpers/type-guards";
2829
import {
2930
afterRunHandler,
@@ -43,9 +44,7 @@ import {
4344
testStepFinishedHandler,
4445
testStepStartedHandler,
4546
} from "./plugin-event-handlers";
46-
import { resolve as origResolve } from "./preprocessor-configuration";
47-
48-
const resolve = memoize(origResolve);
47+
import { resolve } from "./preprocessor-configuration";
4948

5049
export type AddOptions = {
5150
omitBeforeRunHandler?: boolean;
@@ -86,56 +85,93 @@ export async function addCucumberPreprocessorPlugin(
8685
) {
8786
config.env[INTERNAL_SUITE_PROPERTIES] = { isEventHandlersAttached: true };
8887

89-
const preprocessor = await resolve(config, config.env, "/");
88+
const specs = getSpecs(config, "foobar" as any, true);
89+
90+
const implicitIntegrationFolder = ensure(
91+
ancestor(...specs.map(path.dirname).map(path.normalize)),
92+
"Expected to find a common ancestor path",
93+
);
94+
95+
const preprocessor = await resolve(
96+
config,
97+
config.env,
98+
implicitIntegrationFolder,
99+
);
90100

91101
if (!options.omitBeforeRunHandler) {
92-
on("before:run", () => beforeRunHandler(config));
102+
on("before:run", () => beforeRunHandler(config, preprocessor));
93103
}
94104

95105
if (!options.omitAfterRunHandler) {
96-
on("after:run", (results) => afterRunHandler(config, results));
106+
on("after:run", (results) =>
107+
afterRunHandler(config, preprocessor, results),
108+
);
97109
}
98110

99111
if (!options.omitBeforeSpecHandler) {
100-
on("before:spec", (spec) => beforeSpecHandler(config, spec));
112+
on("before:spec", (spec) => beforeSpecHandler(config, preprocessor, spec));
101113
}
102114

103115
if (!options.omitAfterSpecHandler) {
104116
on("after:spec", (spec, results) =>
105-
afterSpecHandler(config, spec, results),
117+
afterSpecHandler(config, preprocessor, spec, results),
106118
);
107119
}
108120

109121
if (!options.omitAfterScreenshotHandler) {
110122
on("after:screenshot", (details) =>
111-
afterScreenshotHandler(config, details),
123+
afterScreenshotHandler(config, preprocessor, details),
112124
);
113125
}
114126

115127
on("task", {
116-
[TASK_SPEC_ENVELOPES]: specEnvelopesHandler.bind(null, config),
117-
[TASK_TEST_CASE_STARTED]: testCaseStartedHandler.bind(null, config),
118-
[TASK_TEST_STEP_STARTED]: testStepStartedHandler.bind(null, config),
128+
[TASK_SPEC_ENVELOPES]: specEnvelopesHandler.bind(
129+
null,
130+
config,
131+
preprocessor,
132+
),
133+
[TASK_TEST_CASE_STARTED]: testCaseStartedHandler.bind(
134+
null,
135+
config,
136+
preprocessor,
137+
),
138+
[TASK_TEST_STEP_STARTED]: testStepStartedHandler.bind(
139+
null,
140+
config,
141+
preprocessor,
142+
),
119143
[TASK_TEST_STEP_FINISHED]: testStepFinishedHandler.bind(
120144
null,
121145
config,
146+
preprocessor,
122147
options,
123148
),
124-
[TASK_TEST_RUN_HOOK_STARTED]: testRunHookStartedHandler.bind(null, config),
149+
[TASK_TEST_RUN_HOOK_STARTED]: testRunHookStartedHandler.bind(
150+
null,
151+
config,
152+
preprocessor,
153+
),
125154
[TASK_TEST_RUN_HOOK_FINISHED]: testRunHookFinishedHandler.bind(
126155
null,
127156
config,
157+
preprocessor,
158+
),
159+
[TASK_TEST_CASE_FINISHED]: testCaseFinishedHandler.bind(
160+
null,
161+
config,
162+
preprocessor,
128163
),
129-
[TASK_TEST_CASE_FINISHED]: testCaseFinishedHandler.bind(null, config),
130164
[TASK_CREATE_STRING_ATTACHMENT]: createStringAttachmentHandler.bind(
131165
null,
132166
config,
167+
preprocessor,
133168
),
134169
[TASK_FRONTEND_TRACKING_ERROR]: frontendTrackingErrorHandler.bind(
135170
null,
136171
config,
172+
preprocessor,
137173
),
138-
[TASK_SUGGESTION]: suggestion.bind(null, config),
174+
[TASK_SUGGESTION]: suggestion.bind(null, config, preprocessor),
139175
});
140176

141177
const tags = getTags(config.env);
@@ -145,48 +181,46 @@ export async function addCucumberPreprocessorPlugin(
145181

146182
const node = parse(tags);
147183

148-
const testFiles = getSpecs(config, "foobar" as any, true).filter(
149-
(testFile) => {
150-
if (!testFile.endsWith(".feature")) {
151-
switch (preprocessor.filterSpecsMixedMode) {
152-
case "hide":
153-
return false;
154-
case "show":
155-
return true;
156-
case "empty-set":
157-
return node.evaluate([]);
158-
default:
159-
assertNever(preprocessor.filterSpecsMixedMode);
160-
}
184+
const testFiles = specs.filter((testFile) => {
185+
if (!testFile.endsWith(".feature")) {
186+
switch (preprocessor.filterSpecsMixedMode) {
187+
case "hide":
188+
return false;
189+
case "show":
190+
return true;
191+
case "empty-set":
192+
return node.evaluate([]);
193+
default:
194+
assertNever(preprocessor.filterSpecsMixedMode);
161195
}
162-
163-
const content = fs.readFileSync(testFile).toString("utf-8");
164-
165-
const options = {
166-
includeSource: false,
167-
includeGherkinDocument: false,
168-
includePickles: true,
169-
newId: IdGenerator.incrementing(),
170-
};
171-
172-
const envelopes = generateMessages(
173-
content,
174-
testFile,
175-
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
176-
options,
177-
);
178-
179-
const pickles = envelopes
180-
.map((envelope) => envelope.pickle)
181-
.filter(notNull);
182-
183-
return pickles.some((pickle) =>
184-
node.evaluate(
185-
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
186-
),
187-
);
188-
},
189-
);
196+
}
197+
198+
const content = fs.readFileSync(testFile).toString("utf-8");
199+
200+
const options = {
201+
includeSource: false,
202+
includeGherkinDocument: false,
203+
includePickles: true,
204+
newId: IdGenerator.incrementing(),
205+
};
206+
207+
const envelopes = generateMessages(
208+
content,
209+
testFile,
210+
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
211+
options,
212+
);
213+
214+
const pickles = envelopes
215+
.map((envelope) => envelope.pickle)
216+
.filter(notNull);
217+
218+
return pickles.some((pickle) =>
219+
node.evaluate(
220+
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
221+
),
222+
);
223+
});
190224

191225
debug(`Resolved specs ${inspect(testFiles)}`);
192226

lib/helpers/memoize.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)