Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/com/mathworks/ci/MatlabBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ public interface MatlabBuild {
* @param matlabCommand MATLAB command to execute on shell
* @return matlabLauncher returns the process launcher to run MATLAB commands
*/
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
throws IOException, InterruptedException {
// Get node specific tmp directory to copy matlab runner script
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
ProcStarter matlabLauncher;
if (launcher.isUnix()) {
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
matlabLauncher = launcher.launch().envs(envVars);
matlabLauncher.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);

// Copy runner .sh for linux platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
targetWorkspace);
} else {
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
matlabLauncher = launcher.launch().envs(envVars);
matlabLauncher.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
.stdout(listener);
// Copy runner.bat for Windows platform in workspace.
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,

// Invoke MATLAB command and transfer output to standard
// Output Console


buildResult = execMatlabCommand(workspace, launcher, listener, getEnv());

Expand All @@ -112,22 +113,46 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
final String uniqueCommandFile =
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
final FilePath uniqeTmpFolderPath =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);

// Create MATLAB script
createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener);
ProcStarter matlabLauncher;

try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
getCommand(), uniqueTmpFldrName);
return matlabLauncher.join();
uniqueCommandFile, uniqueTmpFldrName);
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
listener.getLogger()
.println("#################### Starting command output ####################");
return matlabLauncher.pwd(uniqeTmpFolderPath).join();

} catch (Exception e) {
listener.getLogger().println(e.getMessage());
return 1;
} finally {
// Cleanup the runner File from tmp directory
FilePath matlabRunnerScript =
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
if (matlabRunnerScript.exists()) {
matlabRunnerScript.deleteRecursive();
// Cleanup the tmp directory
if (uniqeTmpFolderPath.exists()) {
uniqeTmpFolderPath.deleteRecursive();
}
}

}

private void createMatlabScriptByName(FilePath uniqeTmpFolderPath, String uniqueScriptName, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {

// Create a new command runner script in the temp folder.
final FilePath matlabCommandFile =
new FilePath(uniqeTmpFolderPath, uniqueScriptName + ".m");
final String matlabCommandFileContent =
"cd '" + workspace.getRemote().replaceAll("'", "''") + "';\n" + getCommand();

// Display the commands on console output for users reference
listener.getLogger()
.println("Generating MATLAB script with content:\n" + getCommand() + "\n");

matlabCommandFile.write(matlabCommandFileContent, "UTF-8");
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,13 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
try {
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
constructCommandForTest(getInputArguments()), uniqueTmpFldrName);

// Copy MATLAB scratch file into the workspace.
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
copyFileInWorkspace(MatlabBuilderConstants.MATLAB_TESTS_RUNNER_RESOURCE,
MatlabBuilderConstants.MATLAB_TESTS_RUNNER_TARGET_FILE, targetWorkspace);
return matlabLauncher.join();

return matlabLauncher.pwd(workspace).join();
} catch (Exception e) {
listener.getLogger().println(e.getMessage());
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div>
Enter the command you want to run in MATLAB. If the command represents a MATLAB function or script, do not specify the file extension.<br>
<b>Examples:</b><br>
<b>Run commands:</b> results = runtests(‘IncludingSubfolders’,true); assert(all(~[results.Failed]))<br>
<b>Run a script:</b> runMyScript<br>
Insert the MATLAB commands you want to execute in the <b>Command</b> box. If you need to specify more than one command, use a comma or semicolon to separate the commands.<br>
<b>Example: </b>results = runtests, assertSuccess(results);<br><br>
If you need to specify several MATLAB commands, consider writing a MATLAB script or function and executing this script or function instead. (To run a script or function, do not specify the file extension.)<br>
<b>Example: </b>myscript<br>
<br>&nbsp;</br>
<b>Recommendation:</b>If you require a number of MATLAB commands to execute your build, consider writing a MATLAB script and executing the script file instead.<br>
<b>Note:</b> The build will fail if the execution of any MATLAB command causes an error.
<br>
<b>Note:</b><ul><li>The build fails if the execution of any command results in an error.</li>
<li>If the build uses a MATLAB version prior to R2020a, MATLAB might display non-ASCII characters, specified in the <b>Command</b> box, as garbled text. If you specify non-ASCII characters in your commands, consider running your commands as a .m or .mlx file created in the same locale that MATLAB uses on the build agent</li>
</ul>
</div>
26 changes: 25 additions & 1 deletion src/test/java/com/mathworks/ci/RunMatlabCommandBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public void verifyMATLABlaunchedWithDefaultArguments() throws Exception {
project.getBuildersList().add(this.scriptBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
jenkins.assertLogContains("run_matlab_command", build);
jenkins.assertLogContains("pwd", build);
}

/*
Expand Down Expand Up @@ -185,6 +184,7 @@ public void verifyBuildFailureWhenMatlabCommandPasses() throws Exception {

/*
* Test to verify Builder picks the exact command that user entered.
*
*/

@Test
Expand All @@ -195,6 +195,7 @@ public void verifyBuildPicksTheCorretCommandBatch() throws Exception {
project.getBuildersList().add(this.scriptBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
jenkins.assertLogContains("run_matlab_command", build);
jenkins.assertLogContains("Generating MATLAB script with content", build);
jenkins.assertLogContains("pwd", build);
}

Expand All @@ -214,6 +215,7 @@ public void verifyMATLABscratchFileNotGenerated() throws Exception {

/*
* Test to verify command supports resolving environment variable (For MATRIX builds).
*
*/
@Test
public void verifyCommandSupportsEnvVar() throws Exception {
Expand Down Expand Up @@ -291,4 +293,26 @@ public void verifyMatrixBuildPasses() throws Exception {
jenkins.assertLogContains("R2018b completed", build);
jenkins.assertBuildStatus(Result.SUCCESS, build);
}

/*
* Test to verify if command parses succesfully when multiple combinations of
* characters are passed. (candidate for integ-tests once integrated)
*/


public void verifyMultispecialChar() throws Exception {
final String actualCommand =
"!\"\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
final String expectedCommand =
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
this.buildWrapper.setMatlabRootFolder(getMatlabroot("R2018b"));

project.getBuildWrappersList().add(this.buildWrapper);
scriptBuilder.setMatlabCommand("disp(" + actualCommand + ")");
project.getBuildersList().add(this.scriptBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();

jenkins.assertLogContains("Generating MATLAB script with content", build);
jenkins.assertLogContains(expectedCommand, build);
}
}