Skip to content

Commit

Permalink
samepath, fix normalize, canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed May 17, 2024
1 parent a280712 commit b9c30a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 2 additions & 0 deletions +stdlib/+fileio/normalize.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

n = stdlib.fileio.posix(File(expanduser(p)).toPath().normalize());

if(strlength(n) == 0), n = "."; end

end
19 changes: 10 additions & 9 deletions +stdlib/+fileio/samepath.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

import java.io.File
import java.nio.file.Files
import stdlib.fileio.canonical

issame = false;
% java.nio.file.Files needs CANONICAL -- not just absolute path
p1 = canonical(path1);
if ~stdlib.fileio.exists(p1), return, end
p1 = File(p1).toPath();
if ~stdlib.fileio.exists(path1) || ~stdlib.fileio.exists(path2)
return
end

% not correct without canoncial(). Normalize() doesn't help.
path1 = stdlib.fileio.canonical(path1);
path2 = stdlib.fileio.canonical(path2);

p2 = canonical(path2);
if ~stdlib.fileio.exists(p2), return, end
p2 = File(p2).toPath();
issame = Files.isSameFile(File(path1).toPath(), File(path2).toPath());

issame = Files.isSameFile(p1, p2);
% alternative, lower-level method is lexical only (not suitable for us):
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#equals(java.lang.Object)

end
10 changes: 5 additions & 5 deletions test/TestFileImpure.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
ref_is_write = {true, false}
in_expand = {"", "~abc", "~", "~/foo"}
ref_expand
in_same = {"", tempname, "~/b/..", "."}
other_same = {"", tempname, "~/c/..", fullfile(pwd, "a/..")}
in_same = {"", tempname, "..", ".."}
other_same = {"", tempname, "./..", fullfile(pwd, "..")}
ref_same = {false, false, true, true}
end

Expand Down Expand Up @@ -124,7 +124,7 @@ function test_canonical(tc)

% all non-existing files

tc.verifyEqual(stdlib.canonical(""), "")
tc.verifyEqual(stdlib.canonical(""), ".")

pabs = stdlib.canonical('2foo');
tc.verifyThat(pabs, StartsWithSubstring("2foo"))
Expand Down Expand Up @@ -207,12 +207,12 @@ function test_makedir(tc)
tc.assertTrue(isfolder(d))
end


%%
function test_samepath(tc, in_same, other_same, ref_same)
tc.verifyEqual(stdlib.samepath(in_same, other_same), ref_same)
end


%%
function test_which_name(tc)

tc.verifyEmpty(stdlib.which(tempname))
Expand Down
2 changes: 1 addition & 1 deletion test/TestFilePure.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function test_absolute_path(tc)

function test_normalize(tc)

tc.verifyEqual(stdlib.normalize(""), "")
tc.verifyEqual(stdlib.normalize(""), ".")

pabs = stdlib.normalize('2foo//');
tc.verifyEqual(pabs, "2foo")
Expand Down

0 comments on commit b9c30a2

Please sign in to comment.