Skip to content

Commit aeb76b9

Browse files
committed
create_symlink: choose_method
1 parent 0e12b5d commit aeb76b9

File tree

5 files changed

+32
-47
lines changed

5 files changed

+32
-47
lines changed

+stdlib/+native/create_symlink.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function ok = create_symlink(target, link)
2+
3+
createSymbolicLink(link, target);
4+
ok = true;
5+
6+
end

+stdlib/create_symlink.m

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,15 @@
66
%%% Outputs
77
% * ok: true if successful
88

9-
function ok = create_symlink(target, link)
9+
function ok = create_symlink(target, link, method)
1010
arguments
1111
target {mustBeTextScalar}
1212
link {mustBeTextScalar}
13+
method (1,:) string = ["native", "dotnet", "python", "sys"]
1314
end
1415

15-
ok = false;
16+
fun = choose_method(method, "create_symlink", 'R2024b');
1617

17-
try
18-
createSymbolicLink(link, target);
19-
ok = true;
20-
catch e
21-
switch e.identifier
22-
case {"MATLAB:io:filesystem:symlink:NeedsAdminPerms", "MATLAB:UndefinedFunction"}
23-
% windows requires RunAsAdmin
24-
% https://www.mathworks.com/help/releases/R2024b/matlab/ref/createsymboliclink.html
25-
% ok = java.nio.file.Files.createSymbolicLink(java.io.File(link).toPath(), java.io.File(target).toPath());
26-
% Matlab Java doesn't recognize the optional argument omitted.
27-
% see example/Filesystem.java for this working in plain Java.
28-
% see example/javaCreateSymbolicLink.m for a non-working attempt in Matlab.
29-
if stdlib.strempty(target) || stdlib.strempty(link), return, end
18+
ok = fun(target, link);
3019

31-
if stdlib.has_python()
32-
ok = stdlib.python.create_symlink(target, link);
33-
elseif stdlib.dotnet_api() >= 6
34-
ok = stdlib.dotnet.create_symlink(target, link);
35-
else
36-
ok = stdlib.sys.create_symlink(target, link);
37-
end
38-
case "Octave:undefined-function"
39-
ok = symlink(target, link) == 0;
40-
otherwise
41-
warning(e.identifier, "%s", e.message)
42-
end
4320
end
44-
45-
end
46-
47-
48-
%!assert (create_symlink("https://invalid", "https://invalid"), false)
49-
%!test
50-
%! if !ispc
51-
%! assert(create_symlink(tempname, tempname))
52-
%! endif

+stdlib/private/choose_method.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
case "dotnet"
1616
has = @stdlib.has_dotnet;
1717

18-
if endsWith(name, ["ram_total", "read_symlink"])
18+
if endsWith(name, ["create_symlink", "ram_total", "read_symlink"])
1919
if stdlib.dotnet_api() < 6, continue, end
2020
end
2121
case "java"
@@ -36,7 +36,12 @@
3636
end
3737

3838
case {"legacy", "sys"}, has = true;
39-
case "native", has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
39+
case "native"
40+
has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
41+
42+
if endsWith(name, "create_symlink")
43+
if ~has || ispc(), continue, end
44+
end
4045
otherwise
4146
has = str2func("stdlib.has_" + m);
4247
end

test/TestSymlink.m

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
p = {{"not-exist", false}, ...
1111
{mfilename("fullpath") + ".m", false}, ...
1212
{"", false}};
13-
create_symlink_fun = {@stdlib.create_symlink, @stdlib.sys.create_symlink, @stdlib.dotnet.create_symlink, @stdlib.python.create_symlink}
13+
cs_fun = {'native', 'sys', 'dotnet', 'python'}
1414
Pre = {'', "", tempname()}
1515
rs_fun = {'native', 'sys', 'dotnet', 'java', 'python'}
1616
end
@@ -74,16 +74,22 @@ function test_read_symlink(tc, rs_fun)
7474
end
7575

7676

77-
function test_create_symlink(tc, create_symlink_fun)
78-
is_capable(tc, create_symlink_fun)
79-
77+
function test_create_symlink(tc, cs_fun)
78+
tc.assumeNotEmpty(which("stdlib." + cs_fun + ".create_symlink"))
8079
tc.applyFixture(matlab.unittest.fixtures.SuppressedWarningsFixture(["MATLAB:io:filesystem:symlink:TargetNotFound","MATLAB:io:filesystem:symlink:FileExists"]))
8180

82-
tc.verifyFalse(create_symlink_fun('', tempname()))
83-
tc.verifyFalse(create_symlink_fun(tc.target, tc.link), "should fail for existing symlink")
84-
8581
ano = tc.td + "/another.lnk";
86-
tc.verifyTrue(create_symlink_fun(tc.target, ano))
82+
83+
h = @stdilb.create_symlink;
84+
85+
try
86+
tc.verifyFalse(h('', tempname(), cs_fun))
87+
tc.verifyFalse(h(tc.target, tc.link, cs_fun), "should fail for existing symlink")
88+
tc.verifyTrue(h(tc.target, ano, cs_fun))
89+
catch e
90+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
91+
return
92+
end
8793
tc.verifyTrue(stdlib.is_symlink(ano))
8894
end
8995

test/is_capable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function is_capable(tc, f)
1616
tc.assumeTrue(ispc(), "Windows only function")
1717
end
1818

19-
if endsWith(n, ["create_symlink", "ram_total"])
19+
if endsWith(n, "ram_total")
2020
tc.assumeGreaterThanOrEqual(dapi, 6);
2121
end
2222

0 commit comments

Comments
 (0)