-
Notifications
You must be signed in to change notification settings - Fork 159
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
DXCDT-56: Fixing @@ array replacement to work with and without wrapped quotes #421
Conversation
…target regex pattern with or without wrapped strings, added tests
…dFile to loadFileAndReplaceKeywords
@@ -4,18 +4,32 @@ import dotProp from 'dot-prop'; | |||
import _ from 'lodash'; | |||
import log from './logger'; | |||
|
|||
export function keywordArrayReplace(input, mappings) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separated out and exported to enable unit testing.
const pattern = `@@${key}@@`; | ||
const patternWithQuotes = `"${pattern}"`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key functional change in this PR is using regex to replace the @@
pattern with and without wrapping quotes.
const patternWithQuotes = `"${pattern}"`; | ||
|
||
const regex = new RegExp(`${patternWithQuotes}|${pattern}`, 'g'); | ||
input = input.replace(regex, JSON.stringify(mappings[key])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key difference between this function and the below keywordStringReplace
function is the JSON.stringify
call.
stringReplaceNoQuotes: ##STRING_REPLACEMENT## | ||
stringReplaceWithQuotes: "##STRING_REPLACEMENT##" | ||
arrayReplace: @@ARRAY_REPLACEMENT@@ | ||
arrayReplaceWithQuotes: "@@ARRAY_REPLACEMENT@@" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlikely that someone would wrap this value in quotes when using YAML, but at least it's technically supported. I don't believe this behavior will interfere with any current customer workflows.
const inputJSON = '{ "arrayReplaceNoQuotes": @@ARRAY_REPLACEMENT@@, "arrayReplaceWithQuotes": "@@ARRAY_REPLACEMENT@@", "stringReplace": "##STRING_REPLACEMENT##", "noReplace": "OTHER_REPLACEMENT" }'; | ||
const output = utils.keywordReplace(inputJSON, mapping); | ||
|
||
expect(() => JSON.parse(output)).to.not.throw(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the big test, just making sure the output JSON is validated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏻
…nction Distinguishing between two loadFile functions
✏️ Changes
The way that the
@@FOO@@
array variable replacement worked has caused a bit of confusion from users. Firstly, it's not obvious if these markers need to be wrapped in quotes or not. Without them, it invalidates the JSON file that they're used in, but if they do use quotes, the replacement won't work as expected. This can be seen in #415 and #184.For example, as it currently is implemented, developers would need to have the following configurations:
This is invalid JSON and a bit of a wonky thing to force onto the developer. With this change, they can now use the array replacements with or without wrapped quotes:
The tricky part here is that this needs to work both for JSON and YAML input files. This fix is primarily for JSON configurations to maintain the validity of the file. However, YAML is a bit less strict about having the @@ array markers wrapped in quotes, however that is still supported, albeit not with a desired outcome. Examples of this can be seen in the test YAML test cases below.
A big bulk of this PR is adding tests to confirm and enforce the behavior that exists currently.
🎯 Testing
Separated out
keywordArrayReplace
andkeywordStringReplace
into their own functions so that they could have their own unit tests applied. Also added higher-level unit tests to thekeywordReplace
function to confirm expected results for both YAML and JSON inputs.✅This change has unit test coverage