From b1bb4852c60f456339963d4a766d93b4b8dc1f78 Mon Sep 17 00:00:00 2001 From: scivision Date: Mon, 9 Dec 2024 13:38:27 -0500 Subject: [PATCH] note ambiguity in .. components is_subdir, proximate_to, relative_to avoid testing these functions with .. components --- +stdlib/proximate_to.m | 7 +++---- +stdlib/relative_to.m | 15 +++++++-------- +stdlib/with_suffix.m | 20 +++++++++++--------- test/TestFilePure.m | 11 ++++++----- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/+stdlib/proximate_to.m b/+stdlib/proximate_to.m index 7b7984f..4633ddb 100644 --- a/+stdlib/proximate_to.m +++ b/+stdlib/proximate_to.m @@ -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 diff --git a/+stdlib/relative_to.m b/+stdlib/relative_to.m index a9afb25..dc6a359 100644 --- a/+stdlib/relative_to.m +++ b/+stdlib/relative_to.m @@ -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 @@ -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"), "") diff --git a/+stdlib/with_suffix.m b/+stdlib/with_suffix.m index 64856cd..9c752c9 100644 --- a/+stdlib/with_suffix.m +++ b/+stdlib/with_suffix.m @@ -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") diff --git a/test/TestFilePure.m b/test/TestFilePure.m index 0e41d3b..3014d36 100644 --- a/test/TestFilePure.m +++ b/test/TestFilePure.m @@ -82,7 +82,6 @@ {"Hello", "Hello/", "."}, ... {"./this/one", "./this/two", "../two"}, ... {"/path/same", "/path/same/hi/..", "hi/.."}, ... -{"a/b/..", "a/b", ".."}, ... {"", "/", ""}, ... {"/", "", ""}, ... {"/", "/", "."}, ... @@ -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++ , etc. p_proximate_to = p_relative_to; @@ -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++ , etc p_proximate_to{6}{3} = "/"; p_proximate_to{10}{3} = "c"; @@ -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}; @@ -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