Skip to content

Commit 4aa6f14

Browse files
committed
add is_file(), is_folder()
1 parent 8f2373d commit 4aa6f14

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

+stdlib/exists.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@
1111

1212
function y = exists(fpath)
1313

14-
try
15-
y = isfile(fpath) || isfolder(fpath);
16-
catch e
17-
if strcmp(e.identifier, 'MATLAB:UndefinedFunction')
18-
y = exist(fpath,'file') == 2 || exist(fpath, 'dir') == 7;
19-
else
20-
rethrow(e)
21-
end
22-
end
14+
y = stdlib.is_file(fpath) || stdlib.is_folder(fpath);
2315

2416
end
2517

26-
%!assert (stdlib.exists('.'))
18+
%!assert (stdlib.exists('.'))

+stdlib/is_file.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%% IS_FILE Determine if path is a file
2+
%
3+
% for Matlab < R2017b that doesn't have isfile(), note that exist() also looks
4+
% on the Matlab path.
5+
6+
function y = is_file(fpath)
7+
8+
try
9+
y = isfile(fpath);
10+
catch e
11+
if strcmp(e.identifier, 'MATLAB:UndefinedFunction')
12+
y = exist(fpath,'file') == 2;
13+
else
14+
rethrow(e)
15+
end
16+
end
17+
18+
end

+stdlib/is_folder.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%% IS_FOLDER Determine if path is a folder
2+
%
3+
% for Matlab < R2017b that doesn't have isfolder(), note that exist() also looks
4+
% on the Matlab path.
5+
6+
function y = is_folder(fpath)
7+
8+
try
9+
y = isfolder(fpath);
10+
catch e
11+
if strcmp(e.identifier, 'MATLAB:UndefinedFunction')
12+
y = exist(fpath, 'dir') == 7;
13+
else
14+
rethrow(e)
15+
end
16+
end
17+
18+
end

test/TestDisk.m

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2017b'}) ...
2+
TestTags = {'R2017a'}) ...
33
TestDisk < matlab.unittest.TestCase
44

55
properties
66
CI = is_ci()
77
end
88

99
properties (TestParameter)
10-
Ps = {".", "", "/", getenv('SystemDrive'), "not-exist"}
11-
Po = {mfilename("fullpath") + ".m", pwd(), ".", "", "not-exist"}
10+
Ps = {'.', '', '/', pwd(), getenv('SystemDrive'), 'not-exist'}
1211
B_ps = {'python', 'sys'}
1312
B_jps = {'java', 'python', 'sys'}
1413
B_jdps = {'java', 'dotnet', 'python', 'sys'}
@@ -147,13 +146,13 @@ function test_remove_file(tc)
147146
end
148147

149148

150-
function test_device(tc, Po, B_jps)
151-
[i, b] = stdlib.device(Po, B_jps);
149+
function test_device(tc, Ps, B_jps)
150+
[i, b] = stdlib.device(Ps, B_jps);
152151
tc.verifyClass(i, 'uint64')
153152
tc.assertEqual(char(b), B_jps)
154153

155154
if ismember(B_jps, stdlib.Backend().select('device'))
156-
if ~stdlib.exists(Po)
155+
if ~stdlib.exists(Ps)
157156
tc.verifyEmpty(i)
158157
else
159158
tc.assertNotEmpty(i)
@@ -165,14 +164,14 @@ function test_device(tc, Po, B_jps)
165164
end
166165

167166

168-
function test_inode(tc, Po, B_jps)
167+
function test_inode(tc, Ps, B_jps)
169168

170-
[i, b] = stdlib.inode(Po, B_jps);
169+
[i, b] = stdlib.inode(Ps, B_jps);
171170
tc.verifyClass(i, 'uint64')
172171
tc.assertEqual(char(b), B_jps)
173172

174173
if ismember(B_jps, stdlib.Backend().select('inode'))
175-
if ~stdlib.exists(Po)
174+
if ~stdlib.exists(Ps)
176175
tc.verifyEmpty(i)
177176
else
178177
tc.assertNotEmpty(i)
@@ -185,13 +184,13 @@ function test_inode(tc, Po, B_jps)
185184
end
186185

187186

188-
function test_owner(tc, Po, B_jdps)
189-
[o, b] = stdlib.get_owner(Po, B_jdps);
187+
function test_owner(tc, Ps, B_jdps)
188+
[o, b] = stdlib.get_owner(Ps, B_jdps);
190189
tc.assertEqual(char(b), B_jdps)
191190
tc.verifyClass(o, 'char')
192191

193192
if ismember(B_jdps, stdlib.Backend().select('get_owner'))
194-
if ~stdlib.exists(Po)
193+
if ~stdlib.exists(Ps)
195194
tc.verifyEqual(o, '')
196195
else
197196
tc.verifyGreaterThan(strlength(o), 0)

test/TestPlatform.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function test_all_toolboxes(tc)
1616
end
1717

1818

19-
methods (Test, TestTags={'R2017b', 'toolbox'})
19+
methods (Test, TestTags={'R2017a', 'toolbox'})
2020

2121
function test_has_parallel_toolbox(tc)
2222
y = stdlib.has_parallel_toolbox();
@@ -54,7 +54,7 @@ function test_perl(tc)
5454
end
5555

5656

57-
methods (Test, TestTags={'R2017b'})
57+
methods (Test, TestTags={'R2017a'})
5858

5959
function test_platform_tell(tc)
6060
r = stdlib.platform_tell();

0 commit comments

Comments
 (0)