Skip to content

Commit 5b6f9b5

Browse files
committed
which: handle multi-paths correctly and add test
1 parent 7f345b1 commit 5b6f9b5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

+stdlib/which.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
end
2828

2929
% path given
30-
if isempty(fpath) || strlength(fpath) == 0
30+
if isempty(fpath) || all(strlength(fpath) == 0)
3131
fpath = string(getenv("PATH"));
3232
end
3333

3434
if isscalar(fpath)
35-
fpath = split(fpath, pathsep);
35+
fpath = split(fpath, pathsep).';
3636
end
3737
fpath = fpath(strlength(fpath)>0);
3838

39-
for p = fpath.'
39+
for p = fpath
4040
e = p + "/" + filename;
4141
if isfile(e) && stdlib.is_exe(e)
4242
if find_all

test/TestWhich.m

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ function test_which_fullpath(tc)
2525
import matlab.unittest.constraints.IsFile
2626
import matlab.unittest.constraints.EndsWithSubstring
2727

28-
n = "matlab";
2928
%% is_exe test
30-
p = matlabroot + "/bin/" + n;
31-
if ispc
32-
p = p + ".exe";
33-
end
29+
p = matlabroot + "/bin/" + matlab_name();
30+
3431
tc.assumeTrue(stdlib.is_exe(p), "Matlab not executable " + p)
3532
%% which: test absolute path
3633
exe = stdlib.which(p);
3734

35+
tc.verifyNotEmpty(exe, "Matlab not found " + p)
36+
3837
if ispc
3938
tc.verifyThat(exe, EndsWithSubstring(".exe"))
4039
else
@@ -44,6 +43,29 @@ function test_which_fullpath(tc)
4443

4544
end
4645

46+
function test_which_multipath(tc)
47+
48+
n = matlab_name();
49+
50+
paths = string(getenv("PATH"));
51+
paths = split(paths, pathsep);
52+
paths(end+1) = matlabroot + "/bin";
53+
54+
exe = stdlib.which(n, paths);
55+
56+
tc.verifyNotEmpty(exe, "Matlab not found by which()")
57+
4758
end
4859

4960
end
61+
62+
end
63+
64+
65+
function n = matlab_name()
66+
67+
n = "matlab";
68+
if ispc
69+
n = n + ".exe";
70+
end
71+
end

0 commit comments

Comments
 (0)