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

Commit 12917d8

Browse files
committed
Added unzipping strategy
1 parent 71d98da commit 12917d8

File tree

6 files changed

+71
-34
lines changed

6 files changed

+71
-34
lines changed

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

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*/
88

99
import java.io.IOException;
10+
1011
import java.io.InputStream;
12+
import java.net.URL;
13+
1114
import org.apache.commons.lang.RandomStringUtils;
1215
import hudson.EnvVars;
1316
import hudson.FilePath;
@@ -21,10 +24,10 @@ public interface MatlabBuild {
2124
* This Method decorates the launcher with MATLAB command provided and returns the Process
2225
* object to launch MATLAB with appropriate startup options like -r or -batch
2326
*
24-
* @param workspace Current build workspace
27+
* @param workspace Current build workspace
2528
* @param launcher Current build launcher
26-
* @param listener Current build listener
27-
* @param envVars Environment variables of the current build
29+
* @param listener Current build listener
30+
* @param envVars Environment variables of the current build
2831
* @param matlabCommand MATLAB command to execute on shell
2932
* @return matlabLauncher returns the process launcher to run MATLAB commands
3033
*/
@@ -67,6 +70,8 @@ default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath
6770
targetFilePath.copyFrom(in);
6871
// set executable permission
6972
targetFilePath.chmod(0755);
73+
74+
7075
}
7176

7277
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName,
@@ -81,30 +86,44 @@ default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName
8186
default String getUniqueNameForRunnerFile() {
8287
//Using 8 bit long random alphanumeric string
8388
return RandomStringUtils.randomAlphanumeric(8);
84-
}
89+
}
8590

8691
// This method prepares the temp folder by coping all helper files in it.
8792
default void prepareTmpFldr(FilePath tmpFldr, String runnerScript) throws IOException, InterruptedException {
88-
// Write MATLAB scratch file in temp folder.
89-
FilePath scriptFile =
90-
new FilePath(tmpFldr, getValidMatlabFileName(tmpFldr.getBaseName()) + ".m");
91-
scriptFile.write(runnerScript, "UTF-8");
92-
// copy genscript package
93-
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
94-
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
95-
FilePath zipFileLocation =
96-
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR);
93+
94+
// copy genscript package
95+
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR,
96+
MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR, tmpFldr);
97+
FilePath zipFileLocation =
98+
new FilePath(tmpFldr, MatlabBuilderConstants.MATLAB_SCRIPT_GENERATOR);
99+
97100

98-
// Unzip the file in temp folder.
99-
zipFileLocation.unzip(tmpFldr);
101+
runnerScript=getZipScript(runnerScript, zipFileLocation.getRemote());
102+
103+
// Write MATLAB scratch file in temp folder.
104+
FilePath scriptFile =
105+
new FilePath(tmpFldr, getValidMatlabFileName(tmpFldr.getBaseName()) + ".m");
106+
scriptFile.write(runnerScript, "UTF-8");
107+
108+
109+
110+
}
111+
112+
default String getZipScript(String script, String url) {
113+
script = script.replace("${ZIP_FILE}", url);
114+
return script;
100115
}
101116

117+
118+
102119
default String getRunnerScript(String script, String params, String uniqueTmpFldrName) {
103120
script = script.replace("${PARAMS}", params);
104-
script = script.replaceAll("\\$\\{TMPDIR\\}", uniqueTmpFldrName);
121+
// script = script.replaceAll("\\$\\{TMPDIR\\}", uniqueTmpFldrName);
105122
return script;
106123
}
107124

125+
126+
108127
default String getValidMatlabFileName(String actualName) {
109128
return MatlabBuilderConstants.MATLAB_TEST_RUNNER_FILE_PREFIX
110129
+ actualName.replaceAll("-", "_");

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package com.mathworks.ci;
1+
package com.mathworks.ci;
2+
23
/*
34
* Copyright 2019-2020 The MathWorks, Inc.
45
*/
@@ -37,14 +38,22 @@ public class MatlabBuilderConstants {
3738
//Temporary MATLAB folder name in workspace
3839
static final String TEMP_MATLAB_FOLDER_NAME = ".matlab";
3940

40-
// MATLAB runner script
41-
static final String TEST_RUNNER_SCRIPT = "testScript = genscript(${PARAMS});\n" + "\n"
42-
+ "disp('Running MATLAB script with content:');\n"
43-
+ "disp(strtrim(testScript.Contents));\n"
44-
+ "runnerFile = testScript.writeToFile(fullfile(tempname,'runnerScript.m'));\n"
45-
+ "[tmpDir,scriptName,ext] = fileparts(runnerFile);"
46-
+ "addpath(tmpDir);\n"
47-
+ "rmdir('.matlab/${TMPDIR}/+scriptgen', 's');\n"
48-
+ "delete('.matlab/${TMPDIR}/genscript.m');\n"
49-
+ "fprintf('___________________________________\\n\\n');\n";
41+
42+
43+
static final String TEST_RUNNER_SCRIPT="destination=tempname;\n"
44+
+"mkdir(destination);\n"
45+
+ "addpath(destination);\n"
46+
+"zipURL='${ZIP_FILE}';\n"
47+
+"unzip(zipURL,destination);\n"
48+
+"disp('Unziping genscript resource in');\n"
49+
+"disp(destination);\n"
50+
+ "testScript = genscript(${PARAMS});\n"
51+
+ "disp('Running MATLAB script with content:');\n"
52+
+ "disp(strtrim(testScript.Contents));\n"
53+
+ "runnerFile = testScript.writeToFile(fullfile(destination,'runnerScript.m'));\n"
54+
+"warning('off');"
55+
+ "fprintf('___________________________________\\n\\n');\n";
56+
57+
5058
}
59+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
7272
getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
7373
cmdPrefix + matlabScriptName + ",delete('.matlab/"
7474
+ genScriptLocation.getBaseName() + "/" + matlabScriptName
75-
+ ".m'),runnerScript,rmdir(tmpDir,'s')",
75+
+ ".m'),runnerScript,rmdir(destination,'s')",
7676
uniqueMatlabResourceFldr);
7777

7878
// prepare temp folder by coping genscript package and writing runner script.

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010

1111
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
1214
import java.util.Map;
1315
import java.util.HashMap;
1416
import java.util.List;
@@ -262,7 +264,7 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
262264
}
263265
}
264266

265-
private int execMatlabCommand(FilePath workspace, Launcher launcher,
267+
private int execMatlabCommand(FilePath workspace, Launcher launcher,/////change needs to be made here.
266268
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
267269

268270
/*
@@ -273,14 +275,17 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
273275

274276
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
275277
ProcStarter matlabLauncher;
278+
276279
try {
280+
277281
FilePath genScriptLocation =
278282
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
279283

280284
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
281285
constructCommandForTest(genScriptLocation), uniqueTmpFldrName);
282286

283287
// copy genscript package in temp folder and write a runner script.
288+
284289
prepareTmpFldr(genScriptLocation, getRunnerScript(
285290
MatlabBuilderConstants.TEST_RUNNER_SCRIPT, envVars.expand(getInputArguments()),uniqueTmpFldrName));
286291

@@ -292,17 +297,18 @@ private int execMatlabCommand(FilePath workspace, Launcher launcher,
292297
// Cleanup the runner File from tmp directory
293298
FilePath matlabRunnerScript =
294299
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
295-
if (matlabRunnerScript.exists()) {
300+
if (matlabRunnerScript.exists()) {
296301
matlabRunnerScript.deleteRecursive();
297302
}
298303
}
299304
}
300305

301306
public String constructCommandForTest(FilePath scriptPath) {
302307
final String matlabScriptName = getValidMatlabFileName(scriptPath.getBaseName());
308+
303309
final String runCommand = "addpath('" + scriptPath.getRemote().replaceAll("'", "''")
304310
+ "'); " + matlabScriptName + ",delete('.matlab/" + scriptPath.getBaseName() + "/"
305-
+ matlabScriptName + ".m'),runnerScript,rmdir(tmpDir,'s')";
311+
+ matlabScriptName + ".m'),runnerScript,rmdir(destination,'s')";
306312
return runCommand;
307313
}
308314

src/main/resources/com/mathworks/ci/RunMatlabTestsBuilder/config.jelly

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
<f:block>
1818
<br></br>
1919
<b>Generate Test Artifacts</b>
20+
2021
<f:optionalBlock name="pdfReportArtifact" field="pdfReportArtifact" title="PDF test report" checked="${instance.pdfReportArtifact.selected}">
2122
<f:entry field="pdfReportFilePath" title="File path: ">
2223
<f:textbox default="matlabTestArtifacts/testreport.pdf"/>
2324
</f:entry>
2425
</f:optionalBlock>
2526

2627
<f:optionalBlock name="tapArtifact" field="tapArtifact" title="TAP test results" checked="${instance.tapArtifact.selected}">
27-
<f:entry field="tapReportFilePath" title="File path: ">
28+
<f:entry field="tapReportFilePath" title="File path: ">
2829
<f:textbox default="matlabTestArtifacts/taptestresults.tap"/>
2930
</f:entry>
3031
</f:optionalBlock>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.mathworks.ci;
22
/**
33
* Copyright 2019-2020 The MathWorks, Inc.
4+
45
*
56
* Test class for RunMatlabTestsBuilder
67
*
78
*/
89

910
import java.io.File;
11+
1012
import java.io.IOException;
1113
import java.net.URISyntaxException;
1214
import java.net.URL;
@@ -154,7 +156,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsBatch() throws Exception {
154156
public void verifyMATLABlaunchedWithDefaultArgumentsRWindows() throws Exception {
155157
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a")));
156158
project.getBuildWrappersList().add(this.buildWrapper);
157-
project.getBuildersList().add(testBuilder);
159+
project.getBuildersList().add(testBuilder);//adds latest plugin
158160
FreeStyleBuild build = project.scheduleBuild2(0).get();
159161
jenkins.assertLogContains("run_matlab_command", build);
160162
jenkins.assertLogContains("runner", build);
@@ -522,6 +524,6 @@ public void verifySystemTempDirDeleted() throws Exception {
522524
project.getBuildWrappersList().add(this.buildWrapper);
523525
project.getBuildersList().add(testBuilder);
524526
FreeStyleBuild build = project.scheduleBuild2(0).get();
525-
jenkins.assertLogContains("rmdir(tmpDir,'s')", build);
527+
jenkins.assertLogContains("rmdir(destination,'s')", build);
526528
}
527529
}

0 commit comments

Comments
 (0)