Skip to content

Commit 52de7e9

Browse files
zbynekslide
authored andcommitted
Fix serialization for FILE macro
1 parent a048c8d commit 52de7e9

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

src/main/java/org/jenkinsci/plugins/tokenmacro/impl/WorkspaceFileMacro.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,51 @@ public MasterToSlaveCallable<String, IOException> getCallable(Run<?, ?> run, Str
8484
} catch (Exception e) {
8585
listener.error("Error retrieving environment: %s", e.getMessage());
8686
}
87-
return new MasterToSlaveCallable<String, IOException>() {
88-
@Override
89-
public String call() throws IOException {
90-
91-
File file = new File(root, path);
92-
if (!file.exists()) {
93-
return String.format(fileNotFoundMessage, path);
94-
}
95-
96-
try {
97-
Charset charset = Charset.forName(charSet);
98-
try (BufferedReader reader =
99-
new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
100-
if (maxLines > 0) {
101-
return reader.lines().limit(maxLines).collect(Collectors.joining("\n"));
102-
} else {
103-
return reader.lines().collect(Collectors.joining("\n"));
104-
}
105-
}
106-
} catch (IOException e) {
107-
return "ERROR: File '" + path + "' could not be read";
108-
}
109-
}
110-
};
87+
return new WorkspaceFileMasterToSlaveCallable(root, path, fileNotFoundMessage, charSet, maxLines);
11188
}
11289

11390
@Override
11491
public boolean hasNestedContent() {
11592
return true;
11693
}
94+
95+
private static class WorkspaceFileMasterToSlaveCallable extends MasterToSlaveCallable<String, IOException> {
96+
private final String root;
97+
private final String path;
98+
private final String fileNotFoundMessage;
99+
private final String charSet;
100+
private final int maxLines;
101+
102+
public WorkspaceFileMasterToSlaveCallable(
103+
String root, String path, String fileNotFoundMessage, String charSet, int maxLines) {
104+
this.root = root;
105+
this.path = path;
106+
this.fileNotFoundMessage = fileNotFoundMessage;
107+
this.charSet = charSet;
108+
this.maxLines = maxLines;
109+
}
110+
111+
@Override
112+
public String call() throws IOException {
113+
114+
File file = new File(root, path);
115+
if (!file.exists()) {
116+
return String.format(fileNotFoundMessage, path);
117+
}
118+
119+
try {
120+
Charset charset = Charset.forName(charSet);
121+
try (BufferedReader reader =
122+
new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
123+
if (maxLines > 0) {
124+
return reader.lines().limit(maxLines).collect(Collectors.joining("\n"));
125+
} else {
126+
return reader.lines().collect(Collectors.joining("\n"));
127+
}
128+
}
129+
} catch (IOException e) {
130+
return "ERROR: File '" + path + "' could not be read";
131+
}
132+
}
133+
}
117134
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import hudson.model.Run;
1212
import hudson.model.TaskListener;
1313
import hudson.remoting.Callable;
14+
import hudson.slaves.DumbSlave;
1415
import hudson.util.StreamTaskListener;
1516
import java.io.File;
1617
import java.io.IOException;
18+
import java.io.OutputStream;
19+
import java.nio.charset.StandardCharsets;
1720
import java.util.Collections;
1821
import java.util.List;
1922
import java.util.Map;
@@ -35,9 +38,11 @@ public class PipelineTest {
3538
@Rule
3639
public final JenkinsRule j = new JenkinsRule();
3740

41+
private DumbSlave agent;
42+
3843
@Before
3944
public void setup() throws Exception {
40-
j.createOnlineSlave(Label.get("agents"));
45+
agent = j.createOnlineSlave(Label.get("agents"));
4146
}
4247

4348
@Test
@@ -72,6 +77,19 @@ public void testWorkspaceNeededWithAgent() throws Exception {
7277
j.assertLogContains("Workspace: foo", run);
7378
}
7479

80+
@Test
81+
public void testFileNeededWithAgent() throws Exception {
82+
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
83+
FilePath workspace = agent.getWorkspaceFor(job);
84+
workspace.mkdirs();
85+
try (OutputStream of = new FilePath(workspace, "in.txt").write()) {
86+
of.write("42".getBytes(StandardCharsets.UTF_8));
87+
}
88+
job.setDefinition(new CpsFlowDefinition(getPipeline("{label 'agents'}", "${FILE,path=\"in.txt\"}"), true));
89+
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
90+
j.assertLogContains("VERSION=42", run);
91+
}
92+
7593
@Test
7694
public void testEscapedExpandAll() throws Exception {
7795
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");

0 commit comments

Comments
 (0)