Skip to content

Commit

Permalink
remove absolute_path() that duplicated resolve() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Sep 17, 2024
1 parent 9dd3dfe commit b7d01bb
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 115 deletions.
37 changes: 0 additions & 37 deletions +stdlib/absolute_path.m

This file was deleted.

2 changes: 1 addition & 1 deletion +stdlib/is_exe.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
ok = java.io.File(file).canExecute();

% more complicated
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.absolute_path(file)).toPath());
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.canonical(file)).toPath());

end
6 changes: 1 addition & 5 deletions +stdlib/is_readable.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
file (1,1) string
end

import java.io.File
import java.nio.file.Files


ok = Files.isReadable(File(stdlib.absolute_path(file)).toPath());
ok = java.nio.file.Files.isReadable(java.io.File(stdlib.canonical(file)).toPath());

end
2 changes: 1 addition & 1 deletion +stdlib/is_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

if isMATLABReleaseOlderThan("R2024b")
% must be absolute path
p = stdlib.absolute_path(p);
p = stdlib.canonical(p);
ok = java.nio.file.Files.isSymbolicLink(java.io.File(p).toPath());
else
ok = isSymbolicLink(p);
Expand Down
6 changes: 1 addition & 5 deletions +stdlib/is_writable.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
file (1,1) string
end

import java.io.File
import java.nio.file.Files


ok = Files.isWritable(File(stdlib.absolute_path(file)).toPath());
ok = java.nio.file.Files.isWritable(java.io.File(stdlib.canonical(file)).toPath());

end
5 changes: 1 addition & 4 deletions +stdlib/normalize.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
p (1,1) string
end

import java.io.File
import stdlib.expanduser

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

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

Expand Down
4 changes: 2 additions & 2 deletions +stdlib/read_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
return
end

r = stdlib.absolute_path(p);
% must be absolute path
r = stdlib.canonical(p);

% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
% must be absolute path
r = stdlib.posix(...
java.nio.file.Files.readSymbolicLink(java.io.File(r).toPath()));

Expand Down
16 changes: 9 additions & 7 deletions +stdlib/resolve.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function c = resolve(p)
function c = resolve(p, expand_tilde)
%% resolve(p)
% path need not exist--absolute path will be relative to pwd if not exist
% if path exists, same result as canonical()
Expand All @@ -17,12 +17,14 @@
% non-existant path is made absolute relative to pwd
arguments
p (1,1) string
expand_tilde (1,1) logical=true
end

import java.io.File

% have to expand ~ first (like C++ filesystem::path::absolute)
c = stdlib.expanduser(p);
if expand_tilde
c = stdlib.expanduser(p);
else
c = p;
end

if ispc && startsWith(c, "\\")
% UNC path is not canonicalized
Expand All @@ -34,8 +36,8 @@
c = stdlib.join(pwd, c);
end

% % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()

c = stdlib.posix(File(c).getCanonicalPath());
c = stdlib.posix(java.io.File(java.io.File(c).getAbsolutePath()).toPath().normalize());

end % function
27 changes: 0 additions & 27 deletions test/TestFileImpure.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,6 @@ function test_touch_modtime(tc)
end


function test_absolute_path(tc)
import matlab.unittest.constraints.StartsWithSubstring

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

pabs = stdlib.absolute_path('2foo');
pabs2 = stdlib.absolute_path('4foo');
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(pabs, pabs2, 2))

par1 = stdlib.absolute_path("../2foo");
tc.verifyNotEmpty(par1)

par2 = stdlib.absolute_path("../4foo");
tc.verifyTrue(strncmp(par2, pabs2, 2))

pt1 = stdlib.absolute_path("bar/../2foo");
tc.verifyNotEmpty(pt1)

va = stdlib.absolute_path("2foo");
vb = stdlib.absolute_path("4foo");
tc.verifyThat(va, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(va, vb, 2))

end


function test_canonical(tc)
import matlab.unittest.fixtures.TemporaryFolderFixture
import matlab.unittest.fixtures.CurrentFolderFixture
Expand Down
26 changes: 0 additions & 26 deletions test/TestFilePure.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,6 @@ function test_is_absolute(tc, in_is_absolute, ref_is_absolute)
end


function test_absolute_path(tc)
import matlab.unittest.constraints.StartsWithSubstring

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

pabs = stdlib.absolute_path('2foo');
pabs2 = stdlib.absolute_path('4foo');
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(pabs, pabs2, 2))

par1 = stdlib.absolute_path("../2foo");
tc.verifyNotEmpty(par1)

par2 = stdlib.absolute_path("../4foo");
tc.verifyTrue(strncmp(par2, pabs2, 2))

pt1 = stdlib.absolute_path("bar/../2foo");
tc.verifyNotEmpty(pt1)

va = stdlib.absolute_path("2foo");
vb = stdlib.absolute_path("4foo");
tc.verifyThat(va, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(va, vb, 2))

end

function test_normalize(tc)

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

0 comments on commit b7d01bb

Please sign in to comment.