Skip to content

Commit

Permalink
Support directories containing square brackets
Browse files Browse the repository at this point in the history
Related to #1196 [1].

[1] #1196
  • Loading branch information
badeball committed Sep 22, 2024
1 parent 73637e6 commit e7acd3f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Other changees:

- Generate a temporary messages report in case of `JsonFormatter` errors, relates to [#1161](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1161).

- Support project directories containing square brackets, EG. `/home/[foo] my project/`, relates to [#1196](https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1196).

## v20.1.2

- Updated all dependencies, including esbuild, relates to [#1068](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1068).
Expand Down
17 changes: 17 additions & 0 deletions features/issues/1196 [foo].feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1196

Feature: square brackets in directory name
Scenario:
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() {})
"""
When I run cypress
Then it passes
8 changes: 8 additions & 0 deletions features/support/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ const projectPath = path.join(__dirname, "..", "..");
Before(async function ({ gherkinDocument, pickle }) {
assert(gherkinDocument.uri, "Expected gherkinDocument.uri to be present");

/**
* Using the URI as the directory name of the temporary project, is imperative for the test of
* #1196 to actually test directory names containing square brackets. Consider this before
* changing the following line.
*
* @see features/issues/1196 [foo].feature
* @see https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1196
*/
const relativeUri = path.relative(process.cwd(), gherkinDocument.uri);

const { line } = formatterHelpers.PickleParser.getPickleLocation({
Expand Down
1 change: 1 addition & 0 deletions lib/diagnostics/diagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export async function diagnose(configuration: {
);

const stepDefinitions = await getStepDefinitionPaths(
configuration.cypress.projectRoot,
stepDefinitionPatterns,
);

Expand Down
48 changes: 26 additions & 22 deletions lib/step-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ import {
IPreprocessorConfiguration,
} from "./preprocessor-configuration";

import { ensureIsAbsolute } from "./helpers/paths";
import { ensureIsRelative } from "./helpers/paths";

Check warning on line 18 in lib/step-definitions.ts

View workflow job for this annotation

GitHub Actions / test (^10.0.0)

'ensureIsRelative' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 18 in lib/step-definitions.ts

View workflow job for this annotation

GitHub Actions / test (^11.0.0)

'ensureIsRelative' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 18 in lib/step-definitions.ts

View workflow job for this annotation

GitHub Actions / test (^12.0.0)

'ensureIsRelative' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 18 in lib/step-definitions.ts

View workflow job for this annotation

GitHub Actions / test (^13.0.0)

'ensureIsRelative' is defined but never used. Allowed unused vars must match /^_/u

export async function getStepDefinitionPaths(
prjectRoot: string,
stepDefinitionPatterns: string[],
): Promise<string[]> {
return (
await Promise.all(
stepDefinitionPatterns.map((pattern) =>
glob.glob(pattern, { nodir: true, windowsPathsNoEscape: true }),
glob.glob(pattern, {
cwd: prjectRoot,
absolute: true,
nodir: true,
windowsPathsNoEscape: true,
}),
),
)
).reduce((acum, el) => acum.concat(el), []);
Expand Down Expand Up @@ -84,24 +90,22 @@ export function getStepDefinitionPatterns(

const stepDefinitions = [configuration.preprocessor.stepDefinitions].flat();

return stepDefinitions
.flatMap((pattern) => {
if (pattern.includes("[filepath]") && pattern.includes("[filepart]")) {
throw new Error(
`Pattern cannot contain both [filepath] and [filepart], but got ${util.inspect(
pattern,
)}`,
);
} else if (pattern.includes("[filepath]")) {
return pattern.replace("[filepath]", filepathReplacement);
} else if (pattern.includes("[filepart]")) {
return [
...parts.map((part) => pattern.replace("[filepart]", part)),
path.normalize(pattern.replace("[filepart]", ".")),
];
} else {
return pattern;
}
})
.map((pattern) => ensureIsAbsolute(projectRoot, pattern));
return stepDefinitions.flatMap((pattern) => {
if (pattern.includes("[filepath]") && pattern.includes("[filepart]")) {
throw new Error(
`Pattern cannot contain both [filepath] and [filepart], but got ${util.inspect(
pattern,
)}`,
);
} else if (pattern.includes("[filepath]")) {
return pattern.replace("[filepath]", filepathReplacement);
} else if (pattern.includes("[filepart]")) {
return [
...parts.map((part) => pattern.replace("[filepart]", part)),
path.normalize(pattern.replace("[filepart]", ".")),
];
} else {
return pattern;
}
});
}
9 changes: 3 additions & 6 deletions lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,16 @@ export async function compile(
preprocessor,
},
uri,
);
).map((pattern) => ensureIsRelative(configuration.projectRoot, pattern));

debug(
`for ${inspect(
ensureIsRelative(configuration.projectRoot, uri),
)} yielded patterns ${inspect(
stepDefinitionPatterns.map((pattern) =>
ensureIsRelative(configuration.projectRoot, pattern),
),
)}`,
)} yielded patterns ${inspect(stepDefinitionPatterns)}`,
);

const stepDefinitionPaths = await getStepDefinitionPaths(
configuration.projectRoot,
stepDefinitionPatterns,
);

Expand Down

0 comments on commit e7acd3f

Please sign in to comment.