Skip to content

Commit 4f2bed6

Browse files
authored
🐛 Fix JENKINS-69219 (#134)
Added check for delimited vs. non-delimited tokens
1 parent a3b19cc commit 4f2bed6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/main/java/org/jenkinsci/plugins/tokenmacro/Parser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ private void parseDelimitedToken(CharacterIterator c) throws MacroEvaluationExce
175175
throw new MacroEvaluationException("Missing } in macro usage");
176176
}
177177

178-
processToken(c.getIndex());
178+
processToken(c.getIndex(), true);
179179
c.next();
180180
}
181181

182182
private void parseNonDelimitedToken(CharacterIterator c) throws MacroEvaluationException, IOException, InterruptedException {
183183
String token = parseIdentifier(c);
184184
if(StringUtils.isNotBlank(token)) {
185185
startToken(token);
186-
processToken(c.getIndex());
186+
processToken(c.getIndex(), false);
187187
}
188188
}
189189

@@ -450,7 +450,7 @@ boolean addTransform(Transform t) {
450450
return true;
451451
}
452452

453-
boolean processToken(int currentIndex) throws IOException, InterruptedException, MacroEvaluationException {
453+
boolean processToken(int currentIndex, boolean isDelimited) throws IOException, InterruptedException, MacroEvaluationException {
454454
String replacement = null;
455455

456456
List<TokenMacro> all = new ArrayList<TokenMacro>(TokenMacro.all());
@@ -492,7 +492,7 @@ boolean processToken(int currentIndex) throws IOException, InterruptedException,
492492
throw new MacroEvaluationException(String.format("Unrecognized macro '%s' in '%s'", tokenName, stringWithMacro));
493493

494494
if (replacement == null && !throwException) { // just put the token back in since we don't want to throw the exception
495-
output.append(stringWithMacro.substring(tokenStartIndex, currentIndex+1));
495+
output.append(stringWithMacro.substring(tokenStartIndex, currentIndex+(isDelimited ? 1 : 0)));
496496
} else if (replacement != null) {
497497
while(transforms != null && transforms.size() > 0) {
498498
Transform t = transforms.pop();

src/test/java/org/jenkinsci/plugins/tokenmacro/TokenMacroTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ public void testNoToken() throws Exception {
235235
assertEquals("^false$",TokenMacro.expandAll(b, TaskListener.NULL, "^false$"));
236236
}
237237

238+
@Test
239+
@Issue("JENKINS-68219")
240+
public void testAddedCharacter() throws Exception {
241+
FreeStyleProject p = j.createFreeStyleProject("foo");
242+
FreeStyleBuild b = p.scheduleBuild2(0).get();
243+
244+
listener = StreamTaskListener.fromStdout();
245+
assertEquals("\"$hello/$dear\"",
246+
TokenMacro.expand(b,listener,"\"$hello/$dear\"", false, Collections.EMPTY_LIST));
247+
}
248+
238249
public class PrivateTestMacro extends TokenMacro {
239250
private static final String MACRO_NAME = "TEST_PRIVATE";
240251

0 commit comments

Comments
 (0)