Skip to content

Commit

Permalink
add join() posix throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Feb 29, 2024
1 parent 7fba4ee commit 8945e7c
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 30 deletions.
4 changes: 2 additions & 2 deletions +stdlib/+fileio/absolute_path.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

if ~stdlib.fileio.is_absolute_path(abspath)
% .toAbsolutePath() default is Documents/Matlab, which is probably not wanted.
abspath = fullfile(pwd, abspath);
abspath = stdlib.fileio.join(pwd, abspath);
end

abspath = string(java.io.File(abspath).getAbsolutePath());
abspath = stdlib.posix(java.io.File(abspath).getAbsolutePath());

end % function
2 changes: 1 addition & 1 deletion +stdlib/+fileio/canonical.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
if ~stdlib.fileio.is_absolute_path(c)
if isfile(c) || isfolder(c)
% workaround Java/Matlab limitations
c = fullfile(pwd, c);
c = stdlib.fileio.join(pwd, c);
else
% for non-existing path, return normalized relative path
% like C++ filesystem weakly_canonical()
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/expanduser.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
home = stdlib.fileio.homedir();

if ~isempty(home)
e = fullfile(home, extractAfter(e, 1));
e = stdlib.fileio.join(home, extractAfter(e, 1));
end

end %function
10 changes: 10 additions & 0 deletions +stdlib/+fileio/join.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function p = join(a, b)
%% JOIN: join two paths with posix file separator
arguments
a string {mustBeScalarOrEmpty}
b string {mustBeScalarOrEmpty}
end

p = stdlib.fileio.posix(fullfile(a, b));

end
9 changes: 3 additions & 6 deletions +stdlib/+fileio/posix.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
function ppath = posix(file)
function p = posix(p)

arguments
file string
p string
end


ppath = fullfile(file);

if ispc
ppath = strrep(ppath, "\", "/");
p = strrep(p, "\", "/");
end

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

if ~stdlib.fileio.is_absolute_path(c)
% .toAbsolutePath() default is Documents/Matlab, which is probably not wanted.
c = fullfile(pwd, c);
c = stdlib.fileio.join(pwd, c);
end

% similar benchmark time as java method
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/which.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
for name = names

for p = fpath
exe = fullfile(p, name);
exe = stdlib.fileio.join(p, name);
if stdlib.fileio.is_exe(exe)
return
end
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/+fileio/with_suffix.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function filename = with_suffix(filename, suffix)

arguments
filename string
filename string {mustBeScalarOrEmpty}
suffix (1,1) string
end

[direc, name, ext] = fileparts(filename);
if ext ~= suffix
filename = fullfile(direc, name + suffix);
filename = stdlib.fileio.join(direc, name + suffix);
end

end
10 changes: 10 additions & 0 deletions +stdlib/join.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function p = join(a, b)
%% JOIN: join two paths with posix file separator
arguments
a string {mustBeScalarOrEmpty}
b string {mustBeScalarOrEmpty}
end

p = stdlib.fileio.join(a, b);

end
11 changes: 5 additions & 6 deletions test/TestFileImpure.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function test_expanduser(tc)
tc.verifyEqual(stdlib.expanduser("~"), h)

e = stdlib.expanduser("~/foo");
tc.verifyEqual(e, stdlib.posix(fullfile(h, "foo")))
tc.verifyEqual(e, stdlib.fileio.join(h, "foo"))

end

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

% test existing file
r = stdlib.parent(mfilename('fullpath'));
rp = fullfile(r, "..");
tc.verifyEqual(stdlib.canonical(rp), stdlib.parent(r))
tc.verifyEqual(stdlib.canonical(fullfile(r, "..")), stdlib.parent(r))

h = stdlib.fileio.homedir;
tc.verifyEqual(stdlib.canonical("~"), h)
Expand Down Expand Up @@ -105,16 +104,16 @@ function test_resolve(tc)

% test existing file
r = stdlib.parent(mfilename('fullpath'));
rp = fullfile(r, "..");
rp = stdlib.parent(r);
tc.verifyEqual(stdlib.resolve(rp), stdlib.parent(r))

h = stdlib.fileio.homedir;
tc.verifyEqual(stdlib.resolve("~"), h)
tc.verifyEqual(stdlib.resolve("~/"), h)
tc.verifyEqual(stdlib.resolve("~/.."), stdlib.parent(h))

tc.verifyEqual(stdlib.resolve("nobody.txt"), fullfile(td, "nobody.txt"))
tc.verifyEqual(stdlib.resolve("../nobody.txt"), fullfile(stdlib.parent(td), "nobody.txt"))
tc.verifyEqual(stdlib.resolve("nobody.txt"), stdlib.join(td, "nobody.txt"))
tc.verifyEqual(stdlib.resolve("../nobody.txt"), stdlib.join(stdlib.parent(td), "nobody.txt"))

end

Expand Down
1 change: 0 additions & 1 deletion test/TestFilePure.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ function test_with_suffix(tc)
tc.verifyEqual(stdlib.with_suffix("", ""), "")

tc.verifyEqual(stdlib.with_suffix("foo.h5", ".nc"), "foo.nc")
tc.verifyEqual(stdlib.with_suffix(["foo.h5", "bar.dat"], ".nc"), ["foo.nc", "bar.nc"])

tc.verifyEqual(stdlib.with_suffix("c", ""), "c")
tc.verifyEqual(stdlib.with_suffix("c.nc", ""), "c")
Expand Down
2 changes: 1 addition & 1 deletion test/TestHDF5.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function setup_file(tc)
tc.TestData.utf = utf;
tc.TestData.utf2 = utf2;

tc.TestData.basic = fullfile(fixture.Folder, "basic.h5");
tc.TestData.basic = stdlib.join(fixture.Folder, "basic.h5");
bf = tc.TestData.basic;

% create test data first, so that parallel tests works
Expand Down
10 changes: 5 additions & 5 deletions test/TestHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ function test_extract(tc)
tmpDir = fixture.Folder;

r = fileparts(mfilename('fullpath'));
fn = fullfile(r, "hello.tar.zst");
fn = stdlib.join(r, "hello.tar.zst");

tc.assumeThat(fn, IsFile)

tc.assumeNotEmpty(stdlib.which("cmake"), "CMake not available")

stdlib.extract_zstd(fn, tmpDir)
tc.verifyThat(fullfile(tmpDir, "test/hello.txt"), IsFile)
tc.verifyThat(stdlib.join(tmpDir, "test/hello.txt"), IsFile)

end

function test_sha256(tc)

r = fileparts(mfilename('fullpath'));
fn = fullfile(r, "hello.tar.zst");
fn = stdlib.join(r, "hello.tar.zst");

tc.verifyEqual(stdlib.sha256sum(fn), "36c1bbbdfd8d04ef546ffb15b9c0a65767fd1fe9a6135a257847e3a51fb1426c")

Expand All @@ -42,12 +42,12 @@ function test_sha256(tc)
function test_md5sum(tc)

r = fileparts(mfilename('fullpath'));
fn = fullfile(r, "hello.tar.zst");
fn = stdlib.join(r, "hello.tar.zst");

tc.verifyEqual(stdlib.md5sum(fn), "d58cfb32e075781ba59082a8b18287f9")

end

end

end
end
2 changes: 1 addition & 1 deletion test/TestIni.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function test_example(tc)
import matlab.unittest.constraints.IsFile

cwd = fileparts(mfilename('fullpath'));
example = fullfile(cwd, "example.ini");
example = stdlib.join(cwd, "example.ini");

tc.assumeThat(example, IsFile)

Expand Down
2 changes: 1 addition & 1 deletion test/TestNetCDF.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function setup_file(tc)
tc.TestData.utf1 = utf1;
tc.TestData.utf2 = utf2;

basic = fullfile(fixture.Folder, "basic.nc");
basic = stdlib.join(fixture.Folder, "basic.nc");
tc.TestData.basic = basic;

% create test data first, so that parallel tests works
Expand Down
2 changes: 1 addition & 1 deletion test/TestWSL.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function test_is_exe_which_wsl(tc)
tc.assumeNotEmpty(cc, "did not find WSL C compiler")

cwd = fileparts(mfilename('fullpath'));
src = stdlib.sys.winpath2wslpath(fullfile(cwd, "main.c"));
src = stdlib.sys.winpath2wslpath(stdlib.join(cwd, "main.c"));

[ret, out_wsl] = system("wsl mktemp -u");
tc.assumeEqual(ret, 0, "could not get WSL tempfile")
Expand Down

0 comments on commit 8945e7c

Please sign in to comment.