Skip to content

Commit 2f51b80

Browse files
support multi-part cellstr commands
1 parent 56e6f92 commit 2f51b80

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

matlab/jobs/mjsRunJob.m

+4-5
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ function doCommand(command, name)
5252
eval(command);
5353
printTimestamp('...did %s command', name);
5454
elseif iscell(command)
55-
commandString = strtrim(evalc('disp(command)'));
56-
printTimestamp('...doing %s command <%s>', name, commandString);
57-
feval(command{:});
58-
printTimestamp('...did %s command', name);
59-
end
55+
for cc = 1:numel(command)
56+
doCommand(command{cc}, name);
57+
end
58+
end

matlab/jobs/mjsSaveJob.m

-15
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,10 @@
1919
jobFile = parser.Results.jobFile;
2020

2121

22-
%% Can't serialize function handles to json.
23-
job.setupCommand = commandFunctionToString(job.setupCommand);
24-
job.toolboxCommand = commandFunctionToString(job.toolboxCommand);
25-
job.jobCommand = commandFunctionToString(job.jobCommand);
26-
job.cleanupCommand = commandFunctionToString(job.cleanupCommand);
27-
28-
2922
%% Make sure target dir exists.
3023
jobDir = fileparts(jobFile);
3124
if ~isempty(jobDir) && 7 ~= exist(jobDir, 'dir')
3225
mkdir(jobDir);
3326
end
3427

3528
jobJson = savejson('', job, jobFile);
36-
37-
38-
%% Try to convert function_handle command to equivalent strings.
39-
function command = commandFunctionToString(command)
40-
41-
if iscell(command) && ~isempty(command) && isa(command{1}, 'function_handle')
42-
command{1} = func2str(command{1});
43-
end

matlab/test/MjsLocalJobTests.m

+26-5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ function freshTempDir(testCase)
2525
'jobCommand', command);
2626
end
2727

28+
function [job, intValue, outputFile] = multiPartCommandJob(testCase)
29+
intValue = randi(1e9);
30+
outputFile = fullfile(testCase.tempDir, 'integerSaverJob.mat');
31+
command = { ...
32+
sprintf('evalin(''base'', ''intValue=%d'');', intValue), ...
33+
sprintf('evalin(''base'', ''save(''''%s'''')'', ''intValue'');', outputFile)};
34+
job = mjsJob( ...
35+
'name', 'multiPartCommandJob', ...
36+
'jobCommand', command);
37+
end
38+
2839
function [job, errorMessage] = errorJob(testCase)
2940
errorMessage = 'This is a test error.';
30-
command = {@error, errorMessage};
41+
command = sprintf('error(''%s'')', errorMessage);
3142
job = mjsJob( ...
3243
'name', 'errorJob', ...
3344
'jobCommand', command);
@@ -55,6 +66,16 @@ function testInDockerSuccess(testCase)
5566
testCase.assertEqual(jobOutput.intValue, intValue);
5667
end
5768

69+
function testMultiPartCommandSuccess(testCase)
70+
[job, intValue, outputFile] = testCase.multiPartCommandJob();
71+
mjsExecuteLocal(job, ...
72+
'scriptFile', fullfile(testCase.tempDir, 'scripts', [job.name '.sh']), ...
73+
'outputDir', testCase.tempDir);
74+
testCase.assertEqual(exist(outputFile, 'file'), 2);
75+
jobOutput = load(outputFile);
76+
testCase.assertEqual(jobOutput.intValue, intValue);
77+
end
78+
5879
function testInMatlabError(testCase)
5980
errorMessage = 'no error';
6081
[job, expectedMessage] = testCase.errorJob();
@@ -91,7 +112,7 @@ function testInDockerInputFolderPathError(testCase)
91112
% "forget" to add the input folder that contains the function
92113
job = mjsJob( ...
93114
'name', 'forgotInputDir', ...
94-
'jobCommand', {@deepTestFunction});
115+
'jobCommand', 'deepTestFunction');
95116
status = mjsExecuteLocal(job, ...
96117
'scriptFile', fullfile(testCase.tempDir, 'scripts', [job.name '.sh']), ...
97118
'outputDir', testCase.tempDir);
@@ -113,7 +134,7 @@ function testInDockerInputFolderPathSuccess(testCase)
113134
% "remember" to add the input folder that contains the function
114135
job = mjsJob( ...
115136
'name', 'rememberedInputDir', ...
116-
'jobCommand', {@deepTestFunction});
137+
'jobCommand', 'deepTestFunction');
117138
[status, result] = mjsExecuteLocal(job, ...
118139
'scriptFile', fullfile(testCase.tempDir, 'scripts', [job.name '.sh']), ...
119140
'inputDir', inputDir, ...
@@ -155,7 +176,7 @@ function testInDockerWithCommonToolbox(testCase)
155176

156177
% make a job that uses the same toolbox
157178
job = testCase.integerSaverJob();
158-
job.toolboxCommand = {@tbUse, 'sample-repo'};
179+
job.toolboxCommand = 'tbUse(''sample-repo'')';
159180

160181
% run the job with the shared toolbox folder mapped in
161182
[status, result] = mjsExecuteLocal(job, ...
@@ -179,7 +200,7 @@ function testOutputOwner(testCase)
179200
'outputDir', testCase.tempDir, ...
180201
'outputOwner', 'current');
181202
testCase.assertEqual(status, 0);
182-
203+
183204
% output file should contain expected data
184205
testCase.assertEqual(exist(outputFile, 'file'), 2);
185206
jobOutput = load(outputFile);

0 commit comments

Comments
 (0)