Skip to content

Commit 161e4af

Browse files
committed
add subprocess_run test
1 parent 48d0fe9 commit 161e4af

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

+stdlib/+sys/is_windows_powershell.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function iswps = is_windows_powershell()
2+
%% detects Windows powershell vs. Command Prompt
3+
% Matlab on Windows Powershell vs Comspec for system()
4+
% would impact syntax of some system() commands
5+
%
6+
% adapted from: https://stackoverflow.com/a/34480405/7703794
7+
8+
iswps = false;
9+
if ~ispc
10+
return
11+
end
12+
13+
[ret, ~] = system("dir 2>&1 *`");
14+
iswps = ret == 0;
15+
16+
end

+stdlib/+sys/subprocess_run.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@
2727
cmd = exe;
2828
end
2929

30-
if ~isempty(fieldnames(opt.env))
31-
for f = string(fieldnames(opt.env)).'
32-
if ispc
33-
cmd = append("set ", f, "=", opt.env.(f), " && ", cmd);
34-
else
35-
cmd = append(f, "=", opt.env.(f), " ", cmd);
36-
end
30+
for f = string(fieldnames(opt.env)).'
31+
if ispc
32+
cmd = append("set ", f, "=", opt.env.(f), " && ", cmd);
33+
else
34+
cmd = append(f, "=", opt.env.(f), " ", cmd);
3735
end
3836
end
3937

+stdlib/TestSys.m

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22

33
methods (Test)
44

5-
function test_run(tc)
5+
function test_simple_run(tc)
6+
7+
import stdlib.sys.subprocess_run
8+
69
if ispc
7-
cmd = 'dir';
10+
c = 'dir';
811
else
9-
cmd = 'ls';
12+
c = 'ls';
1013
end
11-
tc.verifyEqual(stdlib.sys.subprocess_run(cmd), 0)
14+
tc.verifyEqual(subprocess_run(c), 0)
15+
end
16+
17+
18+
function test_env_run(tc)
19+
20+
import stdlib.sys.subprocess_run
21+
22+
names = ["TEST1", "TEST2"];
23+
vals = ["test123", "test321"];
24+
25+
env = struct(names(1), vals(1), names(2), vals(2));
26+
27+
% NOTE: test function cannot get specific variables without invoking a
28+
% subshell, as echo is evaluated in current shell before &&
29+
if ispc
30+
c = "set";
31+
else
32+
c = "env";
33+
end
34+
[ret, msg] = subprocess_run(c, env=env);
35+
tc.verifyEqual(ret, 0)
36+
tc.verifyTrue(contains(msg, names(1) + "=" + vals(1)) && contains(msg, names(2) + "=" + vals(2)))
37+
1238
end
1339

1440
function test_find_fortran(tc)

0 commit comments

Comments
 (0)