diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java index 06bae898..08cbae98 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepExecution.java @@ -62,6 +62,9 @@ protected Map doRun() throws Exception { PrintStream logger = getLogger(); Properties properties = new Properties(); + if (step.getDefaults() != null) { + properties.putAll(step.getDefaults()); + } if (!StringUtils.isBlank(step.getFile())) { FilePath f = ws.child(step.getFile()); @@ -92,7 +95,6 @@ protected Map doRun() throws Exception { } Map result = new HashMap<>(); - addAll(step.getDefaults(), result); addAll(properties, result); return result; } diff --git a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java index c6519a00..c1155bcd 100644 --- a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadPropertiesStepTest.java @@ -269,6 +269,42 @@ public void readFileAndTextInterpolated() throws Exception { "}", true)); j.assertBuildStatusSuccess(p.scheduleBuild2(0)); } + + @Test + public void readFileAndTextAndDefaultsInterpolated() throws Exception { + Properties props = new Properties(); + props.setProperty("test", "One"); + props.setProperty("another", "Two"); + File file = temp.newFile(); + try (FileWriter f = new FileWriter(file)) { + props.store(f, "Pipeline test"); + } + + props = new Properties(); + props.setProperty("file", "book.txt"); + props.setProperty("fileUrl", "${url}/${file}"); + File textFile = temp.newFile(); + try (FileWriter f = new FileWriter(textFile)) { + props.store(f, "Pipeline test"); + } + + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition( + "node('slaves') {\n" + + " String propsText = readFile file: '" + separatorsToSystemEscaped(textFile.getAbsolutePath()) + "'\n" + + " def defaults = [\"url\": \"http://localhost\"]\n" + + " def props = readProperties interpolate: true, defaults: defaults, text: propsText, file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" + + " assert props['test'] == 'One'\n" + + " assert props.test == 'One'\n" + + " assert props['url'] == 'http://localhost'\n" + + " assert props.url == 'http://localhost'\n" + + " assert props['file'] == 'book.txt'\n" + + " assert props.file == 'book.txt'\n" + + " assert props['fileUrl'] == 'http://localhost/book.txt'\n" + + " assert props.fileUrl == 'http://localhost/book.txt'\n" + + "}", true)); + j.assertBuildStatusSuccess(p.scheduleBuild2(0)); + } @Test public void readFileAndTextInterpolatedWithCyclicValues() throws Exception {