From ff2a4b09646a8a5949808bfadf31747751831963 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Wed, 29 Mar 2023 14:16:06 +0200 Subject: Function to get the current user's groups --- tests/cli.rs | 34 ++++++++++++++++++++++++++++++++-- user-info/src/lib.rs | 19 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index 1daa850..872c2ce 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -2,7 +2,7 @@ use anyhow::Result; use assert_cmd::{cargo::cargo_bin, Command}; use rexpect::session::{spawn_command, PtySession}; use tempfile::TempDir; -use user_info::get_username; +use user_info::{get_user_groups, get_username}; struct TestContext { p: PtySession, @@ -107,7 +107,7 @@ fn can_init_a_new_git_repo() -> Result<()> { .as_ref() .join("git") .join(username) - .join("my-new-repo.git"), + .join(&format!("{}.git", repo_name)), ) .assert() .success() @@ -115,6 +115,36 @@ fn can_init_a_new_git_repo() -> Result<()> { Ok(()) } +#[test] +#[ignore] +fn can_init_a_new_shared_git_repo() -> Result<()> { + let mut c = spawn_interactive_process()?; + let group = get_user_groups().pop().unwrap(); + let repo_name = "my-new-shared-repo"; + c.p.send_line(&format!("git-init --shared {} {}", group, repo_name))?; + c.p.exp_string(&format!( + "Successfully created \"git/{}/{}.git\"", + group, repo_name + ))?; + expect_prompt(&mut c.p)?; + + Command::new("git") + .arg("rev-list") + .arg("--all") + .current_dir( + c.workdir + .as_ref() + .join("git") + .join(&group) + .join(&format!("{}.git", repo_name)), + ) + .assert() + .success() + .stdout(""); + // TODO: Check file permissions? Shared set in git config? + Ok(()) +} + #[test] fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> { let username = get_username().unwrap(); diff --git a/user-info/src/lib.rs b/user-info/src/lib.rs index bd5ff42..7a42db9 100644 --- a/user-info/src/lib.rs +++ b/user-info/src/lib.rs @@ -1,10 +1,18 @@ -use nix::unistd::{getuid, User}; +use nix::unistd::{getgroups, getuid, Group, User}; pub fn get_username() -> Option { let uid = getuid(); User::from_uid(uid).ok().flatten().map(|user| user.name) } +pub fn get_user_groups() -> Vec { + let gids = getgroups().unwrap_or(Vec::default()); + gids.into_iter() + .filter_map(|gid| Group::from_gid(gid).ok().flatten()) + .map(|group| group.name) + .collect() +} + #[cfg(test)] mod test { use super::*; @@ -17,4 +25,13 @@ mod test { let username_len = username.unwrap().trim().len(); assert!(username_len > 0); } + + #[test] + fn it_returns_some_groups() { + let groups = get_user_groups(); + assert!(groups.len() > 0); + for group in groups { + assert!(group.trim().len() > 0); + } + } } -- cgit v1.2.3