Skip to content

Commit bc35791

Browse files
committed
which: more thorough test and correctness
1 parent d514b38 commit bc35791

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

+stdlib/which.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
end
3434

3535
% path given
36-
if isempty(fpath)
36+
if stdlib.strempty(fpath)
3737
fpath = string(getenv("PATH"));
3838
end
3939

@@ -44,7 +44,11 @@
4444
for p = fpath
4545
if stdlib.strempty(p), continue, end
4646

47-
e = fullfile(p, cmd);
47+
if endsWith(p, "/" | filesep)
48+
e = p + cmd;
49+
else
50+
e = p + "/" + cmd;
51+
end
4852
if isfile(e) && stdlib.is_exe(e)
4953
if find_all
5054
exe(end+1) = e; %#ok<AGROW>

test/TestWhich.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture("..")}, ...
2-
TestTags = {'R2019b', 'impure'}) ...
2+
TestTags = {'impure'}) ...
33
TestWhich < matlab.unittest.TestCase
44

55
properties (TestParameter)
@@ -8,7 +8,7 @@
88
end
99

1010

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

1313
function test_which_name(tc)
1414

@@ -46,27 +46,34 @@ function test_which_absolute(tc, mexe)
4646

4747
tc.verifyGreaterThan(strlength(stdlib.which(r)), 0, "Expected which(" + r + " ) to find " + r)
4848
tc.verifyGreaterThan(strlength(stdlib.which(mexe)), 0, "Expected which(" + mexe + ") to find " + r)
49-
5049
end
5150

5251

5352
function test_which_onepath(tc)
54-
5553
tc.verifyNotEmpty(stdlib.which("matlab", fullfile(matlabroot, 'bin')), ...
5654
"Matlab not found by which() given specific path=")
57-
5855
end
5956

6057

6158
function test_which_multipath(tc)
62-
6359
paths = split(string(getenv('PATH')), pathsep);
6460
paths(end+1) = fullfile(matlabroot, 'bin');
6561

6662
tc.verifyNotEmpty(stdlib.which("matlab", paths), "Matlab not found by which()")
63+
end
6764

6865
end
6966

67+
68+
methods(Test, TestTags={'R2023a'})
69+
function testWhichNoPath(tc)
70+
tc.assumeFalse(isMATLABReleaseOlderThan('R2023a'))
71+
fx = matlab.unittest.fixtures.EnvironmentVariableFixture('PATH', '');
72+
tc.applyFixture(fx)
73+
74+
tc.verifyEmpty(stdlib.which('matlab'), "Matlab found by which() given empty path")
75+
tc.verifyNotEmpty(stdlib.which('matlab', matlabroot + "/bin"))
76+
end
7077
end
7178

7279
end

0 commit comments

Comments
 (0)