Skip to content

Commit fcaa3cd

Browse files
committed
which: more robust, add tests
1 parent 51a71a1 commit fcaa3cd

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

+stdlib/which.m

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,46 @@
99

1010
function exe = which(filename, fpath, find_all)
1111
arguments
12-
filename (1,1) string
12+
filename {mustBeTextScalar}
1313
fpath (1,:) string = string.empty
1414
find_all (1,1) logical = false
1515
end
1616

17-
exe = string.empty;
17+
if find_all
18+
exe = string.empty;
19+
else
20+
exe = '';
21+
end
1822

1923
if isfile(filename) && stdlib.is_exe(filename)
2024
exe = filename;
2125
return
2226
end
2327

2428
% relative directory component, but path was not a file
25-
if stdlib.filename(filename) ~= filename
29+
if ~strcmp(stdlib.filename(filename), filename)
2630
return
2731
end
2832

2933
% path given
3034
if isempty(fpath)
31-
fpath = string(getenv("PATH"));
35+
fpath = getenv("PATH");
3236
end
3337

34-
if isscalar(fpath)
35-
fpath = split(fpath, pathsep).';
38+
if isscalar(fpath) || ischar(fpath)
39+
fpath = strsplit(fpath, pathsep);
3640
end
3741

38-
for p = fpath
42+
for fp = fpath
43+
if iscell(fp)
44+
p = fp{1};
45+
else
46+
p = fp;
47+
end
48+
3949
if ~strlength(p), continue, end
4050

41-
e = p + "/" + filename;
51+
e = strcat(p, '/', filename);
4252
if isfile(e) && stdlib.is_exe(e)
4353
if find_all
4454
exe(end+1) = e; %#ok<AGROW>
@@ -51,4 +61,4 @@
5161

5262
end
5363

54-
%!testif 0
64+
%!assert(~isempty(which("octave", [], false)))

test/TestWhich.m

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
classdef TestWhich < matlab.unittest.TestCase
22

3+
properties (TestParameter)
4+
mexe = {matlabroot + "/bin/" + matlab_name(), ...
5+
fullfile(matlabroot, 'bin', matlab_name())}
6+
end
7+
38
methods (Test)
49

510
function test_which_name(tc)
611

712
tc.verifyEmpty(stdlib.which(tempname()))
813

914
if ispc
10-
n = "pwsh.exe";
15+
n = 'pwsh.exe';
1116
else
12-
n = "ls";
17+
n = 'ls';
1318
end
1419
%% which: Matlab in environment variable PATH
1520
% MacOS Matlab does not source .zshrc so Matlab is not on internal Matlab PATH
@@ -21,34 +26,16 @@ function test_which_name(tc)
2126
end
2227

2328

24-
function test_which_fullpath(tc)
25-
import matlab.unittest.constraints.IsFile
26-
import matlab.unittest.constraints.EndsWithSubstring
27-
28-
%% is_exe test
29-
p = matlabroot + "/bin/" + matlab_name();
30-
31-
tc.assumeTrue(stdlib.is_exe(p), "Matlab not executable " + p)
32-
%% which: test absolute path
33-
exe = stdlib.which(p);
34-
35-
tc.verifyNotEmpty(exe, "Matlab not found " + p)
36-
37-
if ispc
38-
tc.verifyThat(exe, EndsWithSubstring(".exe"))
39-
else
40-
tc.verifyThat(exe, ~EndsWithSubstring(".exe"))
29+
function test_which_absolute(tc, mexe)
30+
tc.verifyEqual(stdlib.which(mexe), mexe)
4131
end
42-
tc.verifyThat(exe, IsFile)
4332

44-
end
4533

4634
function test_which_multipath(tc)
4735

4836
n = matlab_name();
4937

50-
paths = string(getenv("PATH"));
51-
paths = split(paths, pathsep);
38+
paths = split(string(getenv('PATH')), pathsep);
5239
paths(end+1) = matlabroot + "/bin";
5340

5441
exe = stdlib.which(n, paths);
@@ -64,8 +51,8 @@ function test_which_multipath(tc)
6451

6552
function n = matlab_name()
6653

67-
n = "matlab";
54+
n = 'matlab';
6855
if ispc
69-
n = n + ".exe";
56+
n = strcat(n, '.exe');
7057
end
7158
end

0 commit comments

Comments
 (0)