Skip to content

Commit ce9082f

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

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

+stdlib/is_exe.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@
1919
p = p(:);
2020
i = isfile(p);
2121
ok(size(p)) = false;
22+
23+
if ispc()
24+
pe = split(string(getenv("PATHEXT")), pathsep);
25+
i = i & endsWith(stdlib.suffix(p(i)), pe, 'IgnoreCase', true);
26+
end
27+
2228
if ~any(i), return, end
2329

2430
t(i, :) = getPermissions(filePermissions(p(i)), props);
2531

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

2834
catch e
35+
ok = false;
36+
if ~isfile(p), return, end
37+
2938
switch e.identifier
3039
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
3140
a = file_attributes_legacy(p);
32-
ok = isfile(p) && (a.UserExecute || a.GroupExecute || a.OtherExecute);
41+
ok = a.UserExecute || a.GroupExecute || a.OtherExecute;
3342
otherwise, rethrow(e)
3443
end
3544
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)