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

Commit ba97f12

Browse files
committed
Added support for Jenkins environment variables.
1 parent fe8b723 commit ba97f12

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,13 @@ private int execMatlabCommand(Run<?, ?> build, FilePath workspace, Launcher laun
392392
workspace, getClass().getClassLoader());
393393
ProcStarter matlabLauncher;
394394
try {
395-
MatlabReleaseInfo rel = new MatlabReleaseInfo(getLocalMatlab());
395+
MatlabReleaseInfo rel = new MatlabReleaseInfo(env.expand(getLocalMatlab()));
396396
matlabLauncher = launcher.launch().pwd(workspace).envs(env);
397397
if (rel.verLessThan(BASE_MATLAB_VERSION_BATCH_SUPPORT)) {
398398
ListenerLogDecorator outStream = new ListenerLogDecorator(listener);
399-
matlabLauncher = matlabLauncher.cmds(constructDefaultMatlabCommand(isLinuxLauncher)).stderr(outStream);
399+
matlabLauncher = matlabLauncher.cmds(constructDefaultMatlabCommand(isLinuxLauncher,env)).stderr(outStream);
400400
} else {
401-
matlabLauncher = matlabLauncher.cmds(constructMatlabCommandWithBatch()).stdout(listener);
401+
matlabLauncher = matlabLauncher.cmds(constructMatlabCommandWithBatch(env)).stdout(listener);
402402
}
403403
} catch (MatlabVersionNotFoundException e) {
404404
listener.getLogger().println(e.getMessage());
@@ -407,7 +407,7 @@ private int execMatlabCommand(Run<?, ?> build, FilePath workspace, Launcher laun
407407
return matlabLauncher.join();
408408
}
409409

410-
public List<String> constructMatlabCommandWithBatch() {
410+
public List<String> constructMatlabCommandWithBatch(EnvVars env) {
411411
final String testRunMode = this.getTestRunTypeList().getDescriptor().getDisplayName();
412412
final String runCommand;
413413
final List<String> matlabDefaultArgs;
@@ -421,23 +421,23 @@ public List<String> constructMatlabCommandWithBatch() {
421421
+ getTestRunTypeList().getBooleanByName("taCoberturaChkBx") + "))";
422422
} else {
423423

424-
runCommand = this.getTestRunTypeList().getStringByName("customMatlabCommand");
424+
runCommand = env.expand(this.getTestRunTypeList().getStringByName("customMatlabCommand"));
425425
}
426426

427427
matlabDefaultArgs =
428-
Arrays.asList(this.localMatlab + File.separator + "bin" + File.separator + "matlab",
428+
Arrays.asList(env.expand(this.localMatlab) + File.separator + "bin" + File.separator + "matlab",
429429
"-batch", runCommand);
430430

431431
return matlabDefaultArgs;
432432
}
433433

434-
public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher) {
434+
public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher, EnvVars env) {
435435
final List<String> matlabDefaultArgs = new ArrayList<String>();
436-
Collections.addAll(matlabDefaultArgs, getPreRunnerSwitches());
436+
Collections.addAll(matlabDefaultArgs, getPreRunnerSwitches(env));
437437
if (!isLinuxLauncher) {
438438
matlabDefaultArgs.add("-noDisplayDesktop");
439439
}
440-
Collections.addAll(matlabDefaultArgs, getRunnerSwitch());
440+
Collections.addAll(matlabDefaultArgs, getRunnerSwitch(env));
441441
if (!isLinuxLauncher) {
442442
matlabDefaultArgs.add("-wait");
443443
}
@@ -446,9 +446,9 @@ public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher) {
446446
}
447447

448448

449-
private String[] getPreRunnerSwitches() {
449+
private String[] getPreRunnerSwitches(EnvVars env) {
450450
String[] preRunnerSwitches =
451-
{this.localMatlab + File.separator + "bin" + File.separator + "matlab", "-nosplash",
451+
{env.expand(this.localMatlab) + File.separator + "bin" + File.separator + "matlab", "-nosplash",
452452
"-nodesktop", "-noAppIcon"};
453453
return preRunnerSwitches;
454454
}
@@ -458,7 +458,7 @@ private String[] getPostRunnerSwitches() {
458458
return postRunnerSwitch;
459459
}
460460

461-
private String[] getRunnerSwitch() {
461+
private String[] getRunnerSwitch(EnvVars env) {
462462
final String runCommand;
463463
final String testRunMode = this.getTestRunTypeList().getDescriptor().getDisplayName();
464464
if (!testRunMode.equalsIgnoreCase(
@@ -471,7 +471,7 @@ private String[] getRunnerSwitch() {
471471
+ getTestRunTypeList().getBooleanByName("taCoberturaChkBx")
472472
+ ")),catch e,disp(getReport(e,'extended')),exit(1),end";
473473
} else {
474-
runCommand = "try,eval(\"" + this.getTestRunTypeList().getStringByName("customMatlabCommand").replaceAll("\"","\"\"")
474+
runCommand = "try,eval(\"" + env.expand(this.getTestRunTypeList().getStringByName("customMatlabCommand").replaceAll("\"","\"\""))
475475
+ "\"),catch e,disp(getReport(e,'extended')),exit(1),end,exit";
476476
}
477477

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44

55
import static org.junit.Assert.assertFalse;
6+
import hudson.EnvVars;
67
import hudson.model.FreeStyleBuild;
78
import hudson.model.Result;
9+
import hudson.slaves.EnvironmentVariablesNodeProperty;
810
import hudson.model.FreeStyleProject;
911
import hudson.tasks.Builder;
1012
import java.io.File;
@@ -129,7 +131,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsBatch() throws Exception {
129131
FreeStyleBuild build = project.scheduleBuild2(0).get();
130132
jenkins.assertLogContains("-batch", build);
131133
jenkins.assertLogContains("exit(runMatlabTests", build);
132-
Assert.assertEquals(3, matlabBuilder.constructMatlabCommandWithBatch().size());
134+
Assert.assertEquals(3, matlabBuilder.constructMatlabCommandWithBatch(new EnvVars()).size());
133135
}
134136

135137
/*
@@ -145,7 +147,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsRWindows() throws Exception
145147
FreeStyleBuild build = project.scheduleBuild2(0).get();
146148
jenkins.assertLogContains("-r", build);
147149
jenkins.assertLogContains("try,exit(runMatlabTests", build);
148-
Assert.assertEquals(9, matlabBuilder.constructDefaultMatlabCommand(false).size());
150+
Assert.assertEquals(9, matlabBuilder.constructDefaultMatlabCommand(false,new EnvVars()).size());
149151
}
150152

151153
/*
@@ -161,7 +163,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsRLinux() throws Exception {
161163
FreeStyleBuild build = project.scheduleBuild2(0).get();
162164
jenkins.assertLogContains("-r", build);
163165
jenkins.assertLogContains("try,exit(runMatlabTests", build);
164-
Assert.assertEquals(7, matlabBuilder.constructDefaultMatlabCommand(true).size());
166+
Assert.assertEquals(7, matlabBuilder.constructDefaultMatlabCommand(true,new EnvVars()).size());
165167
}
166168

167169
/*
@@ -405,6 +407,48 @@ public void verifyCoberturaError() throws Exception {
405407
Assert.assertTrue(filteredPageText.contains(TestMessage.getValue("Builder.invalid.matlab.root.error")));
406408
}
407409

410+
/*
411+
* Test to verify if MatlabRoot field supports Jenkins environment variables.
412+
*
413+
*/
414+
415+
@Test
416+
public void verifyEnvVarSupportForMatlabRoot() throws Exception {
417+
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
418+
EnvVars var = prop.getEnvVars();
419+
var.put("MATLAB", "R2017a");
420+
jenkins.jenkins.getGlobalNodeProperties().add(prop);
421+
String mroot = getMatlabroot("R2017a");
422+
this.matlabBuilder.setLocalMatlab(mroot.replace("R2017a", "$MATLAB"));
423+
this.matlabBuilder.setTestRunTypeList(new RunTestsAutomaticallyOption());
424+
project.getBuildersList().add(this.matlabBuilder);
425+
FreeStyleBuild build = project.scheduleBuild2(0).get();
426+
jenkins.assertLogContains(mroot, build);
427+
428+
}
429+
430+
/*
431+
* Test to verify if Custom command field supports Jenkins environment variables.
432+
*
433+
*/
434+
435+
@Test
436+
public void verifyEnvVarSupportForCustomCommandField() throws Exception {
437+
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
438+
EnvVars var = prop.getEnvVars();
439+
var.put("TAG", "R2017a");
440+
jenkins.jenkins.getGlobalNodeProperties().add(prop);
441+
String mroot = getMatlabroot("R2017a");
442+
this.matlabBuilder.setLocalMatlab(mroot);
443+
RunTestsWithCustomCommandOption runOption = new RunTestsWithCustomCommandOption();
444+
this.matlabBuilder.setTestRunTypeList(runOption);
445+
runOption.setCustomMatlabCommand("disp($TAG)");
446+
project.getBuildersList().add(this.matlabBuilder);
447+
FreeStyleBuild build = project.scheduleBuild2(0).get();
448+
jenkins.assertLogContains("R2017a", build);
449+
450+
}
451+
408452
/*
409453
* Private helper methods for tests
410454
*/

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13+
import hudson.EnvVars;
1314
import hudson.Extension;
1415
import hudson.Launcher;
1516
import hudson.model.AbstractProject;
@@ -29,12 +30,12 @@ public MatlabBuilderTester(String localMatlab, String matlabExecutorPath,
2930
}
3031

3132
@Override
32-
public List<String> constructMatlabCommandWithBatch() {
33+
public List<String> constructMatlabCommandWithBatch(EnvVars env) {
3334
return testMatlabCommand();
3435
}
3536

3637
@Override
37-
public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher) {
38+
public List<String> constructDefaultMatlabCommand(boolean isLinuxLauncher,EnvVars env) {
3839
return testMatlabCommand();
3940
}
4041

0 commit comments

Comments
 (0)