Skip to content

Commit 6519391

Browse files
committed
set_permissions: choose_method
1 parent 888a196 commit 6519391

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

+stdlib/+legacy/set_permissions.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function ok = set_permissions(file, readable, writable, executable)
22

3+
ok = false;
4+
5+
if ~stdlib.exists(file), return, end
6+
37
mode = '';
48
% mode is space-delimited
59
if ~ispc()

+stdlib/+native/set_permissions.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function ok = set_permissions(file, readable, writable, executable)
22

3+
ok = false;
4+
5+
if ~stdlib.exists(file), return, end
6+
37
p = filePermissions(file);
48

59
k = string.empty;

+stdlib/set_permissions.m

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@
99
%%% Outputs
1010
% * ok (1,1) logical
1111

12-
function ok = set_permissions(file, readable, writable, executable)
12+
function ok = set_permissions(file, readable, writable, executable, method)
1313
arguments
1414
file {mustBeTextScalar}
1515
readable (1,1) {mustBeInteger, mustBeInRange(readable, -1, 1)}
1616
writable (1,1) {mustBeInteger, mustBeInRange(writable, -1, 1)}
1717
executable (1,1) {mustBeInteger, mustBeInRange(executable, -1, 1)}
18+
method (1,:) string = ["native", "legacy"]
1819
end
1920

20-
ok = false;
21+
fun = choose_method(method, "set_permissions", 'R2025a');
2122

22-
if ~stdlib.exists(file), return, end
23-
24-
25-
if isMATLABReleaseOlderThan('R2025a')
26-
ok = stdlib.legacy.set_permissions(file, readable, writable, executable);
27-
else
28-
ok = stdlib.native.set_permissions(file, readable, writable, executable);
29-
end
23+
ok = fun(file, readable, writable, executable);
3024

3125
end

test/TestPermissions.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
properties (TestParameter)
44
Ps = {".", pwd(), "", tempname(), mfilename('fullpath') + ".m"}
5-
sp_fun = {@stdlib.native.set_permissions, @stdlib.legacy.set_permissions}
5+
sp_fun = {'native', 'legacy'}
66
end
77

88
methods(TestClassSetup)
@@ -56,18 +56,23 @@ function test_set_permissions_noread(tc)
5656

5757
function test_set_permissions_nowrite(tc, sp_fun)
5858
import matlab.unittest.constraints.StartsWithSubstring
59-
is_capable(tc, sp_fun)
6059

6160
tc.assumeFalse(isMATLABReleaseOlderThan('R2022a'))
6261
td = tc.createTemporaryFolder();
6362

6463
nw = fullfile(td, "no-write");
6564

6665
tc.verifyTrue(stdlib.touch(nw))
67-
tc.verifyTrue(sp_fun(nw, 0, -1, 0))
68-
p = stdlib.get_permissions(nw);
66+
try
67+
tc.verifyTrue(stdlib.set_permissions(nw, 0, -1, 0, sp_fun))
68+
catch e
69+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
70+
return
71+
end
6972

70-
if ~ispc() || ~contains(func2str(sp_fun), ".legacy.")
73+
74+
p = stdlib.get_permissions(nw);
75+
if ~ispc() || sp_fun ~= "legacy"
7176
tc.verifyThat(p, StartsWithSubstring("r-"), "no-write permission failed to set")
7277
end
7378

test/is_capable.m

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

5454
elseif contains(n, ".native.")
5555

56-
if endsWith(n, ["is_exe", "set_permissions"])
56+
if endsWith(n, "is_exe")
5757
tc.assumeFalse(isMATLABReleaseOlderThan('R2025a'))
5858
end
5959

0 commit comments

Comments
 (0)