Skip to content

Commit 8f2373d

Browse files
committed
exists: compatibility but keep speed
1 parent 3288e25 commit 8f2373d

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

+stdlib/exists.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111

1212
function y = exists(fpath)
1313

14-
y = isfile(fpath) || isfolder(fpath);
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
1523

1624
end
1725

test/TestExists.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test_dirs(tc)
1414
end
1515

1616

17-
methods (Test, TestTags={'R2017b'})
17+
methods (Test, TestTags={'R2017a'})
1818

1919
function test_exists(tc, Ps)
2020
r = stdlib.exists(Ps{1});

test/TestFileImpure.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function test_makedir(tc)
4343
d = 'test_makedir.dir';
4444
stdlib.makedir(d)
4545

46-
if stdlib.matlabOlderThan('R2018a')
46+
if stdlib.matlabOlderThan('R2017b')
47+
tc.assertTrue(exist(d, 'dir') == 7)
48+
elseif stdlib.matlabOlderThan('R2018a')
4749
tc.assertTrue(isfolder(d))
4850
else
4951
tc.verifyThat(d, matlab.unittest.constraints.IsFolder)

test/TestFilePure.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2017b', 'pure'}) ...
2+
TestTags = {'R2017a', 'pure'}) ...
33
TestFilePure < matlab.unittest.TestCase
44

55

test/TestFilename.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2017b', 'pure'}) ...
2+
TestTags = {'R2017a', 'pure'}) ...
33
TestFilename < matlab.unittest.TestCase
44

55
properties (TestParameter)

test/TestIsExe.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2017b', 'pure'}) ...
2+
TestTags = {'R2017a', 'pure'}) ...
33
TestIsExe < matlab.unittest.TestCase
44

55
properties (TestParameter)

test/TestWhich.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
end
99

1010

11-
methods (Test, TestTags = {'R2017b'})
11+
methods (Test, TestTags = {'R2017a'})
1212

1313
function test_which_name(tc)
1414

@@ -27,7 +27,6 @@ function test_which_name(tc)
2727
for n = names
2828
exe = stdlib.which(n);
2929
tc.verifyNotEmpty(exe, "Executable not found: " + n)
30-
tc.verifyTrue(isfile(exe), "Executable is not a file: " + n)
3130
tc.verifyTrue(stdlib.is_exe(exe), "Executable is not executable: " + n)
3231
end
3332

@@ -41,7 +40,6 @@ function test_which_absolute(tc, mexe)
4140
r = [r, '.exe'];
4241
end
4342

44-
tc.assumeTrue(isfile(r))
4543
tc.assumeTrue(stdlib.is_exe(r))
4644

4745
tc.verifyGreaterThan(strlength(stdlib.which(r)), 0, "Expected which(" + r + " ) to find " + r)
@@ -50,7 +48,7 @@ function test_which_absolute(tc, mexe)
5048

5149

5250
function test_which_onepath(tc)
53-
tc.verifyNotEmpty(stdlib.which("matlab", fullfile(matlabroot, 'bin')), ...
51+
tc.verifyNotEmpty(stdlib.which('matlab', fullfile(matlabroot, 'bin')), ...
5452
"Matlab not found by which() given specific path=")
5553
end
5654

@@ -59,7 +57,7 @@ function test_which_multipath(tc)
5957
paths = split(string(getenv('PATH')), pathsep);
6058
paths(end+1) = fullfile(matlabroot, 'bin');
6159

62-
tc.verifyNotEmpty(stdlib.which("matlab", paths), "Matlab not found by which()")
60+
tc.verifyNotEmpty(stdlib.which('matlab', paths), "Matlab not found by which()")
6361
end
6462

6563
end

0 commit comments

Comments
 (0)