Skip to content

Commit ac72aca

Browse files
committed
create_symlink: functionalize and improve testt
1 parent d163d12 commit ac72aca

File tree

7 files changed

+58
-57
lines changed

7 files changed

+58
-57
lines changed

+stdlib/+dotnet/create_symlink.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function ok = create_symlink(target, link)
2+
3+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink
4+
try
5+
System.IO.File.CreateSymbolicLink(link, target);
6+
ok = true;
7+
catch e
8+
warning(e.identifier, "%s", e.message)
9+
ok = false;
10+
end
11+
12+
end

+stdlib/+python/create_symlink.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function ok = create_symlink(target, link)
22

3+
ok = false;
4+
5+
if strlength(target) == 0 || strlength(link) == 0 || stdlib.exists(link)
6+
return
7+
end
8+
39
try
410
py.os.symlink(target, link);
511
ok = true;

+stdlib/+sys/create_symlink.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function ok = create_symlink(target, link)
3+
4+
ok = false;
5+
6+
if strlength(target) == 0 || strlength(link) == 0
7+
return
8+
end
9+
10+
if ispc
11+
cmd = sprintf('pwsh -c "New-Item -ItemType SymbolicLink -Path "%s" -Target "%s""', link, target);
12+
else
13+
cmd = sprintf('ln -s "%s" "%s"', target, link);
14+
end
15+
16+
% suppress output text on powershell
17+
[stat, ~] = system(cmd);
18+
19+
ok = stat == 0;
20+
21+
end

+stdlib/create_symlink.m

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,20 @@
3131
if stdlib.has_python()
3232
ok = stdlib.python.create_symlink(target, link);
3333
elseif stdlib.dotnet_api() >= 6
34-
ok = dotnet_create_symlink(target, link);
34+
ok = stdlib.dotnet.create_symlink(target, link);
3535
else
36-
ok = system_create_symlink(target, link);
36+
ok = stdlib.sys.create_symlink(target, link);
3737
end
38-
case "Octave:undefined-function", ok = symlink(target, link) == 0;
39-
otherwise, warning(e.identifier, "%s", e.message)
38+
case "Octave:undefined-function"
39+
ok = symlink(target, link) == 0;
40+
otherwise
41+
warning(e.identifier, "%s", e.message)
4042
end
4143
end
4244

4345
end
4446

4547

46-
function ok = system_create_symlink(target, link)
47-
48-
if ispc
49-
cmd = sprintf('pwsh -c "New-Item -ItemType SymbolicLink -Path "%s" -Target "%s""', link, target);
50-
else
51-
cmd = sprintf('ln -s "%s" "%s"', target, link);
52-
end
53-
54-
% suppress output text on powershell
55-
[stat, ~] = system(cmd);
56-
57-
ok = stat == 0;
58-
end
59-
60-
61-
function ok = dotnet_create_symlink(target, link)
62-
63-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink
64-
try
65-
System.IO.File.CreateSymbolicLink(link, target);
66-
ok = true;
67-
catch e
68-
warning(e.identifier, "%s", e.message)
69-
end
70-
71-
end
72-
7348
%!assert (create_symlink("https://invalid", "https://invalid"), false)
7449
%!test
7550
%! if !ispc

+stdlib/set_modtime.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
%% SET_MODTIME set modification time of path
2-
% requires: java
32

43
function ok = set_modtime(p, t)
54
arguments

test/TestSymlink.m

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@
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}
1314
is_symlink_fun = {@stdlib.is_symlink, @stdlib.sys.is_symlink, @stdlib.dotnet.is_symlink, @stdlib.java.is_symlink, @stdlib.python.is_symlink}
1415
read_symlink_fun = {@stdlib.read_symlink, @stdlib.sys.read_symlink, @stdlib.dotnet.read_symlink, @stdlib.java.read_symlink, @stdlib.python.read_symlink}
1516
end
1617

1718

18-
methods(TestClassSetup)
19-
20-
function set_temp_wd(tc)
21-
if isMATLABReleaseOlderThan('R2022a')
22-
tc.td = tempname();
23-
mkdir(tc.td);
24-
else
25-
tc.td = tc.createTemporaryFolder();
26-
end
27-
end
19+
methods(TestMethodSetup)
20+
% needs to be per-method because multiple functions are used to make the same files
2821

2922
function setup_symlink(tc)
3023

24+
tc.assumeFalse(isMATLABReleaseOlderThan('R2022a'))
25+
26+
tc.td = tc.createTemporaryFolder();
27+
3128
tc.link = fullfile(tc.td, 'my.lnk');
3229

3330
tc.target = strcat(mfilename("fullpath"), '.m');
@@ -38,15 +35,6 @@ function setup_symlink(tc)
3835
end
3936

4037

41-
methods(TestClassTeardown)
42-
function remove_temp_wd(tc)
43-
if isMATLABReleaseOlderThan('R2022a')
44-
[s, m, i] = rmdir(tc.td, 's');
45-
if ~s, warning(i, "Failed to remove temporary directory %s: %s", tc.td, m); end
46-
end
47-
end
48-
end
49-
5038

5139
methods (Test, TestTags=["impure", "symlink"])
5240

@@ -75,16 +63,16 @@ function test_read_symlink(tc, read_symlink_fun)
7563
end
7664

7765

78-
function test_create_symlink(tc)
79-
fprintf("create_symlink mex: %d\n", stdlib.is_mex_fun("stdlib.create_symlink"))
66+
function test_create_symlink(tc, create_symlink_fun)
67+
is_capable(tc, create_symlink_fun)
8068

8169
tc.applyFixture(matlab.unittest.fixtures.SuppressedWarningsFixture(["MATLAB:io:filesystem:symlink:TargetNotFound","MATLAB:io:filesystem:symlink:FileExists"]))
8270

83-
tc.verifyFalse(stdlib.create_symlink('', tempname()))
84-
tc.verifyFalse(stdlib.create_symlink(tc.target, tc.link), "should fail for existing symlink")
71+
tc.verifyFalse(create_symlink_fun('', tempname()))
72+
tc.verifyFalse(create_symlink_fun(tc.target, tc.link), "should fail for existing symlink")
8573

8674
ano = tc.td + "/another.lnk";
87-
tc.verifyTrue(stdlib.create_symlink(tc.target, ano))
75+
tc.verifyTrue(create_symlink_fun(tc.target, ano))
8876
tc.verifyTrue(stdlib.is_symlink(ano))
8977
end
9078

test/is_capable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function is_capable(tc, f)
1212

1313
tc.assumeGreaterThan(dapi, 0)
1414

15-
if endsWith(n, ["ram_total", "read_symlink"])
15+
if endsWith(n, ["create_symlink", "ram_total", "read_symlink"])
1616
tc.assumeGreaterThanOrEqual(dapi, 6);
1717
end
1818

0 commit comments

Comments
 (0)