Skip to content

Commit 0527cfd

Browse files
committed
which: more versions supported
1 parent 7287c1a commit 0527cfd

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

+stdlib/which.m

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88
% find_all option finds all executables specified under PATH, instead of only the first
99

1010
function exe = which(cmd, fpath, find_all)
11-
arguments
12-
cmd (1,1) string
13-
fpath (1,:) string = string.empty
14-
find_all (1,1) logical = false
11+
% arguments
12+
% cmd (1,1) string
13+
% fpath (1,:) string = string.empty
14+
% find_all (1,1) logical = false
15+
% end
16+
if nargin < 2
17+
fpath = string.empty;
18+
end
19+
if nargin < 3
20+
find_all = false;
1521
end
1622

1723
exe = string.empty;
1824

1925
%% on Windows, append .exe if not suffix is given
2026
if ispc() && stdlib.strempty(stdlib.suffix(cmd))
21-
cmd = cmd + ".exe";
27+
cmd = stdlib.append(cmd, '.exe');
2228
end
2329
%% full filename was given
2430
if stdlib.is_exe(cmd)
@@ -34,11 +40,17 @@
3440

3541
% path given
3642
if stdlib.strempty(fpath)
37-
fpath = string(getenv('PATH'));
43+
fpath = getenv('PATH');
3844
end
3945

46+
fpath = string(fpath);
47+
4048
if isscalar(fpath)
41-
fpath = split(fpath, pathsep).';
49+
fpath = split(fpath, pathsep);
50+
end
51+
52+
if iscolumn(fpath)
53+
fpath = fpath.';
4254
end
4355

4456
for p = fpath

test/TestWhich.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
TestWhich < matlab.unittest.TestCase
44

55
properties (TestParameter)
6-
mexe = {matlabroot + "/bin/matlab", ...
6+
mexe = {[matlabroot '/bin/matlab'], ...
77
fullfile(matlabroot, 'bin/matlab')}
88
end
99

1010

11-
methods (Test, TestTags = {'R2019b'})
11+
methods (Test, TestTags = {'R2017b'})
1212

1313
function test_which_name(tc)
1414

@@ -27,7 +27,7 @@ function test_which_name(tc)
2727
for n = names
2828
exe = stdlib.which(n);
2929
tc.verifyNotEmpty(exe, "Executable not found: " + n)
30-
tc.verifyThat(exe, matlab.unittest.constraints.IsFile, "Executable is not a file: " + n)
30+
tc.verifyTrue(isfile(exe), "Executable is not a file: " + n)
3131
tc.verifyTrue(stdlib.is_exe(exe), "Executable is not executable: " + n)
3232
end
3333

@@ -36,12 +36,12 @@ function test_which_name(tc)
3636

3737
function test_which_absolute(tc, mexe)
3838

39-
r = string(mexe);
39+
r = mexe;
4040
if ispc()
41-
r = r + ".exe";
41+
r = [r, '.exe'];
4242
end
4343

44-
tc.assumeThat(r, matlab.unittest.constraints.IsFile)
44+
tc.assumeTrue(isfile(r))
4545
tc.assumeTrue(stdlib.is_exe(r))
4646

4747
tc.verifyGreaterThan(strlength(stdlib.which(r)), 0, "Expected which(" + r + " ) to find " + r)
@@ -72,7 +72,7 @@ function testWhichNoPath(tc)
7272
tc.applyFixture(fx)
7373

7474
tc.verifyEmpty(stdlib.which('matlab'), "Matlab found by which() given empty path")
75-
tc.verifyNotEmpty(stdlib.which('matlab', matlabroot + "/bin"))
75+
tc.verifyNotEmpty(stdlib.which('matlab', [matlabroot, '/bin']))
7676
end
7777
end
7878

0 commit comments

Comments
 (0)