Skip to content

Commit

Permalink
[JENKINS-44039] Fix multiline script blocks containing single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Aug 24, 2017
1 parent 1d2fc7f commit 45389a1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
Expand Up @@ -121,7 +121,7 @@ public String toGroovy() {
if (str.indexOf('\n') == -1) {
return "'" + (str.replace("'", "\\'")) + "'";
} else {
return "'''" + (str.replace("'''", "\\'\\'\\'")) + "'''";
return "'''" + (str.replace("'", "\\'")) + "'''";
}
} else if (getValue() != null) {
return getValue().toString();
Expand Down
Expand Up @@ -143,4 +143,16 @@ protected void findErrorInJSON(String expectedError, String jsonName) throws Exc

}

protected void successfulJson(String jsonName) throws Exception {
JsonNode json = JsonLoader.fromString(fileContentsFromResources("json/" + jsonName + ".json"));

assertNotNull("Couldn't parse JSON for " + jsonName, json);
assertFalse("Couldn't parse JSON for " + jsonName, json.size() == 0);
assertFalse("Couldn't parse JSON for " + jsonName, json.isNull());

JSONParser jp = new JSONParser(new SimpleJsonTree(json));
jp.parse();

assertTrue(jp.getErrorCollector().getErrorCount() == 0);
}
}
Expand Up @@ -94,4 +94,10 @@ public void jsonParameterTypeCoercion() throws Exception {
public void jsonMismatchedQuotes() throws Exception {
findErrorInJSON(Messages.JSONParser_InvalidGroovyString("hello\\'"), "jsonMismatchedQuotes");
}

@Issue("JENKINS-44039")
@Test
public void singleQuoteInMultiline() throws Exception {
successfulJson("singleQuoteInMultiline");
}
}
@@ -0,0 +1,31 @@
{"pipeline": {
"agent": {
"type": "any"
},
"stages": [{
"name": "Build",
"branches": [{
"name": "default",
"steps": [{
"name": "sh",
"arguments": [{
"key": "script",
"value": {
"isLiteral": true,
"value": "echo \"42\""
}
}]
}, {
"name": "script",
"arguments": [{
"key": "scriptBlock",
"value": {
"isLiteral": true,
"value": "def initialize = load('jenkins/pipeline/initialize.groovy')\ninitialize()\n\necho 'done initializing'"
}
}]
}]
}]
}]
}
}

0 comments on commit 45389a1

Please sign in to comment.