Skip to content

Commit 3e33887

Browse files
committed
make is_exe own file
1 parent 6011d7d commit 3e33887

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

+stdlib/+fileio/TestFileio.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ function test_makedir(tc)
7878

7979
function test_which(tc)
8080
import stdlib.fileio.which
81+
import stdlib.fileio.is_exe
8182

8283
n = "matlab";
83-
% assumes Matlab in environment variable PATH
84-
tc.assumeNotEmpty(which(n))
85-
84+
%% which: Matlab in environment variable PATH
85+
tc.verifyNotEmpty(which(n))
86+
%% is_exe test
8687
p = fullfile(matlabroot, "bin", n);
87-
88-
% full absolute path
88+
if ispc
89+
fp = p + ".exe";
90+
else
91+
fp = p;
92+
end
93+
tc.verifyTrue(is_exe(fp))
94+
%% which: test absolute path
8995
exe = which(p);
9096

9197
if ispc

+stdlib/+fileio/is_exe.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function ok = is_exe(file)
2+
%% ok = is_exe(file)
3+
% is a file executable?
4+
5+
arguments
6+
file (1,1) string
7+
end
8+
9+
if ~isfile(file)
10+
ok = false;
11+
return
12+
end
13+
14+
[ok1, stat] = fileattrib(file);
15+
ok = ok1 == 1 && (stat.UserExecute == 1 || stat.GroupExecute == 1);
16+
17+
end

+stdlib/+fileio/which.m

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
subdir (1,:) string {mustBeNonempty} = ""
99
end
1010

11+
import stdlib.fileio.is_exe
12+
1113
exe = string.empty;
1214

1315
if ispc
@@ -20,7 +22,7 @@
2022
if any(strlength(fileparts(name)) > 0)
2123
% has directory part
2224
for n = name
23-
if check_exe(n)
25+
if is_exe(n)
2426
exe = n;
2527
break
2628
end
@@ -40,7 +42,7 @@
4042
for s = subdir
4143
for n = name
4244
e = fullfile(p, s, n);
43-
if check_exe(e)
45+
if is_exe(e)
4446
exe = e;
4547
return
4648
end
@@ -50,14 +52,3 @@
5052
end
5153

5254
end
53-
54-
55-
function ok = check_exe(exe)
56-
arguments
57-
exe (1,1) string
58-
end
59-
60-
[ok1, stat] = fileattrib(exe);
61-
ok = ok1 == 1 && (stat.UserExecute == 1 || stat.GroupExecute == 1);
62-
63-
end

0 commit comments

Comments
 (0)