From dd56f3fb7ab9ee1e84687980e71e69320f884a64 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Thu, 2 May 2024 14:22:10 +0200 Subject: [PATCH 1/3] Removed a few useless clone(). --- src/greeter.rs | 6 +++--- src/info.rs | 2 +- src/ui/command.rs | 2 +- src/ui/common/menu.rs | 4 ++-- src/ui/power.rs | 6 ++++-- src/ui/sessions.rs | 9 ++++++--- src/ui/users.rs | 8 +++++--- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index 85d846d..597cc7e 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -204,18 +204,18 @@ impl Greeter { // If we should remember the last logged-in user. if greeter.remember { if let Some(username) = get_last_user_username() { - greeter.username = MaskedString::from(username.clone(), get_last_user_name()); + greeter.username = MaskedString::from(username, get_last_user_name()); // If, on top of that, we should remember their last session. if greeter.remember_user_session { - if let Ok(ref session_path) = get_last_user_session_path(&username) { + if let Ok(ref session_path) = get_last_user_session_path(greeter.username.get()) { // Set the selected menu option and the session source. greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0); greeter.session_source = SessionSource::Session(greeter.sessions.selected); } // See if we have the last free-form command from the user. - if let Ok(command) = get_last_user_session(&username) { + if let Ok(command) = get_last_user_session(greeter.username.get()) { greeter.session_source = SessionSource::Command(command); } } diff --git a/src/info.rs b/src/info.rs index 0437452..7ecda9e 100644 --- a/src/info.rs +++ b/src/info.rs @@ -104,7 +104,7 @@ pub fn get_last_user_name() -> Option { } pub fn write_last_username(username: &MaskedString) { - let _ = fs::write(LAST_USER_USERNAME, username.value.clone()); + let _ = fs::write(LAST_USER_USERNAME, &username.value); if let Some(ref name) = username.mask { let _ = fs::write(LAST_USER_NAME, name); diff --git a/src/ui/command.rs b/src/ui/command.rs index d0ae269..1cc7184 100644 --- a/src/ui/command.rs +++ b/src/ui/command.rs @@ -44,7 +44,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box String; + fn format(&self) -> Cow<'_, str>; } #[derive(Default)] diff --git a/src/ui/power.rs b/src/ui/power.rs index b1f6d09..5c4af7d 100644 --- a/src/ui/power.rs +++ b/src/ui/power.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use crate::{power::PowerOption, ui::common::menu::MenuItem}; #[derive(SmartDefault, Clone)] @@ -8,7 +10,7 @@ pub struct Power { } impl MenuItem for Power { - fn format(&self) -> String { - self.label.clone() + fn format(&self) -> Cow<'_, str> { + Cow::Borrowed(&self.label) } } diff --git a/src/ui/sessions.rs b/src/ui/sessions.rs index c7296ee..e014855 100644 --- a/src/ui/sessions.rs +++ b/src/ui/sessions.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + borrow::Cow, + path::{Path, PathBuf}, +}; use crate::Greeter; @@ -86,8 +89,8 @@ pub struct Session { } impl MenuItem for Session { - fn format(&self) -> String { - self.name.clone() + fn format(&self) -> Cow<'_, str> { + Cow::Borrowed(&self.name) } } diff --git a/src/ui/users.rs b/src/ui/users.rs index 745c7f5..69cb87c 100644 --- a/src/ui/users.rs +++ b/src/ui/users.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use super::common::menu::MenuItem; #[derive(Default, Clone)] @@ -7,10 +9,10 @@ pub struct User { } impl MenuItem for User { - fn format(&self) -> String { + fn format(&self) -> Cow<'_, str> { match &self.name { - Some(name) => format!("{name} ({})", self.username), - None => self.username.clone(), + Some(name) => Cow::Owned(format!("{name} ({})", self.username)), + None => Cow::Borrowed(&self.username), } } } From 96f7d283773de8f5bb2b54d5205cbd463b0fb2c3 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Mon, 6 May 2024 09:15:14 +0200 Subject: [PATCH 2/3] Prepare 0.9.0. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 23f75fe..f1de39b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tuigreet" version = "0.9.0" -authors = ["Antoine POPINEAU "] +authors = ["Antoine POPINEAU "] edition = "2018" build = "build.rs" From f67e43ffa07c145267ffc3499dacededc7bd7522 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 8 May 2024 22:27:40 +0200 Subject: [PATCH 3/3] Added instructions to run the test suite (#138). --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index afab5f8..422a4dc 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,21 @@ Please refer to the snippet below for the minimal `tuigreet` configuration: Pre-built binaries of `tuigreet` for several architectures can be found in the [releases](https://github.com/apognu/tuigreet/releases) section of this repository. The [tip prerelease](https://github.com/apognu/tuigreet/releases/tag/tip) is continuously built and kept in sync with the `master` branch. +## Running the tests + +Tests from the default features should run without any special consideration by running `cargo test`. + +If you intend to run the whole test suite, you will need to perform some setup. One of our features uses NSS to list and filter existing users on the system, and in order not to rely on actual users being created on the host, we use [libnss_wrapper](https://cwrap.org/nss_wrapper.html) to mock responses from NSS. Without this, the tests would use the real user list from your system and probably fail because it cannot find the one it looks for. + +After installing `libnss_wrapper` on your system (or compiling it to get the `.so`), you can run those specific tests as such: + +``` +$ export NSS_WRAPPER_PASSWD=contrib/fixtures/passwd +$ export NSS_WRAPPER_GROUP=contrib/fixtures/group +$ LD_PRELOAD=/path/to/libnss_wrapper.so cargo test --features nsswrapper nsswrapper_ # To run those tests specifically +$ LD_PRELOAD=/path/to/libnss_wrapper.so cargo test --all-features # To run the whole test suite +``` + ## Configuration Edit `/etc/greetd/config.toml` and set the `command` setting to use `tuigreet`: