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

Commit 100647f

Browse files
authored
Merge pull request #119 from mathworks/github_startup_issue_117
Run MATLAB command using cd and invoke MATLAB from workspace.
2 parents cfec84c + 4198ff9 commit 100647f

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,27 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
6363
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
6464
final String uniqueCommandFile =
6565
"command_" + getUniqueNameForRunnerFile().replaceAll("-", "_");
66-
final FilePath uniqeTmpFolderPath =
66+
final FilePath uniqueTmpFolderPath =
6767
getFilePathForUniqueFolder(launcher, uniqueTmpFldrName, workspace);
6868

6969
// Create MATLAB script
70-
createMatlabScriptByName(uniqeTmpFolderPath, uniqueCommandFile, workspace, listener);
70+
createMatlabScriptByName(uniqueTmpFolderPath, uniqueCommandFile, workspace, listener);
7171
ProcStarter matlabLauncher;
7272

7373
try {
7474
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
75-
uniqueCommandFile, uniqueTmpFldrName);
76-
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
75+
"cd('"+ uniqueTmpFolderPath.getRemote().replaceAll("'", "''") +"'); "+ uniqueCommandFile, uniqueTmpFldrName);
7776
listener.getLogger()
7877
.println("#################### Starting command output ####################");
79-
return matlabLauncher.pwd(uniqeTmpFolderPath).join();
78+
return matlabLauncher.pwd(workspace).join();
8079

8180
} catch (Exception e) {
8281
listener.getLogger().println(e.getMessage());
8382
return 1;
8483
} finally {
8584
// Cleanup the tmp directory
86-
if (uniqeTmpFolderPath.exists()) {
87-
uniqeTmpFolderPath.deleteRecursive();
85+
if (uniqueTmpFolderPath.exists()) {
86+
uniqueTmpFolderPath.deleteRecursive();
8887
}
8988
}
9089
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
124124

125125
try {
126126
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
127-
uniqueCommandFile, uniqueTmpFldrName);
128-
launcher.launch().pwd(uniqeTmpFolderPath).envs(envVars);
127+
"cd('"+ uniqeTmpFolderPath.getRemote().replaceAll("'", "''") +"');"+ uniqueCommandFile, uniqueTmpFldrName);
128+
129129
listener.getLogger()
130130
.println("#################### Starting command output ####################");
131-
return matlabLauncher.pwd(uniqeTmpFolderPath).join();
131+
return matlabLauncher.pwd(workspace).join();
132132

133133
} catch (Exception e) {
134134
listener.getLogger().println(e.getMessage());

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ public void verifyMATLABlaunchedWithDefaultArguments() throws Exception {
136136
jenkins.assertLogContains("run_matlab_command", build);
137137
}
138138

139+
/*
140+
* Test To verify MATLAB is launched always from build workspace.
141+
*
142+
*/
143+
144+
@Test
145+
public void verifyMATLABlaunchedfromWorkspace() throws Exception {
146+
this.buildWrapper.setMatlabRootFolder(getMatlabroot("R2017a"));
147+
project.getBuildWrappersList().add(this.buildWrapper);
148+
scriptBuilder.setMatlabCommand("pwd");
149+
project.getBuildersList().add(this.scriptBuilder);
150+
FreeStyleBuild build = project.scheduleBuild2(0).get();
151+
String workspace = build.getWorkspace().getName();
152+
jenkins.assertLogContains("[" + workspace + "]", build);
153+
}
154+
139155
/*
140156
* Test to verify if job fails when invalid MATLAB path is provided and Exception is thrown
141157
*/

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.mathworks.ci;
22
/**
33
* Copyright 2020 The MathWorks, Inc.
4-
*
4+
*
55
*/
66

77
import java.io.IOException;
@@ -43,6 +43,23 @@ public void verifyMATLABPathNotSet() throws Exception {
4343
j.assertLogContains("MATLAB_ROOT", build);
4444
}
4545

46+
/*
47+
* Verify MATLAB gets invoked from workspace.
48+
*/
49+
50+
@Test
51+
public void verifyMATLABstartsInWorkspace() throws Exception {
52+
DumbSlave s = j.createOnlineSlave();
53+
project.setDefinition(
54+
new CpsFlowDefinition("node('!master') { runMATLABCommand(command: 'pwd')}", true));
55+
56+
FilePath workspace = s.getWorkspaceFor(project);
57+
String workspaceName = workspace.getName();
58+
WorkflowRun build = project.scheduleBuild2(0).get();
59+
60+
j.assertLogContains(workspaceName, build);
61+
}
62+
4663
/*
4764
* Verify MATLAB is invoked when valid MATLAB is in PATH.
4865
*
@@ -65,12 +82,11 @@ public void verifyMATLABPathSet() throws Exception {
6582
public void verifyPipelineOnSlave() throws Exception {
6683
DumbSlave s = j.createOnlineSlave();
6784
project.setDefinition(new CpsFlowDefinition(
68-
"node('!master') { testMATLABCommand(command: 'pwd')}",
69-
true));
85+
"node('!master') { testMATLABCommand(command: 'pwd')}", true));
7086

7187
s.getWorkspaceFor(project);
7288
WorkflowRun build = project.scheduleBuild2(0).get();
73-
89+
7490
j.assertBuildStatusSuccess(build);
7591
}
7692

0 commit comments

Comments
 (0)