Skip to content

Commit

Permalink
note ambiguity in .. components is_subdir, proximate_to, relative_to
Browse files Browse the repository at this point in the history
avoid testing these functions with .. components
  • Loading branch information
scivision committed Dec 9, 2024
1 parent 2963737 commit b1bb485
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
7 changes: 3 additions & 4 deletions +stdlib/proximate_to.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
end

r = stdlib.relative_to(base, other);
if stdlib.len(r) > 0
return;
end

r = other;
if ~stdlib.len(r)
r = other;
end

end

Expand Down
15 changes: 7 additions & 8 deletions +stdlib/relative_to.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
end

% must remove trailing slashes
base = stdlib.drop_slash(base);
other = stdlib.drop_slash(other);
b1 = stdlib.drop_slash(base);
o1 = stdlib.drop_slash(other);

if strcmp(base, other)
if strcmp(b1, o1)
r = ".";
return
end

if stdlib.isoctave()
b = javaObject("java.io.File", base).toPath();
o = javaObject("java.io.File", other).toPath();
b = javaObject("java.io.File", b1).toPath();
o = javaObject("java.io.File", o1).toPath();
else
b = java.io.File(base).toPath();
o = java.io.File(other).toPath();
b = java.io.File(b1).toPath();
o = java.io.File(o1).toPath();
end

try
Expand All @@ -43,5 +43,4 @@
%!assert(relative_to("/a/b", "/a/b"), ".")
%!assert(relative_to("/a/b", "/a/b/c"), "c")
%!assert(relative_to("/a/b", "/a/b/c/"), "c")
%!assert(relative_to("a/b/..", "a/b"), "..")
%!assert(relative_to("/a/b", "d"), "")
20 changes: 11 additions & 9 deletions +stdlib/with_suffix.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@
s = stdlib.stem(p);

if stdlib.len(s) == 0
f = stdlib.join(p, suffix);
f = stdlib.join(p, suffix, false);
return
end

if r == '.'
f = s;
elseif stdlib.isoctave()
elseif ischar(r)
f = strcat(r, '/', s);
else
f = r + "/" + s;
end

f = f + suffix;

if ischar(r)
f = strcat(f, suffix);
else
f = f + suffix;
end

end

%!assert(with_suffix("", ""), "")
%!assert(with_suffix("a.h5", ".nc"), "a.nc")
%!assert(with_suffix("a", ".nc"), "a.nc")
%!assert(with_suffix("a.h5", ""), "a")
%!assert(with_suffix("a", ""), "a")
%!assert(with_suffix("ab.h5", ".nc"), "ab.nc")
%!assert(with_suffix("ab", ".nc"), "ab.nc")
%!assert(with_suffix("ab.h5", ""), "ab")
%!assert(with_suffix("ab", ""), "ab")
11 changes: 6 additions & 5 deletions test/TestFilePure.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
{"Hello", "Hello/", "."}, ...
{"./this/one", "./this/two", "../two"}, ...
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
{"a/b/..", "a/b", ".."}, ...
{"", "/", ""}, ...
{"/", "", ""}, ...
{"/", "/", "."}, ...
Expand All @@ -92,7 +91,8 @@
{"/a/b", "/a/b", "."}, ...
{"/a/b", "/a", ".."}, ...
{"/a/b/c/d", "/a/b", "../.."}, ...
{"/this/one", "/this/two", "../two"}};
{"this/one", "this/two", "../two"}};
% NOTE: ".." in relative_to is ambiguous including for python.pathlib, C++ <filesystem>, etc.

p_proximate_to = p_relative_to;

Expand All @@ -111,6 +111,7 @@
{"c:/path", "d:/path", ""}}];

p_proximate_to = p_relative_to;
% NOTE: ".." in proximate_to is ambiguous including for python.pathlib, C++ <filesystem>, etc

p_proximate_to{6}{3} = "/";
p_proximate_to{10}{3} = "c";
Expand Down Expand Up @@ -153,9 +154,9 @@
{"a/b", "a/b", false}, ...
{"a/b", "a/b/", false}, ...
{"a/b", "a", true}, ...
{"a/.c", "a", true}, ...
{"a/b", "a/b/..", false} % tricky one
{"a/.c", "a", true}
};
% NOTE: ".." in is_subidr is ambiguous

if ispc
p_is_subdir{end+1} = {"c:\", "c:/", false};
Expand Down Expand Up @@ -272,7 +273,7 @@ function test_relative_to(tc, p_relative_to)

function test_proximate_to(tc, p_proximate_to)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3})
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3}, "proximate_to(" + p_proximate_to{1} + "," + p_proximate_to{2}+")")
end


Expand Down

0 comments on commit b1bb485

Please sign in to comment.