Skip to content

Commit 089ba5e

Browse files
committed
is_exe: file or directory can be executable
1 parent 5094606 commit 089ba5e

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

+stdlib/is_exe.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
end
1111

1212

13-
ok = false;
14-
15-
if ~isfile(p)
16-
return
17-
end
18-
1913
if use_java
2014
% about the same time as fileattrib
2115
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#canExecute()
@@ -26,6 +20,11 @@
2620

2721
else
2822

23+
if ~stdlib.len(p)
24+
ok = false;
25+
return
26+
end
27+
2928
[status, v] = fileattrib(p);
3029

3130
ok = status ~= 0 && (v.UserExecute || (~isnan(v.GroupExecute) && v.GroupExecute) || (~isnan(v.OtherExecute) && v.OtherExecute));
@@ -36,4 +35,5 @@
3635

3736
%!assert (!is_exe(''))
3837
%!assert (!is_exe(tempname))
38+
%!assert (is_exe("."))
3939
%!assert (is_exe(program_invocation_name))

+stdlib/is_symlink.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
%% IS_SYMLINK is path a symbolic link
2-
%
2+
% on Windows, distinguishes between symlinks and App Execution Aliases,
3+
% which are not symlinks.
34

45
function ok = is_symlink(p)
56
arguments
67
p (1,1) string
78
end
89

10+
911
try
1012
ok = isSymbolicLink(p);
1113
catch e

+stdlib/which.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
exe = string.empty;
1616

17-
if stdlib.is_exe(filename, use_java)
17+
if isfile(filename) && stdlib.is_exe(filename, use_java)
1818
exe = stdlib.posix(filename);
1919
return
2020
end
@@ -34,7 +34,7 @@
3434

3535
for p = fpath
3636
e = p + "/" + filename;
37-
if stdlib.is_exe(e, use_java)
37+
if isfile(e) && stdlib.is_exe(e, use_java)
3838
exe = stdlib.posix(e);
3939
return
4040
end

test/TestIsExe.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classdef TestIsExe < matlab.unittest.TestCase
2+
3+
properties (TestParameter)
4+
p = init_is_exe()
5+
use_java = num2cell(unique([stdlib.has_java(), false]))
6+
end
7+
8+
methods(Test)
9+
function test_is_exe(tc, p, use_java)
10+
tc.verifyEqual(stdlib.is_exe(p{1}, use_java), p{2})
11+
end
12+
end
13+
end
14+
15+
16+
function p = init_is_exe()
17+
18+
n = "matlab";
19+
20+
f = matlabroot + "/bin/" + n;
21+
if ispc
22+
f = f + ".exe";
23+
end
24+
25+
26+
p = {{"", false}, {tempname, false}, {".", true}, {f, true}};
27+
end

test/TestWhich.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ function test_which_name(tc, use_java)
2626
end
2727

2828

29-
function test_is_exe_which_fullpath(tc, use_java)
29+
function test_which_fullpath(tc, use_java)
3030
import matlab.unittest.constraints.IsFile
3131
import matlab.unittest.constraints.EndsWithSubstring
3232

33-
tc.verifyFalse(stdlib.is_exe("", use_java))
34-
tc.verifyFalse(stdlib.is_exe(tempname, use_java))
35-
3633
n = "matlab";
3734
%% is_exe test
3835
p = matlabroot + "/bin/" + n;
3936
if ispc
4037
p = p + ".exe";
4138
end
42-
tc.verifyTrue(stdlib.is_exe(p, use_java))
39+
tc.assumeTrue(stdlib.is_exe(p, use_java), "Matlab not executable " + p)
4340
%% which: test absolute path
44-
exe = stdlib.which(p, getenv('PATH'), use_java);
41+
exe = stdlib.which(p, [], use_java);
4542

4643
if ispc
4744
tc.verifyThat(exe, EndsWithSubstring(".exe"))

0 commit comments

Comments
 (0)