Skip to content

Commit

Permalink
Support config file locations other than project root
Browse files Browse the repository at this point in the history
This fixes #1243 [1].

[1] #1243
  • Loading branch information
badeball committed Oct 3, 2024
1 parent a8b0926 commit 10423a8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## Unreleased

- Support config file locations other than project root, fixes [#1243](https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1243).

## v21.0.0

Breaking changes:
Expand Down
7 changes: 6 additions & 1 deletion declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ declare module "find-cypress-specs" {
export function getSpecs(
config: Cypress.ConfigOptions,
type: Cypress.TestingType,
returnAbsolute?: boolean,
): string[];

export function getSpecs(config: Cypress.PluginConfigOptions): string[];
export function getSpecs(
config: Cypress.PluginConfigOptions,
type: Cypress.TestingType,
returnAbsolute?: boolean,
): string[];

export function getConfig(): Cypress.ConfigOptions;
}
21 changes: 21 additions & 0 deletions features/issues/1243.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://github.com/badeball/cypress-cucumber-preprocessor/issues/1243

Feature: custom config location
Scenario: custom config location
Given a file named "cypress/e2e/a.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
Given("a step", function() {})
"""
And a file named "config/cypress.config.js" with:
"""
module.exports = require("../cypress.config.js");
"""
When I run cypress with "--config-file config/cypress.config.js"
Then it passes
80 changes: 41 additions & 39 deletions lib/add-cucumber-preprocessor-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,46 +138,48 @@ export async function addCucumberPreprocessorPlugin(

const node = parse(tags);

const testFiles = getSpecs(config).filter((testFile) => {
if (!testFile.endsWith(".feature")) {
switch (preprocessor.filterSpecsMixedMode) {
case "hide":
return false;
case "show":
return true;
case "empty-set":
return node.evaluate([]);
default:
assertNever(preprocessor.filterSpecsMixedMode);
const testFiles = getSpecs(config, "foobar" as any, true).filter(
(testFile) => {
if (!testFile.endsWith(".feature")) {
switch (preprocessor.filterSpecsMixedMode) {
case "hide":
return false;
case "show":
return true;
case "empty-set":
return node.evaluate([]);
default:
assertNever(preprocessor.filterSpecsMixedMode);
}
}
}

const content = fs.readFileSync(testFile).toString("utf-8");

const options = {
includeSource: false,
includeGherkinDocument: false,
includePickles: true,
newId: IdGenerator.incrementing(),
};

const envelopes = generateMessages(
content,
testFile,
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
options,
);

const pickles = envelopes
.map((envelope) => envelope.pickle)
.filter(notNull);

return pickles.some((pickle) =>
node.evaluate(
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
),
);
});

const content = fs.readFileSync(testFile).toString("utf-8");

const options = {
includeSource: false,
includeGherkinDocument: false,
includePickles: true,
newId: IdGenerator.incrementing(),
};

const envelopes = generateMessages(
content,
testFile,
SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
options,
);

const pickles = envelopes
.map((envelope) => envelope.pickle)
.filter(notNull);

return pickles.some((pickle) =>
node.evaluate(
pickle.tags?.map((tag) => tag.name).filter(notNull) ?? [],
),
);
},
);

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

Expand Down
16 changes: 16 additions & 0 deletions lib/step-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ export function getStepDefinitionPatterns(
>,
filepath: string,
): string[] {
/**
* The reason for these assertions is that when giving relative paths to path.relative, the result
* will depend on CWD, which is affected by EG. the --config-file parameter [1].
*
* [1] https://github.com/badeball/cypress-cucumber-preprocessor/issues/1243
*/
assert(
path.isAbsolute(configuration.implicitIntegrationFolder),
`Expected an absolute path for implicit integration folder but got ${configuration.implicitIntegrationFolder}`,
);

assert(
path.isAbsolute(filepath),
`Expected an absolute path for spec but got ${filepath}`,
);

const filepathReplacement = glob.escape(
trimFeatureExtension(
path.relative(configuration.implicitIntegrationFolder, filepath),
Expand Down
12 changes: 9 additions & 3 deletions lib/subpath-entrypoints/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ export function createEsbuildPlugin(
if (needPrettify) {
debug("esbuild: prettifying sources");

/**
*/
sourceMap.sources = sourceMap.sources.map((source: string) => {
return path.relative(
configuration.projectRoot,
Expand All @@ -95,9 +93,17 @@ export function createEsbuildPlugin(
"base64",
);

/**
* Why `${"sourceMappingURL"}` you may ask. This is so esbuild doesn't crap itself upon
* errors, where it would search for source maps and find THIS code line, which is not a
* valid source map (obvously).
*
* Without this, esbuild would error with "Unexpected token z in JSON at position 0" every
* time an error occurred during build time.
*/
await fs.appendFile(
outfile,
`//# sourceMappingURL=data:application/json;base64,${encoded}\n`,
`//# ${"sourceMappingURL"}=data:application/json;base64,${encoded}\n`,
);
});
}
Expand Down
6 changes: 5 additions & 1 deletion lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export async function compile(
const pickles = envelopes.map((envelope) => envelope.pickle).filter(notNull);

const implicitIntegrationFolder = assertAndReturn(
ancestor(...getSpecs(configuration).map(path.dirname).map(path.normalize)),
ancestor(
...getSpecs(configuration, "foobar" as any, true)
.map(path.dirname)
.map(path.normalize),
),
"Expected to find a common ancestor path",
);

Expand Down

0 comments on commit 10423a8

Please sign in to comment.