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

Commit e0ddf94

Browse files
author
Nikhil Bhoski
committed
Scriptgen support for test run
1 parent d821dd2 commit e0ddf94

File tree

9 files changed

+75
-180
lines changed

9 files changed

+75
-180
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@
126126
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
127127
</configuration>
128128
</execution>
129+
<execution>
130+
<id>get-matlab-gen-script</id>
131+
<phase>validate</phase>
132+
<goals>
133+
<goal>wget</goal>
134+
</goals>
135+
<configuration>
136+
<url>https://ssd.mathworks.com/supportfiles/ci/matlab-script-generator/v0/matlab-script-generator.zip</url>
137+
<unpack>false</unpack>
138+
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
139+
</configuration>
140+
</execution>
129141
</executions>
130142
</plugin>
131143
</plugins>
@@ -186,6 +198,7 @@
186198
<include>**/*.bat</include>
187199
<include>**/*.sh</include>
188200
<include>**/*.txt</include>
201+
<include>**/*.zip</include>
189202
</includes>
190203
<followSymlinks>false</followSymlinks>
191204
</fileset>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,16 @@ default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOExcepti
9696
default String getUniqueNameForRunnerFile() {
9797
return UUID.randomUUID().toString();
9898
}
99+
100+
// This method prepares the temp folder by coping all helper files in it.
101+
default void prepareTmpFldr(FilePath tmpFldr) throws IOException, InterruptedException {
102+
// copy genscript package
103+
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
104+
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
105+
FilePath zipFileLocation =
106+
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR);
107+
108+
// Unzip the file in temp folder.
109+
zipFileLocation.unzip(tmpFldr);
110+
}
99111
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ public class MatlabBuilderConstants {
2828
// Matlab Runner files
2929
static final String BAT_RUNNER_SCRIPT = "run_matlab_command.bat";
3030
static final String SHELL_RUNNER_SCRIPT = "run_matlab_command.sh";
31+
32+
//Matlab Script generator package
33+
static final String MATLAB_SCRIPT_GENERATOR = "matlab-script-generator.zip";
3134
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
5858
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
5959
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
6060
try {
61-
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
62-
envVars.expand(getCommand()), uniqueTmpFldrName);
61+
FilePath genScriptLocation =
62+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
63+
final String cmdPrefix = "addpath(genpath('" + genScriptLocation.getRemote() + "')); ";
64+
65+
ProcStarter matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener,
66+
envVars, cmdPrefix + envVars.expand(getCommand()), uniqueTmpFldrName);
6367

64-
68+
//prepare temp folder by coping genscript package.
69+
prepareTmpFldr(genScriptLocation);
70+
6571
return matlabLauncher.pwd(workspace).join();
6672
} catch (Exception e) {
6773
listener.getLogger().println(e.getMessage());

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,19 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
238238
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
239239
ProcStarter matlabLauncher;
240240
try {
241+
FilePath genScriptLocation =
242+
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
243+
241244
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
242-
constructCommandForTest(getInputArguments()), uniqueTmpFldrName);
245+
constructCommandForTest(getInputArguments(), genScriptLocation.getRemote()),
246+
uniqueTmpFldrName);
243247

244-
// Copy MATLAB scratch file into the workspace.
245-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
248+
// Copy MATLAB scratch file into the workspace.
246249
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
247-
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
250+
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, workspace);
251+
252+
// copy genscript package in temp folder
253+
prepareTmpFldr(genScriptLocation);
248254

249255
return matlabLauncher.pwd(workspace).join();
250256
} catch (Exception e) {
@@ -259,11 +265,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
259265
}
260266
}
261267
}
262-
263-
public String constructCommandForTest(String inputArguments) {
268+
269+
public String constructCommandForTest(String inputArguments, String scriptPath) {
264270
final String matlabFunctionName =
265271
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
266-
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
272+
final String runCommand = "addpath(genpath('"+scriptPath+"')); " + matlabFunctionName + "(" + inputArguments + ")";
267273
return runCommand;
268274
}
269275

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public String getDisplayName() {
137137
public String constructCommandForTest(String inputArguments) {
138138
final String matlabFunctionName =
139139
FilenameUtils.removeExtension(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE);
140-
final String runCommand = "exit(" + matlabFunctionName + "(" + inputArguments + "))";
140+
final String runCommand = matlabFunctionName + "(" + inputArguments + ")";
141141
return runCommand;
142142
}
143143

Lines changed: 7 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
%Copyright 2019-2020 The MathWorks, Inc.
1+
%Copyright 2020 The MathWorks, Inc.
22

3-
function failed = runMatlabTests(varargin)
3+
function runMatlabTests(varargin)
44

55
p = inputParser;
66
validationFcn = @(c)ischar(c) && (isempty(c) || isrow(c));
@@ -14,175 +14,16 @@
1414

1515
p.parse(varargin{:});
1616

17-
1817
pdfReportPath = p.Results.PDFReportPath;
1918
tapReportPath = p.Results.TAPResultsPath;
2019
junitReportPath = p.Results.JUnitResultsPath;
2120
stmReportPath = p.Results.SimulinkTestResultsPath;
2221
coberturaReportPath = p.Results.CoberturaCodeCoveragePath;
2322
modelCoveragePath = p.Results.CoberturaModelCoveragePath;
2423

25-
BASE_VERSION_MATLABUNIT_SUPPORT = '8.1';
26-
27-
if verLessThan('matlab',BASE_VERSION_MATLABUNIT_SUPPORT)
28-
error('MATLAB:unitTest:testFrameWorkNotSupported','Running tests automatically is not supported in this relase.');
29-
end
30-
31-
%Create test suite for tests folder
32-
suite = getTestSuite();
33-
34-
% Create and configure the runner
35-
import('matlab.unittest.TestRunner');
36-
runner = TestRunner.withTextOutput;
37-
38-
39-
40-
% Produce JUnit report
41-
if ~isempty(junitReportPath)
42-
BASE_VERSION_JUNIT_SUPPORT = '8.6';
43-
if verLessThan('matlab',BASE_VERSION_JUNIT_SUPPORT)
44-
warning('MATLAB:testArtifact:junitReportNotSupported', 'Producing JUnit xml results is not supported in this release.');
45-
else
46-
import('matlab.unittest.plugins.XMLPlugin');
47-
preparePath(junitReportPath);
48-
runner.addPlugin(XMLPlugin.producingJUnitFormat(junitReportPath));
49-
end
50-
end
51-
52-
% Produce TAP report
53-
if ~isempty(tapReportPath)
54-
BASE_VERSION_TAPORIGINALFORMAT_SUPPORT = '8.3';
55-
BASE_VERSION_TAP13_SUPPORT = '9.1';
56-
if verLessThan('matlab',BASE_VERSION_TAPORIGINALFORMAT_SUPPORT)
57-
warning('MATLAB:testArtifact:tapReportNotSupported', 'Producing TAP results is not supported in this release.');
58-
elseif verLessThan('matlab',BASE_VERSION_TAP13_SUPPORT)
59-
tapFile = getTapResultFile(tapReportPath);
60-
import('matlab.unittest.plugins.TAPPlugin');
61-
tapPlugin = TAPPlugin.producingOriginalFormat(tapFile);
62-
runner.addPlugin(tapPlugin);
63-
else
64-
tapFile = getTapResultFile(tapReportPath);
65-
import('matlab.unittest.plugins.TAPPlugin');
66-
tapPlugin = TAPPlugin.producingVersion13(tapFile);
67-
runner.addPlugin(tapPlugin);
68-
end
69-
70-
end
71-
72-
% Produce Cobertura report (Cobertura report generation is not supported
73-
% below R17a)
74-
if ~isempty(coberturaReportPath)
75-
BASE_VERSION_COBERTURA_SUPPORT = '9.3';
76-
77-
if verLessThan('matlab',BASE_VERSION_COBERTURA_SUPPORT)
78-
warning('MATLAB:testArtifact:coberturaReportNotSupported', 'Producing Cobertura code coverage results is not supported in this release.');
79-
else
80-
import('matlab.unittest.plugins.CodeCoveragePlugin');
81-
preparePath(coberturaReportPath);
82-
workSpace = fullfile(pwd);
83-
runner.addPlugin(CodeCoveragePlugin.forFolder(workSpace,'IncludingSubfolders',true,...
84-
'Producing', CoberturaFormat(coberturaReportPath)));
85-
end
86-
end
87-
88-
% Produce Cobertura model coverage report (Not supported below R2018b)
89-
if ~isempty(modelCoveragePath)
90-
if ~exist('sltest.plugins.ModelCoveragePlugin', 'class') || ~coberturaModelCoverageSupported
91-
warning('MATLAB:testArtifact:cannotGenerateModelCoverageReport', ...
92-
'Unable to generate Cobertura model coverage report. To generate the report, use a Simulink Coverage license with MATLAB R2018b or a newer release.');
93-
else
94-
import('sltest.plugins.ModelCoveragePlugin');
95-
96-
preparePath(modelCoveragePath);
97-
runner.addPlugin(ModelCoveragePlugin('Producing',CoberturaFormat(modelCoveragePath)));
98-
end
99-
end
100-
101-
stmResultsPluginAddedToRunner = false;
102-
103-
% Save Simulink Test Manager results in MLDATX format (Not supported below R2019a)
104-
if ~isempty(stmReportPath)
105-
if ~stmResultsPluginPresent || ~exportSTMResultsSupported
106-
issueExportSTMResultsUnsupportedWarning;
107-
else
108-
preparePath(stmReportPath);
109-
runner.addPlugin(TestManagerResultsPlugin('ExportToFile', stmReportPath));
110-
stmResultsPluginAddedToRunner = true;
111-
end
112-
end
113-
114-
% Produce PDF test report (Not supported on MacOS platforms and below R2017a)
115-
if ~isempty(pdfReportPath)
116-
if ismac
117-
warning('MATLAB:testArtifact:unSupportedPlatform', ...
118-
'Producing a PDF test report is not currently supported on MacOS platforms.');
119-
elseif ~testReportPluginPresent
120-
issuePDFReportUnsupportedWarning;
121-
else
122-
preparePath(pdfReportPath);
123-
import('matlab.unittest.plugins.TestReportPlugin');
124-
runner.addPlugin(TestReportPlugin.producingPDF(pdfReportPath));
125-
126-
if ~stmResultsPluginAddedToRunner && stmResultsPluginPresent
127-
runner.addPlugin(TestManagerResultsPlugin);
128-
end
129-
end
130-
end
131-
132-
results = runner.run(suite);
133-
failed = any([results.Failed]);
134-
135-
function preparePath(path)
136-
dir = fileparts(path);
137-
dirExists = isempty(dir) || exist(dir,'dir') == 7;
138-
if ~dirExists
139-
mkdir(dir);
140-
end
141-
142-
function tapFile = getTapResultFile(resultsDir)
143-
import('matlab.unittest.plugins.ToFile');
144-
preparePath(resultsDir);
145-
fclose(fopen(resultsDir,'w'));
146-
tapFile = matlab.unittest.plugins.ToFile(resultsDir);
147-
148-
function suite = getTestSuite()
149-
import('matlab.unittest.TestSuite');
150-
BASE_VERSION_TESTSUITE_SUPPORT = '9.0';
151-
if verLessThan('matlab',BASE_VERSION_TESTSUITE_SUPPORT)
152-
suite = matlab.unittest.TestSuite.fromFolder(pwd,'IncludingSubfolders',true);
153-
else
154-
suite = testsuite(pwd,'IncludeSubfolders',true);
155-
end
156-
157-
function plugin = CoberturaFormat(varargin)
158-
plugin = matlab.unittest.plugins.codecoverage.CoberturaFormat(varargin{:});
159-
160-
function plugin = TestManagerResultsPlugin(varargin)
161-
plugin = sltest.plugins.TestManagerResultsPlugin(varargin{:});
162-
163-
function tf = testReportPluginPresent
164-
BASE_VERSION_REPORTPLUGIN_SUPPORT = '9.2'; % R2017a
165-
166-
tf = ~verLessThan('matlab',BASE_VERSION_REPORTPLUGIN_SUPPORT);
167-
168-
function tf = stmResultsPluginPresent
169-
tf = logical(exist('sltest.plugins.TestManagerResultsPlugin', 'class'));
170-
171-
function tf = coberturaModelCoverageSupported
172-
BASE_VERSION_MODELCOVERAGE_SUPPORT = '9.5'; % R2018b
173-
174-
tf = ~verLessThan('matlab',BASE_VERSION_MODELCOVERAGE_SUPPORT);
175-
176-
function tf = exportSTMResultsSupported
177-
BASE_VERSION_EXPORTSTMRESULTS_SUPPORT = '9.6'; % R2019a
178-
179-
tf = ~verLessThan('matlab',BASE_VERSION_EXPORTSTMRESULTS_SUPPORT);
180-
181-
function issuePDFReportUnsupportedWarning
182-
warning('MATLAB:testArtifact:pdfReportNotSupported', ...
183-
'Producing a test report in PDF format is not supported in the current MATLAB release.');
24+
testScript = genscript('Test','PDFTestReport',pdfReportPath,'TAPTestResults',tapReportPath,'JUnitTestResults',junitReportPath,'SimulinkTestResults',stmReportPath,'CoberturaCodeCoverage',coberturaReportPath,'CoberturaModelCoverage',modelCoveragePath);
18425

185-
function issueExportSTMResultsUnsupportedWarning
186-
warning('MATLAB:testArtifact:cannotExportSimulinkTestManagerResults', ...
187-
['Unable to export Simulink Test Manager results. This feature ', ...
188-
'requires a Simulink Test license and is supported only in MATLAB R2019a or a newer release.']);
26+
disp('Running MATLAB script with content:\n');
27+
disp(strtrim(testScript.writeToText()));
28+
fprintf('___________________________________\n\n');
29+
run(testScript);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public void verifyMATLABlaunchedWithDefaultArgumentsBatch() throws Exception {
142142
project.getBuildersList().add(this.testBuilder);
143143
FreeStyleBuild build = project.scheduleBuild2(0).get();
144144
jenkins.assertLogContains("run_matlab_command", build);
145-
jenkins.assertLogContains("exit(runMatlabTests", build);
145+
jenkins.assertLogContains("runMatlabTests", build);
146+
jenkins.assertLogContains("addpath(genpath", build);
146147
}
147148

148149
/*
@@ -157,7 +158,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsRWindows() throws Exception
157158
project.getBuildersList().add(testBuilder);
158159
FreeStyleBuild build = project.scheduleBuild2(0).get();
159160
jenkins.assertLogContains("run_matlab_command", build);
160-
jenkins.assertLogContains("exit(runMatlabTests", build);
161+
jenkins.assertLogContains("runMatlabTests", build);
161162
}
162163

163164
/*
@@ -328,7 +329,7 @@ public void veriyEmptyParameters() throws Exception {
328329
project.getBuildersList().add(this.testBuilder);
329330
FreeStyleBuild build = project.scheduleBuild2(0).get();
330331
jenkins.assertLogContains("run_matlab_command", build);
331-
jenkins.assertLogContains("exit(runMatlabTests())", build);
332+
jenkins.assertLogContains("runMatlabTests()", build);
332333
}
333334

334335

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public void verifyArtifactPath() throws Exception {
7777
WorkflowRun build = project.scheduleBuild2(0).get();
7878
j.assertLogContains("'PDFReportPath','myresult/result.pdf'", build);
7979
}
80+
81+
/*
82+
* Verify default command options for test run.
83+
*/
84+
85+
@Test
86+
public void verifyCmdOptions() throws Exception {
87+
project.setDefinition(new CpsFlowDefinition(
88+
"node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
89+
WorkflowRun build = project.scheduleBuild2(0).get();
90+
j.assertLogContains("addpath(genpath", build);
91+
j.assertLogContains("runMatlabTests", build);
92+
}
8093

8194
/*
8295
* Verify Artifact is not sent as parameter if not selected in script.

0 commit comments

Comments
 (0)