Skip to content

Commit 7ade9f5

Browse files
committed
username: return char
1 parent 7d20e17 commit 7ade9f5

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

+stdlib/+dotnet/get_username.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
function n = get_username()
44
% https://learn.microsoft.com/en-us/dotnet/api/system.environment.username
55

6-
n = string(System.Environment.UserName);
6+
n = char(System.Environment.UserName);
77

88
end

+stdlib/+java/get_username.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
function n = get_username()
22

3-
try
4-
n = string(javaMethod("getProperty", "java.lang.System", "user.name"));
5-
catch e
6-
warning(e.identifier, "Failed to get username from Java: %s", e.message);
7-
n = string.empty;
8-
end
3+
n = char(javaMethod("getProperty", "java.lang.System", "user.name"));
94

105
end

+stdlib/+python/get_username.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function n = get_username()
22

33
try
4-
n = string(py.getpass.getuser());
4+
n = char(py.getpass.getuser());
55
catch e
66
warning(e.identifier, "get_username(%s) failed: %s", p, e.message);
7-
n = string.empty;
7+
n = '';
88
end
99

1010
end

+stdlib/+sys/get_username.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
function n = get_username()
22

33
if ispc()
4-
cmd = "whoami";
4+
cmd = 'whoami';
55
else
6-
cmd = "id -un";
6+
cmd = 'id -un';
77
end
88
[s, n] = system(cmd);
99

1010
if s == 0
11-
n = string(strtrim(n));
11+
n = strtrim(n);
1212
else
13-
warning("Failed to get username from system %s: %s", cmd, n);
14-
n = string.empty;
13+
n = '';
1514
end
1615

1716
end

test/TestSys.m

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
fun = {@stdlib.isoctave, @stdlib.has_dotnet, ...
99
@stdlib.has_java, @stdlib.has_python}
1010
cpu_arch_fun = {'java', 'dotnet', 'native'}
11-
ia_fun = {'sys', 'dotnet', 'java', 'python'}
1211
cr_method = {'sys', 'java', 'python'}
13-
os_m = {'java', 'python', 'dotnet', 'sys'}
12+
all_fun = {'java', 'python', 'dotnet', 'sys'}
1413
end
1514

1615
methods(TestClassSetup)
@@ -35,10 +34,10 @@ function test_is_cygwin(tc)
3534
tc.verifyFalse(stdlib.is_cygwin())
3635
end
3736

38-
function test_is_admin(tc, ia_fun)
39-
tc.assertNotEmpty(which("stdlib." + ia_fun + ".is_admin"))
37+
function test_is_admin(tc, all_fun)
38+
tc.assertNotEmpty(which("stdlib." + all_fun + ".is_admin"))
4039
try
41-
i = stdlib.is_admin(ia_fun);
40+
i = stdlib.is_admin(all_fun);
4241
tc.verifyClass(i, "logical")
4342
tc.verifyNotEmpty(i)
4443
catch e
@@ -109,9 +108,9 @@ function test_has_python(tc)
109108
tc.verifyTrue(all(v >= [3, 8, 0]), "expected Python >= 3.8")
110109
end
111110

112-
function test_os_version(tc, os_m)
111+
function test_os_version(tc, all_fun)
113112
try
114-
[os, ver] = stdlib.os_version(os_m);
113+
[os, ver] = stdlib.os_version(all_fun);
115114
catch e
116115
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
117116
return
@@ -135,24 +134,27 @@ function test_is_parallel(tc)
135134
tc.verifyClass(ip, 'logical')
136135
end
137136

138-
function test_hostname(tc, ia_fun)
139-
tc.assertNotEmpty(which("stdlib." + ia_fun + ".get_hostname"))
137+
function test_hostname(tc, all_fun)
138+
tc.assertNotEmpty(which("stdlib." + all_fun + ".get_hostname"))
140139
try
141-
h = stdlib.hostname(ia_fun);
140+
h = stdlib.hostname(all_fun);
142141
tc.verifyGreaterThan(strlength(h), 0)
143142
catch e
144143
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
145144
end
146145
end
147146

148-
function test_username(tc, ia_fun)
149-
tc.assertNotEmpty(which("stdlib." + ia_fun + ".get_username"))
147+
function test_username(tc, all_fun)
148+
tc.assertNotEmpty(which("stdlib." + all_fun + ".get_username"))
150149
try
151-
u = stdlib.get_username(ia_fun);
152-
tc.verifyGreaterThan(strlength(u), 0)
150+
u = stdlib.get_username(all_fun);
153151
catch e
154152
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
153+
return
155154
end
155+
156+
tc.verifyClass(u, 'char')
157+
tc.verifyGreaterThan(strlength(u), 0)
156158
end
157159

158160

@@ -176,9 +178,9 @@ function test_cpu_arch(tc, cpu_arch_fun)
176178
tc.verifyGreaterThan(strlength(arch), 0, "CPU architecture should not be empty")
177179
end
178180

179-
function test_ram_total(tc, ia_fun)
181+
function test_ram_total(tc, all_fun)
180182
try
181-
t = stdlib.ram_total(ia_fun);
183+
t = stdlib.ram_total(all_fun);
182184
catch e
183185
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
184186
return

0 commit comments

Comments
 (0)