Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-47347] accept an optional pretty parameter for writeJSON step #33

Merged
merged 2 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import hudson.Extension;
import hudson.Util;
import net.sf.json.JSON;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Writes a {@link JSON} object to file in the current working directory.
Expand All @@ -39,6 +40,7 @@ public class WriteJSONStep extends AbstractStepImpl {

private final String file;
private final JSON json;
private int pretty = 0;

@DataBoundConstructor
public WriteJSONStep(String file, JSON json) {
Expand All @@ -64,6 +66,26 @@ public JSON getJson() {
return json;
}

/**
* Return the number of spaces used to prettify the JSON dump.
*
* @return a int
*/
public int getPretty() {
return pretty;
}

/**
* Indents to use if the JSON should be pretty printed.
* A greater than zero integer will do so.
*
* @param pretty the indent size
*/
@DataBoundSetter
void setPretty(int pretty) {
this.pretty = pretty;
}

@Extension
public static class DescriptorImpl extends AbstractStepDescriptorImpl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ protected Void run() throws Exception {
}

try (OutputStreamWriter writer = new OutputStreamWriter(path.write())) {
step.getJson().write(writer);
if (step.getPretty() > 0) {
writer.write(step.getJson().toString(step.getPretty()));
} else {
step.getJson().write(writer);
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@
def input = readJSON file: 'myfile.json'
//Do some manipulation
writeJSON file: 'output.json', json: input
// or pretty print it, indented with a configurable number of spaces
writeJSON file: 'output.json', json: input, pretty: 4
</pre></code>
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand Down Expand Up @@ -60,6 +61,39 @@ public class WriteJSONStepTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Test
public void writeFilePretty() throws Exception {
File output = temp.newFile();

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" def json = readJSON text: '{\"a\": {\"1\": true,\"2\": 2}}' \n" +
" writeJSON file: '" + FilenameTestsUtils.toPath(output) + "', json: json, pretty: 4\n" +
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

String lines = new String(Files.readAllBytes(Paths.get(output.toURI())), StandardCharsets.UTF_8);
assertThat(lines.split("\r\n|\r|\n").length, equalTo(4));

}

@Test
public void writeFilePrettyEmpty() throws Exception {
File output = temp.newFile();

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" def json = readJSON text: '{}' \n" +
" writeJSON file: '" + FilenameTestsUtils.toPath(output) + "', json: json, pretty: 4\n" +
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));

String lines = new String(Files.readAllBytes(Paths.get(output.toURI())), StandardCharsets.UTF_8);
assertThat(lines.split("\r\n|\r|\n").length, equalTo(1));
}

@Test
public void writeFile() throws Exception {
int elements = 3;
Expand Down