Skip to content

Commit cfaa3be

Browse files
authored
Bug fix: config stream is closed when variable substitution is disabled (jenkinsci#2)
1 parent 528382f commit cfaa3be

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/main/java/com/microsoft/jenkins/kubernetes/util/CommonUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ protected Random initialValue() {
5151
*/
5252
public static InputStream replaceMacro(InputStream original,
5353
VariableResolver<String> variableResolver) throws IOException {
54+
if (variableResolver == null) {
55+
return original;
56+
}
5457
try {
55-
if (variableResolver == null) {
56-
return original;
57-
}
5858
String content = IOUtils.toString(original, Constants.DEFAULT_CHARSET);
5959
content = Util.replaceMacro(content, variableResolver);
6060
if (content != null) {

src/test/java/com/microsoft/jenkins/kubernetes/util/CommonUtilsTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.Test;
1313

1414
import java.io.ByteArrayInputStream;
15+
import java.io.FileInputStream;
1516
import java.io.InputStream;
1617
import java.util.Map;
1718
import java.util.Random;
@@ -26,6 +27,7 @@
2627
public class CommonUtilsTest {
2728
@Test
2829
public void testReplaceMacro() throws Exception {
30+
testReplaceMacro("abcd", "abcd", null);
2931
testReplaceMacro("", "", ImmutableMap.<String, String>of());
3032
testReplaceMacro("$a", "$a", ImmutableMap.<String, String>of());
3133
testReplaceMacro("", "", ImmutableMap.of("a", "b"));
@@ -36,10 +38,30 @@ public void testReplaceMacro() throws Exception {
3638

3739
private void testReplaceMacro(String expected, String original, Map<String, String> variables) throws Exception {
3840
ByteArrayInputStream in = new ByteArrayInputStream(original.getBytes(Constants.DEFAULT_CHARSET));
39-
InputStream result = CommonUtils.replaceMacro(in, new VariableResolver.ByMap<>(variables));
41+
InputStream result = CommonUtils.replaceMacro(in, variables == null ? null : new VariableResolver.ByMap<>(variables));
4042
assertEquals(expected, IOUtils.toString(result, Constants.DEFAULT_CHARSET));
4143
}
4244

45+
@Test
46+
public void testFilestream() throws Exception {
47+
InputStream in = CommonUtilsTest.class.getResourceAsStream("CommonUtilsTest.data");
48+
try {
49+
assertEquals("${name}", IOUtils.toString(CommonUtils.replaceMacro(in, null)));
50+
} finally {
51+
if (in != null) {
52+
in.close();
53+
}
54+
}
55+
in = CommonUtils.class.getResourceAsStream("CommonUtilsTest.data");
56+
try {
57+
assertEquals("Common", IOUtils.toString(CommonUtils.replaceMacro(in, new VariableResolver.ByMap<String>(ImmutableMap.of("name", "Common")))));
58+
} finally {
59+
if (in != null) {
60+
in.close();
61+
}
62+
}
63+
}
64+
4365
@Test
4466
public void testRandomString() {
4567
assertEquals(16, CommonUtils.randomString().length());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${name}

0 commit comments

Comments
 (0)