Skip to content

Commit 591091c

Browse files
committed
is_exe: more robust, esp. on Windows
Use PATHEXT to confirm .exe status
1 parent a0b56a4 commit 591091c

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

+stdlib/is_exe.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
end
99
% need to have string array type for p(:)
1010

11+
p = p(:);
12+
13+
ok(size(p)) = false;
14+
15+
i = isfile(p);
16+
17+
if ispc()
18+
pe = split(string(getenv("PATHEXT")), pathsep);
19+
i = i & endsWith(stdlib.suffix(p(i)), pe, 'IgnoreCase', true);
20+
end
21+
22+
if ~any(i), return, end
23+
24+
1125
try
1226

1327
if isunix
@@ -16,20 +30,16 @@
1630
props = "Readable";
1731
end
1832

19-
p = p(:);
20-
i = isfile(p);
21-
ok(size(p)) = false;
22-
if ~any(i), return, end
23-
2433
t(i, :) = getPermissions(filePermissions(p(i)), props);
2534

2635
ok(i) = isfile(p(i)) & any(t{i,:}, 2);
2736

2837
catch e
38+
2939
switch e.identifier
3040
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
3141
a = file_attributes_legacy(p);
32-
ok = isfile(p) && (a.UserExecute || a.GroupExecute || a.OtherExecute);
42+
ok = a.UserExecute || a.GroupExecute || a.OtherExecute;
3343
otherwise, rethrow(e)
3444
end
3545
end

test/TestIsExe.m

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

33
properties (TestParameter)
4-
p = {{".", false}}
4+
p = {{fileparts(mfilename('fullpath')) + "/../Readme.md", false}}
55
end
66

77
methods(Test, TestTags="impure")
8+
89
function test_is_exe(tc, p)
10+
tc.assumeThat(p{1}, matlab.unittest.constraints.IsFile)
11+
912
tc.verifyEqual(stdlib.is_exe(p{1}), p{2})
1013
end
1114

15+
16+
function test_is_exe_dir(tc)
17+
tc.verifyFalse(stdlib.is_exe('.'))
18+
end
19+
20+
1221
function test_matlab_exe(tc)
1322

1423
f = fullfile(matlabroot, "bin/matlab");
1524
if ispc()
1625
f = f + ".exe";
1726
end
1827

19-
tc.verifyEqual(stdlib.is_exe(f), true)
28+
tc.verifyTrue(stdlib.is_exe(f))
2029
end
2130

2231
end

0 commit comments

Comments
 (0)