Skip to content

Commit fb29f9f

Browse files
committed
which:windows: auto-append .exe
1 parent 529d589 commit fb29f9f

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

+stdlib/which.m

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

10-
function exe = which(filename, fpath, find_all)
10+
function exe = which(cmd, fpath, find_all)
1111
arguments
12-
filename {mustBeTextScalar}
12+
cmd {mustBeTextScalar}
1313
fpath (1,:) string = string.empty
1414
find_all (1,1) logical = false
1515
end
@@ -20,13 +20,21 @@
2020
exe = '';
2121
end
2222

23-
if isfile(filename) && stdlib.is_exe(filename)
24-
exe = filename;
23+
%% on Windows, append .exe if not suffix is given
24+
if ispc() && strempty(stdlib.suffix(cmd))
25+
pathext = '.exe';
26+
if ~endsWith(cmd, pathext, IgnoreCase=true)
27+
cmd = strcat(cmd, pathext);
28+
end
29+
end
30+
%% full filename was given
31+
if isfile(cmd) && stdlib.is_exe(cmd)
32+
exe = cmd;
2533
return
2634
end
2735

2836
% relative directory component, but path was not a file
29-
if ~strcmp(stdlib.filename(filename), filename)
37+
if ~strcmp(stdlib.filename(cmd), cmd)
3038
return
3139
end
3240

@@ -48,7 +56,7 @@
4856

4957
if strempty(p), continue, end
5058

51-
e = strcat(p, '/', filename);
59+
e = strcat(p, '/', cmd);
5260
if isfile(e) && stdlib.is_exe(e)
5361
if find_all
5462
exe(end+1) = e; %#ok<AGROW>

test/TestIsExe.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ function test_is_exe(tc, p)
1010
end
1111

1212
function test_matlabroot(tc)
13-
n = "matlab";
1413

15-
f = matlabroot + "/bin/" + n;
16-
if ispc
14+
f = fullfile(matlabroot, "bin", "matlab");
15+
if ispc()
1716
f = f + ".exe";
1817
end
1918

test/TestWhich.m

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

33
properties (TestParameter)
4-
mexe = {matlabroot + "/bin/" + matlab_name(), ...
5-
fullfile(matlabroot, 'bin', matlab_name())}
4+
mexe = {matlabroot + "/bin/matlab", ...
5+
fullfile(matlabroot, 'bin', 'matlab')}
66
end
77

88
methods (Test, TestTags="impure")
@@ -12,16 +12,21 @@ function test_which_name(tc)
1212
tc.verifyEmpty(stdlib.which(tempname()))
1313

1414
if ispc
15-
n = 'pwsh.exe';
15+
names = ["pwsh", "pwsh.exe"];
1616
else
17-
n = 'ls';
17+
names = "ls";
1818
end
1919
%% which: Matlab in environment variable PATH
2020
% MacOS Matlab does not source .zshrc so Matlab is not on internal Matlab PATH
2121
% Unix-like OS may have Matlab as alias which is not visible to
2222
% stdlib.which()
2323
% virus scanners may block stdlib.which("cmd.exe") on Windows
24-
tc.verifyNotEmpty(stdlib.which(n))
24+
for n = names
25+
exe = stdlib.which(n);
26+
tc.verifyNotEmpty(exe, "Executable not found: " + n)
27+
tc.verifyTrue(isfile(exe), "Executable is not a file: " + n)
28+
tc.verifyTrue(stdlib.is_exe(exe), "Executable is not executable: " + n)
29+
end
2530

2631
end
2732

@@ -31,28 +36,23 @@ function test_which_absolute(tc, mexe)
3136
end
3237

3338

34-
function test_which_multipath(tc)
39+
function test_which_onepath(tc)
3540

36-
n = matlab_name();
41+
tc.verifyNotEmpty(stdlib.which("matlab", fullfile(matlabroot, 'bin')), ...
42+
"Matlab not found by which() given specific path=")
3743

38-
paths = split(string(getenv('PATH')), pathsep);
39-
paths(end+1) = matlabroot + "/bin";
44+
end
4045

41-
exe = stdlib.which(n, paths);
4246

43-
tc.verifyNotEmpty(exe, "Matlab not found by which()")
47+
function test_which_multipath(tc)
4448

45-
end
49+
paths = split(string(getenv('PATH')), pathsep);
50+
paths(end+1) = fullfile(matlabroot, 'bin');
4651

47-
end
52+
tc.verifyNotEmpty(stdlib.which("matlab", paths), "Matlab not found by which()")
4853

4954
end
5055

51-
52-
function n = matlab_name()
53-
54-
n = 'matlab';
55-
if ispc
56-
n = strcat(n, '.exe');
5756
end
57+
5858
end

0 commit comments

Comments
 (0)