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

Commit 5cd5df0

Browse files
authored
Merge pull request #143 from mathworks/genscript_write_issue134
Support to write test runner script instead copying in worksapce.
2 parents a74c602 + 229ab24 commit 5cd5df0

File tree

9 files changed

+89
-124
lines changed

9 files changed

+89
-124
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ 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, getValidMatlabFileName(tmpFldr.getBaseName()) + ".m");
105+
scriptFile.write(runnerScript, "UTF-8");
107106
// copy genscript package
108107
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
109108
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
@@ -113,4 +112,14 @@ default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedExc
113112
// Unzip the file in temp folder.
114113
zipFileLocation.unzip(tmpFldr);
115114
}
115+
116+
default String getRunnerScript(String script, String params) {
117+
script = script.replace("${PARAMS}", params);
118+
return script;
119+
}
120+
121+
default String getValidMatlabFileName(String actualName) {
122+
return MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
123+
+ actualName.replaceAll("-", "_");
124+
}
116125
}

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"
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: 10 additions & 10 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
@@ -62,14 +62,14 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
6262
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
6363
final String cmdPrefix =
6464
"addpath('" + genScriptLocation.getRemote().replaceAll("'", "''") + "'); ";
65-
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
66-
+ genScriptLocation.getBaseName().replaceAll("-", "_");
65+
final String matlabScriptName = getValidMatlabFileName(genScriptLocation.getBaseName());
6766

6867
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener,
69-
envVars, cmdPrefix + matlabFunctionName+ "("+envVars.expand(getCommand()+")"), uniqueTmpFldrName);
68+
envVars, cmdPrefix + matlabScriptName, uniqueTmpFldrName);
7069

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

7474
return matlabLauncher.pwd(workspace).join();
7575
} catch (Exception e) {

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

Lines changed: 22 additions & 21 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, envVars.expand(getInputArguments())));
248248

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

263-
public String constructCommandForTest(String inputArguments, FilePath scriptPath) {
264-
final String matlabFunctionName = MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
265-
+ scriptPath.getBaseName().replaceAll("-", "_");
266-
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''") + "'); "
267-
+ matlabFunctionName + "(" + inputArguments + ")";
263+
public String constructCommandForTest(FilePath scriptPath) {
264+
final String matlabScriptName = getValidMatlabFileName(scriptPath.getBaseName());
265+
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''")
266+
+ "'); " + matlabScriptName;
268267
return runCommand;
269268
}
270269

@@ -278,6 +277,8 @@ private String getInputArguments() {
278277
new ArrayList<Artifact>(Arrays.asList(getPdfReportArtifact(), getTapArtifact(),
279278
getJunitArtifact(), getStmResultsArtifact(), getCoberturaArtifact(),
280279
getModelCoverageArtifact()));
280+
281+
inputArgsList.add("'Test'");
281282

282283
for (Artifact artifact : artifactList) {
283284
artifact.addFilePathArgTo(args);
@@ -301,7 +302,7 @@ private String getInputArguments() {
301302
*/
302303
public static class PdfArtifact extends AbstractArtifactImpl {
303304

304-
private static final String PDF_REPORT_PATH = "PDFReportPath";
305+
private static final String PDF_TEST_REPORT = "PDFTestReport";
305306

306307
@DataBoundConstructor
307308
public PdfArtifact(String pdfReportFilePath) {
@@ -310,13 +311,13 @@ public PdfArtifact(String pdfReportFilePath) {
310311

311312
@Override
312313
public void addFilePathArgTo(Map<String, String> inputArgs) {
313-
inputArgs.put(PDF_REPORT_PATH, getFilePath());
314+
inputArgs.put(PDF_TEST_REPORT, getFilePath());
314315
}
315316
}
316317

317318
public static class TapArtifact extends AbstractArtifactImpl {
318319

319-
private static final String TAP_RESULTS_PATH = "TAPResultsPath";
320+
private static final String TAP_TEST_RESULTS = "TAPTestResults";
320321

321322
@DataBoundConstructor
322323
public TapArtifact(String tapReportFilePath) {
@@ -325,13 +326,13 @@ public TapArtifact(String tapReportFilePath) {
325326

326327
@Override
327328
public void addFilePathArgTo(Map<String, String> inputArgs) {
328-
inputArgs.put(TAP_RESULTS_PATH, getFilePath());
329+
inputArgs.put(TAP_TEST_RESULTS, getFilePath());
329330
}
330331
}
331332

332333
public static class JunitArtifact extends AbstractArtifactImpl {
333334

334-
private static final String JUNIT_RESULTS_PATH = "JUnitResultsPath";
335+
private static final String JUNIT_TEST_RESULTS = "JUnitTestResults";
335336

336337
@DataBoundConstructor
337338
public JunitArtifact(String junitReportFilePath) {
@@ -340,13 +341,13 @@ public JunitArtifact(String junitReportFilePath) {
340341

341342
@Override
342343
public void addFilePathArgTo(Map<String, String> inputArgs) {
343-
inputArgs.put(JUNIT_RESULTS_PATH, getFilePath());
344+
inputArgs.put(JUNIT_TEST_RESULTS, getFilePath());
344345
}
345346
}
346347

347348
public static class CoberturaArtifact extends AbstractArtifactImpl {
348349

349-
private static final String COBERTURA_CODE_COVERAGE_PATH = "CoberturaCodeCoveragePath";
350+
private static final String COBERTURA_CODE_COVERAGE = "CoberturaCodeCoverage";
350351

351352
@DataBoundConstructor
352353
public CoberturaArtifact(String coberturaReportFilePath) {
@@ -355,13 +356,13 @@ public CoberturaArtifact(String coberturaReportFilePath) {
355356

356357
@Override
357358
public void addFilePathArgTo(Map<String, String> inputArgs) {
358-
inputArgs.put(COBERTURA_CODE_COVERAGE_PATH, getFilePath());
359+
inputArgs.put(COBERTURA_CODE_COVERAGE, getFilePath());
359360
}
360361
}
361362

362363
public static class StmResultsArtifact extends AbstractArtifactImpl {
363364

364-
private static final String STM_RESULTS_PATH = "SimulinkTestResultsPath";
365+
private static final String STM_RESULTS = "SimulinkTestResults";
365366

366367
@DataBoundConstructor
367368
public StmResultsArtifact(String stmResultsFilePath) {
@@ -370,13 +371,13 @@ public StmResultsArtifact(String stmResultsFilePath) {
370371

371372
@Override
372373
public void addFilePathArgTo(Map<String, String> inputArgs) {
373-
inputArgs.put(STM_RESULTS_PATH, getFilePath());
374+
inputArgs.put(STM_RESULTS, getFilePath());
374375
}
375376
}
376377

377378
public static class ModelCovArtifact extends AbstractArtifactImpl {
378379

379-
private static final String COBERTURA_MODEL_COVERAGE_PATH = "CoberturaModelCoveragePath";
380+
private static final String COBERTURA_MODEL_COVERAGE = "CoberturaModelCoverage";
380381

381382
@DataBoundConstructor
382383
public ModelCovArtifact(String modelCoverageFilePath) {
@@ -385,7 +386,7 @@ public ModelCovArtifact(String modelCoverageFilePath) {
385386

386387
@Override
387388
public void addFilePathArgTo(Map<String, String> inputArgs) {
388-
inputArgs.put(COBERTURA_MODEL_COVERAGE_PATH, getFilePath());
389+
inputArgs.put(COBERTURA_MODEL_COVERAGE, getFilePath());
389390
}
390391
}
391392

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,27 @@ 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) {
128130
inputArgs.add("'" + key + "'" + "," + "'" + val.replaceAll("'", "''") + "'");
129131
}
130132
});
131133

132-
if (inputArgs.isEmpty()) {
133-
return "";
134-
}
135-
136134
return String.join(",", inputArgs);
137135
}
138136

139-
private Map<String, String> getMatlabArgs() {
137+
private Map<String, String> getGenscriptArgs() {
140138
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());
139+
args.put("PDFTestReport", getTestResultsPDF());
140+
args.put("TAPTestResults", getTestResultsTAP());
141+
args.put("JUnitTestResults", getTestResultsJUnit());
142+
args.put("SimulinkTestResults", getTestResultsSimulinkTest());
143+
args.put("CoberturaCodeCoverage", getCodeCoverageCobertura());
144+
args.put("CoberturaModelCoverage", getModelCoverageCobertura());
147145
return args;
148146
}
149147
}

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

0 commit comments

Comments
 (0)