Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snippets: correctly escape backslashes in generated expressions #1995

Merged
merged 5 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions features/step_definition_snippets.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,20 @@ Feature: step definition snippets
return 'pending';
});
"""

Scenario: a step resulting in special characters in the expression
Given a file named "features/number.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a person's (secret) desires
"""
When I run cucumber-js
Then it fails
And the output contains the text:
"""
Given('a person\'s \\(secret) desires', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
"""
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GeneratedExpression } from '@cucumber/cucumber-expressions'
import {
ISnippetSnytax,
ISnippetSyntaxBuildOptions,
Expand Down Expand Up @@ -43,9 +44,8 @@ export default class JavaScriptSnippetSyntax implements ISnippetSnytax {
if (this.snippetInterface === SnippetInterface.Callback) {
allParameterNames.push(CALLBACK_NAME)
}
return `${prefix + functionName}('${generatedExpression.source.replace(
/'/g,
"\\'"
return `${prefix + functionName}('${this.escapeSpecialCharacters(
generatedExpression
)}', ${functionKeyword}(${allParameterNames.join(', ')}) {\n`
}
)
Expand All @@ -56,4 +56,13 @@ export default class JavaScriptSnippetSyntax implements ISnippetSnytax {
'});'
)
}

private escapeSpecialCharacters(generatedExpression: GeneratedExpression) {
let source = generatedExpression.source
// double up any backslashes because we're in a javascript string
source = source.replace(/\\/g, '\\\\')
// escape any single quotes because that's our quote delimiter
source = source.replace(/'/g, "\\'")
return source
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ describe('JavascriptSnippetSyntax', () => {
})
})

describe('pattern contains escapes', () => {
it('returns the proper snippet', () => {
// Arrange
const syntax = new JavascriptSnippetSyntax(SnippetInterface.Synchronous)
const buildOptions: ISnippetSyntaxBuildOptions = {
comment: 'comment',
functionName: 'functionName',
generatedExpressions: generateExpressions(
'the user (with permissions) executes the action'
),
stepParameterNames: [],
}

// Act
const result = syntax.build(buildOptions)

// Assert
expect(result).to.eql(
reindent(`
functionName('the user \\\\(with permissions) executes the action', function () {
// comment
return 'pending';
});`)
)
})
})

describe('multiple patterns', () => {
it('returns the snippet with the other choices commented out', function () {
// Arrange
Expand Down