Skip to content

Commit 2256b6e

Browse files
committed
is_admin: choose_method
1 parent a66d336 commit 2256b6e

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

+stdlib/is_admin.m

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
%% IS_ADMIN is the process run as root / admin
22

3-
function y = is_admin()
3+
function y = is_admin(method)
4+
arguments
5+
method (1,:) string = ["java", "dotnet", "python", "sys"]
6+
end
47

8+
fun = choose_method(method, "is_admin");
59

6-
if (isunix() || ~isMATLABReleaseOlderThan('R2024a')) && stdlib.has_python()
7-
y = stdlib.python.is_admin();
8-
elseif ispc() && stdlib.has_dotnet()
9-
y = stdlib.dotnet.is_admin();
10-
elseif isunix() && stdlib.has_java()
11-
y = stdlib.java.is_admin();
12-
elseif stdlib.isoctave()
13-
y = getuid() == 0;
14-
else
15-
y = stdlib.sys.is_admin();
16-
end
10+
y = fun();
1711

1812
end
1913

20-
%!assert (islogical(is_admin()))

+stdlib/private/choose_method.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
if endsWith(name, ["create_symlink", "ram_total", "read_symlink"])
1919
if stdlib.dotnet_api() < 6, continue, end
2020
end
21+
22+
if endsWith(name, "is_admin")
23+
if ~ispc(), continue, end
24+
end
25+
2126
case "java"
27+
2228
has = @stdlib.has_java;
23-
if endsWith(name, ["device", "hard_link_count", "inode"])
29+
if endsWith(name, ["device", "hard_link_count", "inode", "is_admin"])
2430
if ~isunix(), continue, end
2531
end
32+
2633
case "python"
2734

2835
has = @stdlib.has_python;
@@ -35,14 +42,22 @@
3542
if ~isunix(), continue, end
3643
end
3744

45+
if endsWith(name, "is_admin")
46+
if ~isunix() || isMATLABReleaseOlderThan('R2024a'), continue, end
47+
end
48+
3849
case "legacy", has = true;
50+
3951
case "native"
52+
4053
has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
4154

4255
if endsWith(name, "create_symlink")
4356
if ~has || ispc(), continue, end
4457
end
58+
4559
case "sys"
60+
4661
has = true;
4762

4863
if endsWith(name, "is_char_device")

test/TestSys.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
cpu_arch_fun = {@stdlib.cpu_arch, @stdlib.dotnet.cpu_arch, @stdlib.java.cpu_arch}
1111
host_fun = {@stdlib.hostname, @stdlib.sys.get_hostname, @stdlib.dotnet.get_hostname, @stdlib.java.get_hostname, @stdlib.python.get_hostname}
1212
user_fun = {@stdlib.get_username, @stdlib.sys.get_username, @stdlib.dotnet.get_username, @stdlib.java.get_username, @stdlib.python.get_username}
13-
is_admin_fun = {@stdlib.is_admin, @stdlib.sys.is_admin, @stdlib.dotnet.is_admin, @stdlib.java.is_admin, @stdlib.python.is_admin}
13+
ia_fun = {'sys', 'dotnet', 'java', 'python'}
1414
ram_free_method = {'sys', 'java', 'python'}
1515
ram_total_method = {'sys', 'dotnet', 'java', 'python'}
1616
cpu_load_method = {"java", "python", "sys"}
@@ -38,10 +38,15 @@ function test_is_cygwin(tc)
3838
tc.verifyFalse(stdlib.is_cygwin())
3939
end
4040

41-
function test_is_admin(tc, is_admin_fun)
42-
is_capable(tc, is_admin_fun)
43-
tc.verifyClass(is_admin_fun(), "logical")
44-
tc.verifyNotEmpty(is_admin_fun())
41+
function test_is_admin(tc, ia_fun)
42+
tc.assertNotEmpty(which("stdlib." + ia_fun + ".is_admin"))
43+
try
44+
i = stdlib.is_admin(ia_fun);
45+
tc.verifyClass(i, "logical")
46+
tc.verifyNotEmpty(i)
47+
catch e
48+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
49+
end
4550
end
4651

4752

test/is_capable.m

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function is_capable(tc, f)
1111
dapi = stdlib.dotnet_api();
1212
tc.assumeGreaterThan(dapi, 0, ".NET not available")
1313

14-
if endsWith(n, ["is_admin", "owner"])
14+
if endsWith(n, "owner")
1515
tc.assumeTrue(ispc(), "Windows only function")
1616
end
1717

@@ -20,10 +20,6 @@ function is_capable(tc, f)
2020
japi = stdlib.java_api();
2121
tc.assumeGreaterThan(japi, 0, "Java not available")
2222

23-
if endsWith(n, "is_admin")
24-
tc.assumeTrue(isunix())
25-
end
26-
2723
elseif contains(n, "python")
2824

2925
tc.assumeTrue(stdlib.has_python(), "Python not available")
@@ -32,10 +28,6 @@ function is_capable(tc, f)
3228
tc.assumeFalse(ispc(), "unix only function")
3329
end
3430

35-
if endsWith(n, "is_admin")
36-
tc.assumeTrue(isunix() || ~isMATLABReleaseOlderThan('R2024a'))
37-
end
38-
3931
elseif contains(n, ".sys.")
4032

4133
if endsWith(n, "samepath")

0 commit comments

Comments
 (0)