fix: GECOS subfield trimming and trailing backslash handling (v0.3.1)
display_name() now returns only the first GECOS subfield (before comma) instead of the full GECOS string with room numbers and phone extensions. split_shell_words() returns None for trailing backslashes instead of silently ignoring them.
This commit is contained in:
+13
-2
@@ -30,12 +30,12 @@ pub struct User {
|
||||
}
|
||||
|
||||
impl User {
|
||||
/// Return the display name (GECOS if available, otherwise username).
|
||||
/// Return the display name (first GECOS subfield if available, otherwise username).
|
||||
pub fn display_name(&self) -> &str {
|
||||
if self.gecos.is_empty() {
|
||||
&self.username
|
||||
} else {
|
||||
&self.gecos
|
||||
self.gecos.split(',').next().unwrap_or(&self.username)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,6 +150,17 @@ mod tests {
|
||||
assert_eq!(users[0].home, PathBuf::from("/home/testuser"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gecos_subfield_trimmed() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = make_passwd(
|
||||
dir.path(),
|
||||
"testuser:x:1000:1000:Test User,Room 123,555-1234:/home/testuser:/bin/bash\n",
|
||||
);
|
||||
let users = get_users(Some(&path));
|
||||
assert_eq!(users[0].display_name(), "Test User");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skip_system_users() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user