Skip to content

Commit 9d18895

Browse files
committed
Support directories containing square brackets
Related to #1196 [1]. [1] #1196
1 parent 73637e6 commit 9d18895

File tree

6 files changed

+56
-29
lines changed

6 files changed

+56
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Other changees:
2020

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

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

2527
- Updated all dependencies, including esbuild, relates to [#1068](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1068).

features/issues/1196 [foo].feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://github.com/badeball/cypress-cucumber-preprocessor/discussions/1196
2+
3+
Feature: square brackets in directory name
4+
Scenario:
5+
Given a file named "cypress/e2e/a.feature" with:
6+
"""
7+
Feature: a feature
8+
Scenario: a scenario
9+
Given a step
10+
"""
11+
And a file named "cypress/support/step_definitions/steps.js" with:
12+
"""
13+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
14+
Given("a step", function() {})
15+
"""
16+
When I run cypress
17+
Then it passes

features/support/hooks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const projectPath = path.join(__dirname, "..", "..");
99
Before(async function ({ gherkinDocument, pickle }) {
1010
assert(gherkinDocument.uri, "Expected gherkinDocument.uri to be present");
1111

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

1422
const { line } = formatterHelpers.PickleParser.getPickleLocation({

lib/diagnostics/diagnose.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export async function diagnose(configuration: {
115115
);
116116

117117
const stepDefinitions = await getStepDefinitionPaths(
118+
configuration.cypress.projectRoot,
118119
stepDefinitionPatterns,
119120
);
120121

lib/step-definitions.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ import {
1515
IPreprocessorConfiguration,
1616
} from "./preprocessor-configuration";
1717

18-
import { ensureIsAbsolute } from "./helpers/paths";
19-
2018
export async function getStepDefinitionPaths(
19+
prjectRoot: string,
2120
stepDefinitionPatterns: string[],
2221
): Promise<string[]> {
2322
return (
2423
await Promise.all(
2524
stepDefinitionPatterns.map((pattern) =>
26-
glob.glob(pattern, { nodir: true, windowsPathsNoEscape: true }),
25+
glob.glob(pattern, {
26+
cwd: prjectRoot,
27+
absolute: true,
28+
nodir: true,
29+
windowsPathsNoEscape: true,
30+
}),
2731
),
2832
)
2933
).reduce((acum, el) => acum.concat(el), []);
@@ -84,24 +88,22 @@ export function getStepDefinitionPatterns(
8488

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

87-
return stepDefinitions
88-
.flatMap((pattern) => {
89-
if (pattern.includes("[filepath]") && pattern.includes("[filepart]")) {
90-
throw new Error(
91-
`Pattern cannot contain both [filepath] and [filepart], but got ${util.inspect(
92-
pattern,
93-
)}`,
94-
);
95-
} else if (pattern.includes("[filepath]")) {
96-
return pattern.replace("[filepath]", filepathReplacement);
97-
} else if (pattern.includes("[filepart]")) {
98-
return [
99-
...parts.map((part) => pattern.replace("[filepart]", part)),
100-
path.normalize(pattern.replace("[filepart]", ".")),
101-
];
102-
} else {
103-
return pattern;
104-
}
105-
})
106-
.map((pattern) => ensureIsAbsolute(projectRoot, pattern));
91+
return stepDefinitions.flatMap((pattern) => {
92+
if (pattern.includes("[filepath]") && pattern.includes("[filepart]")) {
93+
throw new Error(
94+
`Pattern cannot contain both [filepath] and [filepart], but got ${util.inspect(
95+
pattern,
96+
)}`,
97+
);
98+
} else if (pattern.includes("[filepath]")) {
99+
return pattern.replace("[filepath]", filepathReplacement);
100+
} else if (pattern.includes("[filepart]")) {
101+
return [
102+
...parts.map((part) => pattern.replace("[filepart]", part)),
103+
path.normalize(pattern.replace("[filepart]", ".")),
104+
];
105+
} else {
106+
return pattern;
107+
}
108+
});
107109
}

lib/template.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,16 @@ export async function compile(
9393
preprocessor,
9494
},
9595
uri,
96-
);
96+
).map((pattern) => ensureIsRelative(configuration.projectRoot, pattern));
9797

9898
debug(
9999
`for ${inspect(
100100
ensureIsRelative(configuration.projectRoot, uri),
101-
)} yielded patterns ${inspect(
102-
stepDefinitionPatterns.map((pattern) =>
103-
ensureIsRelative(configuration.projectRoot, pattern),
104-
),
105-
)}`,
101+
)} yielded patterns ${inspect(stepDefinitionPatterns)}`,
106102
);
107103

108104
const stepDefinitionPaths = await getStepDefinitionPaths(
105+
configuration.projectRoot,
109106
stepDefinitionPatterns,
110107
);
111108

0 commit comments

Comments
 (0)