Skip to content

Commit 939272c

Browse files
committed
username: functionalize, add tests
1 parent 1c22bfa commit 939272c

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

+stdlib/+python/get_username.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function n = get_username()
2+
3+
try
4+
n = string(py.os.getlogin());
5+
catch e
6+
warning(e.identifier, "%s", e.message)
7+
n = string.empty;
8+
end
9+
10+
end

+stdlib/+sys/get_username.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[s, n] = system(cmd);
99

1010
if s == 0
11-
n = strtrim(n); % Remove any trailing newline or spaces
11+
n = string(strtrim(n)); % Remove any trailing newline or spaces
1212
else
1313
warning("Failed to get username from system command: %s", n);
1414
n = string.empty;

+stdlib/get_username.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
%
33
function n = get_username()
44

5-
n = '';
5+
n = string.empty;
66

77
if stdlib.has_dotnet()
88
n = stdlib.dotnet.get_username();
99
elseif stdlib.has_java()
1010
n = stdlib.java.get_username();
11+
elseif stdlib.has_python()
12+
n = stdlib.python.get_username();
1113
end
1214

1315
if strempty(n)
1416
n = stdlib.sys.get_username();
1517
end
1618

17-
try %#ok<*TRYNC>
18-
n = string(n);
1919
end
20-
21-
end
22-
23-
%!assert(!isempty(get_username()))

test/TestSys.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
stdlib.has_java, stdlib.has_python}
1010
cpu_arch_fun = {@stdlib.cpu_arch, @stdlib.dotnet.cpu_arch, @stdlib.java.cpu_arch}
1111
host_fun = {@stdlib.hostname, @stdlib.dotnet.get_hostname, @stdlib.java.get_hostname, @stdlib.python.get_hostname}
12+
user_fun = {@stdlib.get_username, @stdlib.dotnet.get_username, @stdlib.java.get_username, @stdlib.python.get_username}
1213
ram_free_fun = {@stdlib.ram_free, @stdlib.sys.ram_free, @stdlib.java.ram_free, @stdlib.python.ram_free}
1314
ram_total_fun = {@stdlib.ram_total, @stdlib.sys.ram_total, @stdlib.dotnet.ram_total @stdlib.java.ram_total}
1415
end
@@ -91,8 +92,9 @@ function test_hostname(tc, host_fun)
9192
tc.verifyGreaterThan(strlength(h), 0)
9293
end
9394

94-
function test_username(tc)
95-
u = stdlib.get_username();
95+
function test_username(tc, user_fun)
96+
is_capable(tc, user_fun)
97+
u = user_fun();
9698
tc.verifyGreaterThan(strlength(u), 0)
9799
end
98100

0 commit comments

Comments
 (0)