Skip to content

Commit b9c30a2

Browse files
committed
samepath, fix normalize, canonical
1 parent a280712 commit b9c30a2

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

+stdlib/+fileio/normalize.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

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

13+
if(strlength(n) == 0), n = "."; end
14+
1315
end

+stdlib/+fileio/samepath.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

88
import java.io.File
99
import java.nio.file.Files
10-
import stdlib.fileio.canonical
1110

1211
issame = false;
13-
% java.nio.file.Files needs CANONICAL -- not just absolute path
14-
p1 = canonical(path1);
15-
if ~stdlib.fileio.exists(p1), return, end
16-
p1 = File(p1).toPath();
12+
if ~stdlib.fileio.exists(path1) || ~stdlib.fileio.exists(path2)
13+
return
14+
end
15+
16+
% not correct without canoncial(). Normalize() doesn't help.
17+
path1 = stdlib.fileio.canonical(path1);
18+
path2 = stdlib.fileio.canonical(path2);
1719

18-
p2 = canonical(path2);
19-
if ~stdlib.fileio.exists(p2), return, end
20-
p2 = File(p2).toPath();
20+
issame = Files.isSameFile(File(path1).toPath(), File(path2).toPath());
2121

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

2425
end

test/TestFileImpure.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
ref_is_write = {true, false}
1313
in_expand = {"", "~abc", "~", "~/foo"}
1414
ref_expand
15-
in_same = {"", tempname, "~/b/..", "."}
16-
other_same = {"", tempname, "~/c/..", fullfile(pwd, "a/..")}
15+
in_same = {"", tempname, "..", ".."}
16+
other_same = {"", tempname, "./..", fullfile(pwd, "..")}
1717
ref_same = {false, false, true, true}
1818
end
1919

@@ -124,7 +124,7 @@ function test_canonical(tc)
124124

125125
% all non-existing files
126126

127-
tc.verifyEqual(stdlib.canonical(""), "")
127+
tc.verifyEqual(stdlib.canonical(""), ".")
128128

129129
pabs = stdlib.canonical('2foo');
130130
tc.verifyThat(pabs, StartsWithSubstring("2foo"))
@@ -207,12 +207,12 @@ function test_makedir(tc)
207207
tc.assertTrue(isfolder(d))
208208
end
209209

210-
210+
%%
211211
function test_samepath(tc, in_same, other_same, ref_same)
212212
tc.verifyEqual(stdlib.samepath(in_same, other_same), ref_same)
213213
end
214214

215-
215+
%%
216216
function test_which_name(tc)
217217

218218
tc.verifyEmpty(stdlib.which(tempname))

test/TestFilePure.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function test_absolute_path(tc)
205205

206206
function test_normalize(tc)
207207

208-
tc.verifyEqual(stdlib.normalize(""), "")
208+
tc.verifyEqual(stdlib.normalize(""), ".")
209209

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

0 commit comments

Comments
 (0)