Skip to content

Commit 233620a

Browse files
committed
add proximate_to, is_subdir
1 parent 33f821e commit 233620a

File tree

8 files changed

+196
-101
lines changed

8 files changed

+196
-101
lines changed

+stdlib/+fileio/is_subdir.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function yes = is_subdir(subdir, dir)
2+
arguments
3+
subdir (1,1) string
4+
dir (1,1) string
5+
end
6+
7+
r = stdlib.fileio.relative_to(dir, subdir);
8+
9+
yes = strlength(r) > 0 && r ~= "." && ~startsWith(r, "..");
10+
11+
end

+stdlib/+fileio/proximate_to.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function r = proximate_to(base, other)
2+
arguments
3+
base (1,1) string
4+
other (1,1) string
5+
end
6+
7+
r = stdlib.fileio.relative_to(base, other);
8+
if strlength(r) > 0
9+
return;
10+
end
11+
12+
r = other;
13+
14+
end

+stdlib/+fileio/relative_to.m

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@
88
base = stdlib.fileio.normalize(base);
99
other = stdlib.fileio.normalize(other);
1010

11-
if base == "" || other == ""
12-
r = "";
13-
elseif base == other
11+
if base == other
1412
r = ".";
1513
else
16-
b = java.io.File(base).toPath();
17-
o = java.io.File(other).toPath();
18-
try
19-
r = stdlib.fileio.posix(b.relativize(o));
20-
catch e
21-
if contains(e.message, 'java.lang.IllegalArgumentException')
22-
r = "";
23-
else
24-
rethrow(e);
25-
end
14+
b = java.io.File(base).toPath();
15+
o = java.io.File(other).toPath();
16+
try
17+
r = stdlib.fileio.posix(b.relativize(o));
18+
catch e
19+
if contains(e.message, 'java.lang.IllegalArgumentException')
20+
r = "";
21+
else
22+
rethrow(e);
2623
end
24+
end
2725
end
2826

2927
end

+stdlib/is_subdir.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function yes = is_subdir(subdir, dir)
2+
arguments
3+
subdir (1,1) string
4+
dir (1,1) string
5+
end
6+
7+
yes = stdlib.fileio.is_subdir(subdir, dir);
8+
9+
end

+stdlib/proximate_to.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function r = proximate_to(base, other)
2+
arguments
3+
base (1,1) string
4+
other (1,1) string
5+
end
6+
7+
r = stdlib.fileio.proximate_to(base, other);
8+
9+
end

+stdlib/relative_to.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
other (1,1) string
55
end
66

7-
if base == "" && other == ""
8-
r = "."; % like C++ std::filesystem::relative
9-
else
10-
r = stdlib.fileio.relative_to(base, other);
11-
end
7+
r = stdlib.fileio.relative_to(base, other);
128

139
end

test/TestFileImpure.m

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,88 @@
11
classdef TestFileImpure < matlab.unittest.TestCase
22

3-
methods(TestClassSetup)
4-
5-
function setup_path(tc)
6-
import matlab.unittest.fixtures.PathFixture
7-
cwd = fileparts(mfilename("fullpath"));
8-
top = fullfile(cwd, "..");
9-
tc.applyFixture(PathFixture(top))
3+
properties (ClassSetupParameter)
4+
classToTest = {"TestFileImpure"};
5+
end
106

7+
properties(TestParameter)
8+
in_exists = {pwd, string(mfilename("fullpath"))+".m", "not-exists"}
9+
ref_exists = {true, true, false}
10+
% on CI matlabroot can be writable!
11+
in_is_write = {pwd, "not-exists"};
12+
ref_is_write = {true, false}
13+
in_expand = {"", "~abc", "~", "~/foo"}
14+
ref_expand
15+
in_same = {"", tempname, "~/b/..", "."}
16+
other_same = {"", tempname, "~/c/..", fullfile(pwd, "a/..")}
17+
ref_same = {false, false, true, true}
1118
end
1219

20+
properties
21+
tobj
1322
end
1423

15-
methods (Test)
1624

17-
function test_exists(tc)
25+
methods (TestParameterDefinition, Static)
1826

19-
tc.verifyTrue(stdlib.exists(pwd))
20-
tc.verifyTrue(stdlib.exists(string(mfilename("fullpath"))+".m"))
21-
tc.verifyFalse(stdlib.exists("not-exists"))
27+
function ref_expand = init_expand(classToTest) %#ok<INUSD>
28+
cwd = fileparts(mfilename("fullpath"));
29+
top = fullfile(cwd, "..");
30+
addpath(top)
2231

32+
ref_expand = {"", "~abc", stdlib.fileio.homedir, stdlib.join(stdlib.fileio.homedir, "foo")};
33+
end
2334
end
2435

2536

26-
function test_is_readable(tc)
27-
28-
tc.verifyTrue(stdlib.is_readable(pwd))
29-
tc.verifyTrue(stdlib.is_readable(string(mfilename("fullpath"))+".m"))
30-
tc.verifyFalse(stdlib.is_readable("not-exists"))
37+
methods(TestClassSetup)
3138

39+
function classSetup(tc, classToTest)
40+
constructor = str2func(classToTest);
41+
tc.tobj = constructor();
3242
end
3343

34-
35-
function test_is_writable(tc)
36-
37-
tc.verifyTrue(stdlib.is_writable(pwd))
38-
% tc.verifyFalse(stdlib.is_writable(matlabroot)) % on CI this can be writable!
39-
tc.verifyFalse(stdlib.is_writable("not-exists"))
44+
function setup_path(tc)
45+
import matlab.unittest.fixtures.PathFixture
46+
cwd = fileparts(mfilename("fullpath"));
47+
top = fullfile(cwd, "..");
48+
tc.applyFixture(PathFixture(top))
49+
end
4050

4151
end
4252

53+
methods (Test, ParameterCombination = 'sequential')
4354

55+
function test_exists(tc, in_exists, ref_exists)
56+
tc.verifyEqual(stdlib.exists(in_exists), ref_exists)
57+
end
4458

45-
function test_expanduser(tc)
4659

47-
tc.verifyEqual(stdlib.expanduser(""), "")
60+
function test_is_readable(tc, in_exists, ref_exists)
61+
tc.verifyEqual(stdlib.is_readable(in_exists), ref_exists)
62+
end
4863

49-
tc.verifyEqual(stdlib.expanduser("~abc"), "~abc")
5064

51-
h = stdlib.fileio.homedir();
52-
tc.verifyEqual(stdlib.expanduser("~"), h)
65+
function test_is_writable(tc, in_is_write, ref_is_write)
66+
tc.verifyEqual(stdlib.is_writable(in_is_write), ref_is_write)
67+
end
5368

54-
e = stdlib.expanduser("~/foo");
55-
tc.verifyEqual(e, stdlib.fileio.join(h, "foo"))
5669

70+
function test_expanduser(tc, in_expand, ref_expand)
71+
tc.verifyEqual(stdlib.expanduser(in_expand), ref_expand)
5772
end
5873

5974

6075
function test_touch_modtime(tc)
6176

6277
fn = tempname;
6378
tc.verifyTrue(stdlib.touch(fn))
64-
6579
t0 = stdlib.get_modtime(fn);
66-
pause(1.1)
80+
81+
pause(0.4) % empirical to avoid failing >=
6782
tc.verifyTrue(stdlib.set_modtime(fn))
6883
t1 = stdlib.get_modtime(fn);
6984

70-
tc.verifyGreaterThan(t1, t0)
85+
tc.verifyGreaterThanOrEqual(t1, t0)
7186

7287
end
7388

@@ -115,7 +130,6 @@ function test_canonical(tc)
115130
tc.verifyThat(pabs, StartsWithSubstring("2foo"))
116131

117132
par1 = stdlib.canonical("../2foo");
118-
tc.verifyNotEmpty(par1)
119133
tc.verifyThat(par1, StartsWithSubstring(".."))
120134

121135
pt1 = stdlib.canonical("bar/../2foo");
@@ -194,12 +208,8 @@ function test_makedir(tc)
194208
end
195209

196210

197-
function test_samepath(tc)
198-
199-
tc.verifyFalse(stdlib.samepath("", ""), "empty not same")
200-
tc.verifyFalse(stdlib.samepath(tempname, tempname), "tempname not same")
201-
tc.verifyTrue(stdlib.samepath("~/b/..", "~/c/.."), "tilde path ..")
202-
tc.verifyTrue(stdlib.samepath(".", fullfile(pwd, "a/..")), "dot path ..")
211+
function test_samepath(tc, in_same, other_same, ref_same)
212+
tc.verifyEqual(stdlib.samepath(in_same, other_same), ref_same)
203213
end
204214

205215

0 commit comments

Comments
 (0)