Skip to content

Commit

Permalink
Merge pull request #50 from ronaldvanrij/master
Browse files Browse the repository at this point in the history
Include defaults in interpolation
  • Loading branch information
rsandell authored Oct 25, 2018
2 parents fa8e83f + 5d2f576 commit f13d847
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ protected Map<String, Object> 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());
Expand Down Expand Up @@ -92,7 +95,6 @@ protected Map<String, Object> doRun() throws Exception {
}

Map<String, Object> result = new HashMap<>();
addAll(step.getDefaults(), result);
addAll(properties, result);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f13d847

Please sign in to comment.