Skip to content

Commit d8ad3c7

Browse files
committed
samepath: use java"
1 parent fc402fb commit d8ad3c7

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

+stdlib/+fileio/samepath.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
path2 string {mustBeScalarOrEmpty}
66
end
77

8-
issame = stdlib.fileio.absolute_path(path1) == stdlib.fileio.absolute_path(path2);
8+
if isempty(path1) || isempty(path2)
9+
issame = logical.empty;
10+
return
11+
end
12+
13+
% absolute_path because unlike c++ filesystem, isSameFile does not
14+
% canonicalize first
15+
p1 = java.io.File(stdlib.fileio.absolute_path(path1)).toPath();
16+
p2 = java.io.File(stdlib.fileio.absolute_path(path2)).toPath();
17+
18+
try
19+
issame = java.nio.file.Files.isSameFile(p1, p2);
20+
catch e
21+
if e.identifier == "MATLAB:Java:GenericException" && contains(e.message, "NoSuchFileException")
22+
issame = false;
23+
else
24+
rethrow(e)
25+
end
26+
end
927

1028
end

+stdlib/+test/TestFileImpure.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,26 @@ function test_expanduser(tc)
1515
tc.verifyTrue(all(endsWith(expanduser(["~/abc", "~/123"]), ["abc", "123"])))
1616
end
1717

18+
1819
function test_makedir(tc)
1920
d = tempname;
2021
stdlib.makedir(d)
2122
tc.assertTrue(isfolder(d))
2223
end
2324

25+
26+
function test_samepath(tc)
27+
import stdlib.fileio.samepath
28+
tc.assumeTrue(usejava("jvm"), "Java required for samepath")
29+
30+
tc.verifyEmpty(samepath(string.empty, string.empty))
31+
tc.verifyTrue(samepath("", ""))
32+
tc.verifyFalse(samepath(tempname, tempname))
33+
tc.verifyTrue(samepath("~/b/..", "~/c/.."), "tilde path ..")
34+
tc.verifyTrue(samepath(".", "a/.."))
35+
end
36+
37+
2438
function test_which_name(tc)
2539

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

+stdlib/+test/TestFilePure.m

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ function test_posix(tc)
1313
end
1414
end
1515

16-
function test_samepath(tc)
17-
tc.assumeTrue(usejava("jvm"), "Java required for samepath")
18-
19-
tc.verifyEmpty(stdlib.samepath(string.empty, string.empty))
20-
tc.verifyTrue(stdlib.samepath("", ""))
21-
tc.verifyFalse(stdlib.samepath("a", "b"))
22-
tc.verifyTrue(stdlib.samepath("a/b/..", "a/c/.."))
23-
tc.verifyTrue(stdlib.samepath(".", "a/.."))
24-
25-
end
2616

2717
function test_path_tail(tc)
2818

0 commit comments

Comments
 (0)