Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 1e76537

Browse files
committed
Updated review comments.
1 parent 34ba3a5 commit 1e76537

File tree

10 files changed

+102
-109
lines changed

10 files changed

+102
-109
lines changed

src/main/java/com/mathworks/ci/MatlabBuild.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,34 @@
1313
import hudson.FilePath;
1414
import hudson.Launcher;
1515
import hudson.Launcher.ProcStarter;
16+
import hudson.model.Computer;
1617
import hudson.model.TaskListener;
1718

1819
public interface MatlabBuild {
1920

2021
/**
2122
* This Method decorates the launcher with MATLAB command provided and returns the Process
2223
* object to launch MATLAB with appropriate startup options like -r or -batch
23-
* @param workspace
24-
* @param launcher
25-
* @param listener
26-
* @param envVars
27-
* @param matlabCommand
28-
* @return
29-
* @throws IOException
30-
* @throws InterruptedException
24+
* @param workspace Current build workspace
25+
* @param launcher Current build launcher
26+
* @param listener Current build listener
27+
* @param envVars Environment variables of the current build
28+
* @param matlabCommand MATLAB command to execute on shell
29+
* @return matlabLauncher returns the process launcher to run MATLAB commands
3130
*/
3231
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException {
32+
//Get node specific tmp directory to copy matlab runner script
33+
String tmpDir = getNodeSpecificTmpFolderPath();
34+
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
3335
ProcStarter matlabLauncher;
34-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars);
35-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
3636
if(launcher.isUnix()) {
37-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("./run_matlab_command.sh",matlabCommand).stdout(listener);
37+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/run_matlab_command.sh",matlabCommand).stdout(listener);
38+
3839
//Copy runner .sh for linux platform in workspace.
3940
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace);
4041
}else {
4142
launcher = launcher.decorateByPrefix("cmd.exe","/C");
42-
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds("run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener);
43+
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+"run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener);
4344
//Copy runner.bat for Windows platform in workspace.
4445
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace);
4546
}
@@ -48,11 +49,6 @@ default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher la
4849

4950
/**
5051
* Method to copy given file from source to target node specific workspace.
51-
* @param sourceFile
52-
* @param targetFile
53-
* @param targetWorkspace
54-
* @throws IOException
55-
* @throws InterruptedException
5652
*/
5753
default void copyFileInWorkspace(String sourceFile, String targetFile,
5854
FilePath targetWorkspace) throws IOException, InterruptedException {
@@ -64,5 +60,22 @@ default void copyFileInWorkspace(String sourceFile, String targetFile,
6460
targetFilePath.chmod(0755);
6561

6662
}
63+
64+
default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher) throws IOException, InterruptedException {
65+
Computer cmp = Computer.currentComputer();
66+
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
67+
if(launcher.isUnix()) {
68+
tmpDir = tmpDir+"/run_matlab_command.sh";
69+
}else {
70+
tmpDir = tmpDir+"\\"+"run_matlab_command.bat";
71+
}
72+
return new FilePath(launcher.getChannel(), tmpDir);
73+
}
74+
75+
default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedException {
76+
Computer cmp = Computer.currentComputer();
77+
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
78+
return tmpDir;
79+
}
6780

6881
}

src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,25 @@ public boolean isApplicable(
9595
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
9696
@Nonnull Launcher launcher, @Nonnull TaskListener listener)
9797
throws InterruptedException, IOException {
98-
99-
// Set the environment variable specific to the this build
100-
setEnv(build.getEnvironment(listener));
101-
102-
// Invoke MATLAB command and transfer output to standard
103-
// Output Console
104-
105-
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
106-
107-
if (buildResult != 0) {
108-
build.setResult(Result.FAILURE);
98+
99+
try {
100+
// Set the environment variable specific to the this build
101+
setEnv(build.getEnvironment(listener));
102+
103+
// Invoke MATLAB command and transfer output to standard
104+
// Output Console
105+
106+
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
107+
108+
if (buildResult != 0) {
109+
build.setResult(Result.FAILURE);
110+
}
111+
} finally {
112+
// Cleanup the runner File from tmp directory
113+
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher);
114+
if(matlabRunnerScript.exists()) {
115+
matlabRunnerScript.delete();
116+
}
109117
}
110118
}
111119

src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.kohsuke.stapler.DataBoundSetter;
2121
import org.kohsuke.stapler.QueryParameter;
2222
import org.kohsuke.stapler.StaplerRequest;
23-
import com.mathworks.ci.AddMatlabToPathBuildWrapper.AddMatlabToPathDescriptor;
2423
import hudson.EnvVars;
2524
import hudson.Extension;
2625
import hudson.FilePath;
@@ -33,8 +32,6 @@
3332
import hudson.tasks.BuildStepDescriptor;
3433
import hudson.tasks.Builder;
3534
import hudson.util.FormValidation;
36-
import hudson.util.FormValidation.Kind;
37-
import jenkins.model.Jenkins;
3835
import jenkins.tasks.SimpleBuildStep;
3936
import net.sf.json.JSONObject;
4037

@@ -155,14 +152,18 @@ public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends Abstra
155152
/*
156153
* Validation for Test artifact generator checkBoxes
157154
*/
155+
156+
//Get the MATLAB root entered in build wrapper descriptor
157+
158+
158159

159160
public FormValidation doCheckCoberturaChkBx(@QueryParameter boolean coberturaChkBx) {
160161
List<Function<String, FormValidation>> listOfCheckMethods =
161162
new ArrayList<Function<String, FormValidation>>();
162163
if (coberturaChkBx) {
163164
listOfCheckMethods.add(chkCoberturaSupport);
164165
}
165-
return getFirstErrorOrWarning(listOfCheckMethods);
166+
return FormValidationUtil.getFirstErrorOrWarning(listOfCheckMethods);
166167
}
167168

168169
Function<String, FormValidation> chkCoberturaSupport = (String matlabRoot) -> {
@@ -190,7 +191,7 @@ public FormValidation doCheckModelCoverageChkBx(@QueryParameter boolean modelCov
190191
if (modelCoverageChkBx) {
191192
listOfCheckMethods.add(chkModelCoverageSupport);
192193
}
193-
return getFirstErrorOrWarning(listOfCheckMethods);
194+
return FormValidationUtil.getFirstErrorOrWarning(listOfCheckMethods);
194195
}
195196

196197
Function<String, FormValidation> chkModelCoverageSupport = (String matlabRoot) -> {
@@ -218,7 +219,7 @@ public FormValidation doCheckStmResultsChkBx(@QueryParameter boolean stmResultsC
218219
if (stmResultsChkBx) {
219220
listOfCheckMethods.add(chkSTMResultsSupport);
220221
}
221-
return getFirstErrorOrWarning(listOfCheckMethods);
222+
return FormValidationUtil.getFirstErrorOrWarning(listOfCheckMethods);
222223
}
223224

224225
Function<String, FormValidation> chkSTMResultsSupport = (String matlabRoot) -> {
@@ -235,47 +236,33 @@ public FormValidation doCheckStmResultsChkBx(@QueryParameter boolean stmResultsC
235236
return FormValidation.warning(Message.getValue("Builder.invalid.matlab.root.warning"));
236237
}
237238
}
238-
239-
240239
return FormValidation.ok();
241240
};
242-
243-
public FormValidation getFirstErrorOrWarning(
244-
List<Function<String, FormValidation>> validations) {
245-
if (validations == null || validations.isEmpty())
246-
return FormValidation.ok();
247-
try {
248-
final String matlabRoot = Jenkins.getInstance()
249-
.getDescriptorByType(AddMatlabToPathDescriptor.class).getMatlabRootFolder();
250-
for (Function<String, FormValidation> val : validations) {
251-
FormValidation validationResult = val.apply(matlabRoot);
252-
if (validationResult.kind.compareTo(Kind.ERROR) == 0
253-
|| validationResult.kind.compareTo(Kind.WARNING) == 0) {
254-
return validationResult;
255-
}
256-
}
257-
}catch (Exception e) {
258-
return FormValidation.warning(Message.getValue("Builder.invalid.matlab.root.warning"));
259-
}
260-
261-
return FormValidation.ok();
262-
}
263-
}
241+
}
264242

265243
@Override
266244
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
267245
@Nonnull Launcher launcher, @Nonnull TaskListener listener)
268246
throws InterruptedException, IOException {
269-
//Set the environment variable specific to the this build
270-
setEnv(build.getEnvironment(listener));
271-
272-
// Invoke MATLAB command and transfer output to standard
273-
// Output Console
274247

275-
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
248+
try {
249+
// Set the environment variable specific to the this build
250+
setEnv(build.getEnvironment(listener));
276251

277-
if (buildResult != 0) {
278-
build.setResult(Result.FAILURE);
252+
// Invoke MATLAB command and transfer output to standard
253+
// Output Console
254+
255+
buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());
256+
257+
if (buildResult != 0) {
258+
build.setResult(Result.FAILURE);
259+
}
260+
} finally {
261+
// Cleanup the runner File from tmp directory
262+
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher);
263+
if (matlabRunnerScript.exists()) {
264+
matlabRunnerScript.delete();
265+
}
279266
}
280267
}
281268

@@ -298,22 +285,21 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
298285
}
299286

300287
public String constructCommandForTest(String inputArguments) {
301-
String runCommand;
302-
String matlabFunctionName = FilenameUtils.removeExtension(
288+
final String matlabFunctionName = FilenameUtils.removeExtension(
303289
Message.getValue(MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE));
304-
runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
290+
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
305291
return runCommand;
306292
}
307293

308294
// Concatenate the input arguments
309295
private String getInputArguments() {
310-
String pdfReport = MatlabBuilderConstants.PDF_REPORT + "," + this.getPdfReportChkBx();
311-
String tapResults = MatlabBuilderConstants.TAP_RESULTS + "," + this.getTapChkBx();
312-
String junitResults = MatlabBuilderConstants.JUNIT_RESULTS + "," + this.getJunitChkBx();
313-
String stmResults = MatlabBuilderConstants.STM_RESULTS + "," + this.getStmResultsChkBx();
314-
String coberturaCodeCoverage = MatlabBuilderConstants.COBERTURA_CODE_COVERAGE + "," + this.getCoberturaChkBx();
315-
String coberturaModelCoverage = MatlabBuilderConstants.COBERTURA_MODEL_COVERAGE + "," + this.getModelCoverageChkBx();
316-
String inputArgsToMatlabFcn = pdfReport + "," + tapResults + "," + junitResults + ","
296+
final String pdfReport = MatlabBuilderConstants.PDF_REPORT + "," + this.getPdfReportChkBx();
297+
final String tapResults = MatlabBuilderConstants.TAP_RESULTS + "," + this.getTapChkBx();
298+
final String junitResults = MatlabBuilderConstants.JUNIT_RESULTS + "," + this.getJunitChkBx();
299+
final String stmResults = MatlabBuilderConstants.STM_RESULTS + "," + this.getStmResultsChkBx();
300+
final String coberturaCodeCoverage = MatlabBuilderConstants.COBERTURA_CODE_COVERAGE + "," + this.getCoberturaChkBx();
301+
final String coberturaModelCoverage = MatlabBuilderConstants.COBERTURA_MODEL_COVERAGE + "," + this.getModelCoverageChkBx();
302+
final String inputArgsToMatlabFcn = pdfReport + "," + tapResults + "," + junitResults + ","
317303
+ stmResults + "," + coberturaCodeCoverage + "," + coberturaModelCoverage;
318304

319305
return inputArgsToMatlabFcn;

src/main/java/com/mathworks/ci/AddMatlabToPathBuildWrapper.java renamed to src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@
2626
import hudson.model.TaskListener;
2727
import hudson.tasks.BuildWrapperDescriptor;
2828
import hudson.util.FormValidation;
29-
import hudson.util.FormValidation.Kind;
3029
import jenkins.tasks.SimpleBuildWrapper;
3130

32-
public class AddMatlabToPathBuildWrapper extends SimpleBuildWrapper {
31+
public class UseMatlabVersionBuildWrapper extends SimpleBuildWrapper {
3332

3433
private String matlabRootFolder;
3534
private EnvVars env;
3635

3736
@DataBoundConstructor
38-
public AddMatlabToPathBuildWrapper() {}
37+
public UseMatlabVersionBuildWrapper() {}
3938

4039
public String getMatlabRootFolder() {
4140
return this.matlabRootFolder;
@@ -56,7 +55,7 @@ private void setEnv(EnvVars env) {
5655

5756
@Symbol("Matlab")
5857
@Extension
59-
public static final class AddMatlabToPathDescriptor extends BuildWrapperDescriptor {
58+
public static final class UseMatlabVersionDescriptor extends BuildWrapperDescriptor {
6059

6160
MatlabReleaseInfo rel;
6261
String matlabRootFolder;
@@ -94,21 +93,7 @@ public FormValidation doCheckMatlabRootFolder(@QueryParameter String matlabRootF
9493
listOfCheckMethods.add(chkMatlabEmpty);
9594
listOfCheckMethods.add(chkMatlabSupportsRunTests);
9695

97-
return getFirstErrorOrWarning(listOfCheckMethods, matlabRootFolder);
98-
}
99-
100-
public FormValidation getFirstErrorOrWarning(
101-
List<Function<String, FormValidation>> validations, String matlabRootFolder) {
102-
if (validations == null || validations.isEmpty())
103-
return FormValidation.ok();
104-
for (Function<String, FormValidation> val : validations) {
105-
FormValidation validationResult = val.apply(matlabRootFolder);
106-
if (validationResult.kind.compareTo(Kind.ERROR) == 0
107-
|| validationResult.kind.compareTo(Kind.WARNING) == 0) {
108-
return validationResult;
109-
}
110-
}
111-
return FormValidation.ok();
96+
return FormValidationUtil.getFirstErrorOrWarning(listOfCheckMethods);
11297
}
11398

11499
Function<String, FormValidation> chkMatlabEmpty = (String matlabRootFolder) -> {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div>
2-
Enter the full path to the MATLAB root folder, which is returned by the <i><b>matlabroot</b></i> function. <br><br>Example:<br><br><i><b><u>Windows:</u></b></i><br> C:\Program Files\MATLAB\R2019a <br>
2+
Enter the full path to the MATLAB root folder, which is returned by the <i><b>matlabroot</b></i> function. The specified MATLAB will be added to PATH variable and will be used across this build.
3+
If this field is empty then default matlabroot from PATH variable will be picked if added. <br><br><br>Example:<br><br><i><b><u>Windows:</u></b></i><br> C:\Program Files\MATLAB\R2019a <br>
34
<i><b><u>Linux:</u></b></i><br> /usr/local/MATLAB/R2019a <br>
45
<i><b><u>Mac:</u></b></i><br> /Applications/MATLAB_R2019a.app <br>
56
<p>

src/main/resources/config.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Builder.display.name = Run MATLAB Tests
55
Builder.script.builder.display.name = Run MATLAB Command
6-
Buildwrapper.display.name = Add MATLAB to PATH
6+
Buildwrapper.display.name = Use MATLAB version
77
Builder.matlab.runner.target.file.name = runMatlabTests.m
88
Builder.matlab.cobertura.support.warning = To generate a Cobertura code coverage report, use MATLAB R2017b or a newer release.
99
Builder.invalid.matlab.root.warning = Unable to find MATLAB from the specified location on this system(but perhaps it exists on some agents)

src/test/java/com/mathworks/ci/RunMatlabCommandBuilderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class RunMatlabCommandBuilderTest {
3131

3232
private static String matlabExecutorAbsolutePath;
3333
private FreeStyleProject project;
34-
private AddMatlabToPathBuildWrapper buildWrapper;
34+
private UseMatlabVersionBuildWrapper buildWrapper;
3535
private RunMatlabCommandBuilder scriptBuilder;
3636
private static URL url;
3737
private static String FileSeperator;
@@ -71,7 +71,7 @@ public void testSetup() throws IOException {
7171

7272
this.project = jenkins.createFreeStyleProject();
7373
this.scriptBuilder = new RunMatlabCommandBuilder();
74-
this.buildWrapper = new AddMatlabToPathBuildWrapper();
74+
this.buildWrapper = new UseMatlabVersionBuildWrapper();
7575
}
7676

7777
@After
@@ -230,14 +230,14 @@ public void verifyMATLABrunnerFileGeneratedForAutomaticOption() throws Exception
230230
project.getBuildWrappersList().add(this.buildWrapper);
231231
scriptBuilder.setMatlabCommand("pwd");
232232
project.getBuildersList().add(scriptBuilder);
233-
FreeStyleBuild build = project.scheduleBuild2(0).get();
233+
project.scheduleBuild2(0).get();
234234
String runnerFile;
235235
if (!System.getProperty("os.name").startsWith("Win")) {
236236
runnerFile = "run_matlab_command.sh";
237237
}else {
238238
runnerFile = "run_matlab_command.bat";
239239
}
240-
File matlabRunner = new File(build.getWorkspace() + File.separator + runnerFile);
240+
File matlabRunner = new File(System.getProperty("java.io.tmpdir") + File.separator + runnerFile);
241241
Assert.assertTrue(matlabRunner.exists());
242242
}
243243
}

0 commit comments

Comments
 (0)