Skip to content

Commit b12683a

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

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-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: 26 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,47 +12,54 @@ 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

2833

2934
function test_which_absolute(tc, mexe)
30-
tc.verifyEqual(stdlib.which(mexe), mexe)
31-
end
3235

36+
r = mexe;
37+
if ispc()
38+
r = strcat(r, '.exe');
39+
end
3340

34-
function test_which_multipath(tc)
41+
tc.verifyEqual(stdlib.which(mexe), r)
3542

36-
n = matlab_name();
43+
end
3744

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

41-
exe = stdlib.which(n, paths);
46+
function test_which_onepath(tc)
4247

43-
tc.verifyNotEmpty(exe, "Matlab not found by which()")
48+
tc.verifyNotEmpty(stdlib.which("matlab", fullfile(matlabroot, 'bin')), ...
49+
"Matlab not found by which() given specific path=")
4450

4551
end
4652

47-
end
4853

49-
end
54+
function test_which_multipath(tc)
5055

56+
paths = split(string(getenv('PATH')), pathsep);
57+
paths(end+1) = fullfile(matlabroot, 'bin');
5158

52-
function n = matlab_name()
59+
tc.verifyNotEmpty(stdlib.which("matlab", paths), "Matlab not found by which()")
5360

54-
n = 'matlab';
55-
if ispc
56-
n = strcat(n, '.exe');
5761
end
62+
63+
end
64+
5865
end

0 commit comments

Comments
 (0)