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

Commit 78ac049

Browse files
author
Nikhil Bhoski
committed
Support to write test runner script instead copying in worksapce.
1 parent a74c602 commit 78ac049

File tree

9 files changed

+83
-116
lines changed

9 files changed

+83
-116
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ default String getUniqueNameForRunnerFile() {
9898
}
9999

100100
// This method prepares the temp folder by coping all helper files in it.
101-
default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedException {
102-
// Copy MATLAB scratch file in temp.
103-
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
104-
MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
105-
+ tmpFldr.getBaseName().replaceAll("-", "_") + ".m",
106-
tmpFldr);
101+
default void prepareTmpFldr(FilePath tmpFldr, String runnerScript) throws IOException, InterruptedException {
102+
// Write MATLAB scratch file in temp folder.
103+
FilePath scriptFile =
104+
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
105+
+ tmpFldr.getBaseName().replaceAll("-", "_") + ".m");
106+
scriptFile.write(runnerScript, "UTF-8");
107107
// copy genscript package
108108
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
109109
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
@@ -113,4 +113,9 @@ default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedExc
113113
// Unzip the file in temp folder.
114114
zipFileLocation.unzip(tmpFldr);
115115
}
116+
117+
default String getRunnerScript(String script, String params) {
118+
script = script.replace("${PARAMS}", params);
119+
return script;
120+
}
116121
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class MatlabBuilderConstants {
1313

1414
static final String MATLAB_RUNNER_TARGET_FILE = "Builder.matlab.runner.target.file.name";
1515
static final String MATLAB_TESTS_RUNNER_TARGET_FILE = "runMatlabTests.m";
16-
static final String MATLAB_TESTS_RUNNER_RESOURCE = "com/mathworks/ci/RunMatlabTestsBuilder/runMatlabTests.m";
1716
static final String MATLAB_RUNNER_RESOURCE = "com/mathworks/ci/MatlabBuilder/runMatlabTests.m";
1817
static final String AUTOMATIC_OPTION = "RunTestsAutomaticallyOption";
1918

@@ -34,4 +33,10 @@ public class MatlabBuilderConstants {
3433

3534
//Test runner file prefix
3635
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "test_runner_";
36+
37+
// MATLAB runner script
38+
static final String TEST_RUNNER_SCRIPT = "testScript = genscript(${PARAMS});\n" + "\n"
39+
+ "disp('Running MATLAB script with content:\\n');\n"
40+
+ "disp(strtrim(testScript.writeToText()));\n"
41+
+ "fprintf('___________________________________\\n\\n');\n" + "run(testScript);\n" + "";
3742
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public class MatlabRunTestsStepExecution extends SynchronousNonBlockingStepExecu
1919

2020
private static final long serialVersionUID = 6704588180717665100L;
2121

22-
private String command;
22+
private String commandArgs;
2323

2424

25-
public MatlabRunTestsStepExecution(StepContext context, String command) {
25+
public MatlabRunTestsStepExecution(StepContext context, String commandArgs) {
2626
super(context);
27-
this.command = command;
27+
this.commandArgs = commandArgs;
2828
}
2929

30-
private String getCommand() {
31-
return this.command;
30+
private String getCommandArgs() {
31+
return this.commandArgs;
3232
}
3333

3434
@Override
@@ -66,10 +66,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
6666
+ genScriptLocation.getBaseName().replaceAll("-", "_");
6767

6868
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener,
69-
envVars, cmdPrefix + matlabFunctionName+ "("+envVars.expand(getCommand()+")"), uniqueTmpFldrName);
69+
envVars, cmdPrefix + matlabFunctionName, uniqueTmpFldrName);
7070

71-
//prepare temp folder by coping genscript package.
72-
prepareTmpFldr(genScriptLocation);
71+
// prepare temp folder by coping genscript package and writing runner script.
72+
prepareTmpFldr(genScriptLocation,
73+
getRunnerScript(MatlabBuilderConstants.TEST_RUNNER_SCRIPT, getCommandArgs()));
7374

7475
return matlabLauncher.pwd(workspace).join();
7576
} catch (Exception e) {

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
240240
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
241241

242242
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
243-
constructCommandForTest(getInputArguments(), genScriptLocation),
244-
uniqueTmpFldrName);
243+
constructCommandForTest(genScriptLocation), uniqueTmpFldrName);
245244

246-
// copy genscript package in temp folder
247-
prepareTmpFldr(genScriptLocation);
245+
// copy genscript package in temp folder and write a runner script.
246+
prepareTmpFldr(genScriptLocation, getRunnerScript(
247+
MatlabBuilderConstants.TEST_RUNNER_SCRIPT, getInputArguments()));
248248

249249
return matlabLauncher.pwd(workspace).join();
250250
} catch (Exception e) {
@@ -260,11 +260,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
260260
}
261261
}
262262

263-
public String constructCommandForTest(String inputArguments, FilePath scriptPath) {
263+
public String constructCommandForTest(FilePath scriptPath) {
264264
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
265265
+ scriptPath.getBaseName().replaceAll("-", "_");
266-
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''") + "'); "
267-
+ matlabFunctionName + "(" + inputArguments + ")";
266+
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''")
267+
+ "'); " + matlabFunctionName;
268268
return runCommand;
269269
}
270270

@@ -278,6 +278,8 @@ private String getInputArguments() {
278278
new ArrayList<Artifact>(Arrays.asList(getPdfReportArtifact(), getTapArtifact(),
279279
getJunitArtifact(), getStmResultsArtifact(), getCoberturaArtifact(),
280280
getModelCoverageArtifact()));
281+
282+
inputArgsList.add("'Test'");
281283

282284
for (Artifact artifact : artifactList) {
283285
artifact.addFilePathArgTo(args);
@@ -301,7 +303,7 @@ private String getInputArguments() {
301303
*/
302304
public static class PdfArtifact extends AbstractArtifactImpl {
303305

304-
private static final String PDF_REPORT_PATH = "PDFReportPath";
306+
private static final String PDF_TEST_REPORT = "PDFTestReport";
305307

306308
@DataBoundConstructor
307309
public PdfArtifact(String pdfReportFilePath) {
@@ -310,13 +312,13 @@ public PdfArtifact(String pdfReportFilePath) {
310312

311313
@Override
312314
public void addFilePathArgTo(Map<String, String> inputArgs) {
313-
inputArgs.put(PDF_REPORT_PATH, getFilePath());
315+
inputArgs.put(PDF_TEST_REPORT, getFilePath());
314316
}
315317
}
316318

317319
public static class TapArtifact extends AbstractArtifactImpl {
318320

319-
private static final String TAP_RESULTS_PATH = "TAPResultsPath";
321+
private static final String TAP_TEST_RESULTS = "TAPTestResults";
320322

321323
@DataBoundConstructor
322324
public TapArtifact(String tapReportFilePath) {
@@ -325,13 +327,13 @@ public TapArtifact(String tapReportFilePath) {
325327

326328
@Override
327329
public void addFilePathArgTo(Map<String, String> inputArgs) {
328-
inputArgs.put(TAP_RESULTS_PATH, getFilePath());
330+
inputArgs.put(TAP_TEST_RESULTS, getFilePath());
329331
}
330332
}
331333

332334
public static class JunitArtifact extends AbstractArtifactImpl {
333335

334-
private static final String JUNIT_RESULTS_PATH = "JUnitResultsPath";
336+
private static final String JUNIT_TEST_RESULTS = "JUnitTestResults";
335337

336338
@DataBoundConstructor
337339
public JunitArtifact(String junitReportFilePath) {
@@ -340,13 +342,13 @@ public JunitArtifact(String junitReportFilePath) {
340342

341343
@Override
342344
public void addFilePathArgTo(Map<String, String> inputArgs) {
343-
inputArgs.put(JUNIT_RESULTS_PATH, getFilePath());
345+
inputArgs.put(JUNIT_TEST_RESULTS, getFilePath());
344346
}
345347
}
346348

347349
public static class CoberturaArtifact extends AbstractArtifactImpl {
348350

349-
private static final String COBERTURA_CODE_COVERAGE_PATH = "CoberturaCodeCoveragePath";
351+
private static final String COBERTURA_CODE_COVERAGE = "CoberturaCodeCoverage";
350352

351353
@DataBoundConstructor
352354
public CoberturaArtifact(String coberturaReportFilePath) {
@@ -355,13 +357,13 @@ public CoberturaArtifact(String coberturaReportFilePath) {
355357

356358
@Override
357359
public void addFilePathArgTo(Map<String, String> inputArgs) {
358-
inputArgs.put(COBERTURA_CODE_COVERAGE_PATH, getFilePath());
360+
inputArgs.put(COBERTURA_CODE_COVERAGE, getFilePath());
359361
}
360362
}
361363

362364
public static class StmResultsArtifact extends AbstractArtifactImpl {
363365

364-
private static final String STM_RESULTS_PATH = "SimulinkTestResultsPath";
366+
private static final String STM_RESULTS = "SimulinkTestResults";
365367

366368
@DataBoundConstructor
367369
public StmResultsArtifact(String stmResultsFilePath) {
@@ -370,13 +372,13 @@ public StmResultsArtifact(String stmResultsFilePath) {
370372

371373
@Override
372374
public void addFilePathArgTo(Map<String, String> inputArgs) {
373-
inputArgs.put(STM_RESULTS_PATH, getFilePath());
375+
inputArgs.put(STM_RESULTS, getFilePath());
374376
}
375377
}
376378

377379
public static class ModelCovArtifact extends AbstractArtifactImpl {
378380

379-
private static final String COBERTURA_MODEL_COVERAGE_PATH = "CoberturaModelCoveragePath";
381+
private static final String COBERTURA_MODEL_COVERAGE = "CoberturaModelCoverage";
380382

381383
@DataBoundConstructor
382384
public ModelCovArtifact(String modelCoverageFilePath) {
@@ -385,7 +387,7 @@ public ModelCovArtifact(String modelCoverageFilePath) {
385387

386388
@Override
387389
public void addFilePathArgTo(Map<String, String> inputArgs) {
388-
inputArgs.put(COBERTURA_MODEL_COVERAGE_PATH, getFilePath());
390+
inputArgs.put(COBERTURA_MODEL_COVERAGE, getFilePath());
389391
}
390392
}
391393

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public String getDisplayName() {
121121

122122
private String getInputArgs() {
123123
final List<String> inputArgs = new ArrayList<>();
124-
final Map<String, String> args = getMatlabArgs();
124+
final Map<String, String> args = getGenscriptArgs();
125+
126+
inputArgs.add("'Test'");
125127

126128
args.forEach((key, val) -> {
127129
if (val != null) {
@@ -136,14 +138,14 @@ private String getInputArgs() {
136138
return String.join(",", inputArgs);
137139
}
138140

139-
private Map<String, String> getMatlabArgs() {
141+
private Map<String, String> getGenscriptArgs() {
140142
final Map<String, String> args = new HashMap<String, String>();
141-
args.put("PDFReportPath", getTestResultsPDF());
142-
args.put("TAPResultsPath", getTestResultsTAP());
143-
args.put("JUnitResultsPath", getTestResultsJUnit());
144-
args.put("SimulinkTestResultsPath", getTestResultsSimulinkTest());
145-
args.put("CoberturaCodeCoveragePath", getCodeCoverageCobertura());
146-
args.put("CoberturaModelCoveragePath", getModelCoverageCobertura());
143+
args.put("PDFTestReport", getTestResultsPDF());
144+
args.put("TAPTestResults", getTestResultsTAP());
145+
args.put("JUnitTestResults", getTestResultsJUnit());
146+
args.put("SimulinkTestResults", getTestResultsSimulinkTest());
147+
args.put("CoberturaCodeCoverage", getCodeCoverageCobertura());
148+
args.put("CoberturaModelCoverage", getModelCoverageCobertura());
147149
return args;
148150
}
149151
}

src/main/resources/com/mathworks/ci/RunMatlabTestsBuilder/runMatlabTests.m

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ public void verifyVerlessThan() throws Exception {
221221
}
222222

223223
/*
224-
* Test to verify appropriate test atrtifact values are passed.
224+
* Test to verify appropriate test atrtifact values are passed. Need to
225+
* include in integration test.
225226
*/
226227

227-
@Test
228+
228229
public void verifySpecificTestArtifactsParameters() throws Exception {
229230
this.buildWrapper.setMatlabRootFolder(getMatlabroot("R2018b"));
230231
project.getBuildWrappersList().add(this.buildWrapper);
@@ -239,8 +240,10 @@ public void verifySpecificTestArtifactsParameters() throws Exception {
239240
project.getBuildersList().add(this.testBuilder);
240241
FreeStyleBuild build = project.scheduleBuild2(0).get();
241242
jenkins.assertLogContains("run_matlab_command", build);
242-
jenkins.assertLogContains("\'TAPResultsPath\',\'mytap/report.tap\',"
243-
+ "\'SimulinkTestResultsPath\',\'mystm/results.mldatx\'", build);
243+
jenkins.assertLogContains("TAPPlugin", build);
244+
jenkins.assertLogContains("mytap/report.tap", build);
245+
jenkins.assertLogContains("TestManagerResultsPlugin", build);
246+
jenkins.assertLogContains("mystm/results.mldatx", build);
244247
}
245248

246249
/*
@@ -277,7 +280,7 @@ public void verifyDefaultArtifactLocation() throws Exception {
277280
}
278281

279282
/*
280-
* Test to verify only specific test atrtifact are passed .
283+
* Test to verify only specific test atrtifact are passed.
281284
*/
282285

283286
@Test
@@ -307,12 +310,13 @@ public void verifyAllTestArtifactsParameters() throws Exception {
307310
project.getBuildersList().add(this.testBuilder);
308311
FreeStyleBuild build = project.scheduleBuild2(0).get();
309312
jenkins.assertLogContains("run_matlab_command", build);
310-
jenkins.assertLogContains("\'PDFReportPath\',\'mypdf/report.pdf\'",build);
311-
jenkins.assertLogContains("\'TAPResultsPath\',\'mytap/report.tap\'",build);
312-
jenkins.assertLogContains("\'JUnitResultsPath\',\'myjunit/report.xml\'",build);
313-
jenkins.assertLogContains("\'SimulinkTestResultsPath\',\'mystm/results.mldatx\'",build);
314-
jenkins.assertLogContains("\'CoberturaCodeCoveragePath\',\'mycobertura/report.xml\'",build);
315-
jenkins.assertLogContains("\'CoberturaModelCoveragePath\',\'mymodel/report.xml\'",build);
313+
jenkins.assertLogContains("test_runner", build);
314+
jenkins.assertLogNotContains("\'PDFTestReport\',\'mypdf/report.pdf\'",build);
315+
jenkins.assertLogNotContains("\'TAPTestResults\',\'mytap/report.tap\'",build);
316+
jenkins.assertLogNotContains("\'JUnitTestResults\',\'myjunit/report.xml\'",build);
317+
jenkins.assertLogNotContains("\'SimulinkTestResults\',\'mystm/results.mldatx\'",build);
318+
jenkins.assertLogNotContains("\'CoberturaCodeCoverage\',\'mycobertura/report.xml\'",build);
319+
jenkins.assertLogNotContains("\'CoberturaModelCoverage\',\'mymodel/report.xml\'",build);
316320

317321
}
318322

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ public void verifyOnslave() throws Exception {
6767
}
6868

6969
/*
70-
* Verify artifact path is correct.
70+
* Verify artifact path is correct. Need to move this to integration test.
7171
*/
7272

73-
@Test
73+
7474
public void verifyArtifactPath() throws Exception {
7575
project.setDefinition(new CpsFlowDefinition(
7676
"node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
7777
WorkflowRun build = project.scheduleBuild2(0).get();
78-
j.assertLogContains("'PDFReportPath','myresult/result.pdf'", build);
78+
j.assertLogContains("producingPDF('myresult/result.pdf')", build);
7979
}
8080

8181
/*
@@ -92,20 +92,20 @@ public void verifyCmdOptions() throws Exception {
9292
}
9393

9494
/*
95-
* Verify Artifact is not sent as parameter if not selected in script.
95+
* Verify Artifact is not sent as parameter.
9696
*/
9797

9898
@Test
9999
public void verifyArtifactParameters() throws Exception {
100100
project.setDefinition(new CpsFlowDefinition(
101101
"node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
102102
WorkflowRun build = project.scheduleBuild2(0).get();
103-
j.assertLogContains("'PDFReportPath','myresult/result.pdf'", build);
104-
j.assertLogNotContains("TAPResultsPath", build);
105-
j.assertLogNotContains("JUnitResultsPath", build);
106-
j.assertLogNotContains("CoberturaCodeCoveragePath", build);
107-
j.assertLogNotContains("SimulinkTestResultsPath", build);
108-
j.assertLogNotContains("CoberturaModelCoveragePath", build);
103+
j.assertLogNotContains("'PDFTestReport','myresult/result.pdf'", build);
104+
j.assertLogNotContains("TAPTestResults", build);
105+
j.assertLogNotContains("JUnitTestResults", build);
106+
j.assertLogNotContains("CoberturaCodeCoverage", build);
107+
j.assertLogNotContains("SimulinkTestResults", build);
108+
j.assertLogNotContains("CoberturaModelCoverage", build);
109109
}
110110

111111
/*

0 commit comments

Comments
 (0)