Separate users into users and groups modules

This commit is contained in:
Benjamin Sago 2017-05-20 18:39:37 +01:00
parent 070fc76a8d
commit f2169faf94
3 changed files with 192 additions and 178 deletions

128
src/output/groups.rs Normal file
View File

@ -0,0 +1,128 @@
use users::{Users, Groups};
use fs::fields as f;
use output::cell::TextCell;
use output::details::Table;
impl<'a, U: Users+Groups+'a> Table<'a, U> {
pub fn render_group(&self, group: f::Group) -> TextCell {
use users::os::unix::GroupExt;
let mut style = self.opts.colours.users.group_not_yours;
let users = self.env.users();
let group = match users.get_group_by_gid(group.0) {
Some(g) => (*g).clone(),
None => return TextCell::paint(style, group.0.to_string()),
};
let current_uid = users.get_current_uid();
if let Some(current_user) = users.get_user_by_uid(current_uid) {
if current_user.primary_group_id() == group.gid()
|| group.members().contains(&current_user.name().to_owned()) {
style = self.opts.colours.users.group_yours;
}
}
TextCell::paint(style, group.name().to_owned())
}
}
#[cfg(test)]
#[allow(unused_results)]
pub mod test {
use output::details::Details;
use output::details::test::new_table;
use fs::fields as f;
use output::column::Columns;
use output::cell::TextCell;
use users::{User, Group};
use users::mock::MockUsers;
use users::os::unix::GroupExt;
use ansi_term::Style;
use ansi_term::Colour::*;
#[test]
fn named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(101).normal();
let mut users = MockUsers::with_current_uid(1000);
users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users);
let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(101).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(87).normal();
let users = MockUsers::with_current_uid(1000);
let table = new_table(&columns, &details, users);
let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(87).normal(), "100");
assert_eq!(expected, table.render_group(group));
}
#[test]
fn primary() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_yours = Fixed(64).normal();
let mut users = MockUsers::with_current_uid(2);
users.add_user(User::new(2, "eve", 100));
users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users);
let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(64).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn secondary() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_yours = Fixed(31).normal();
let mut users = MockUsers::with_current_uid(2);
users.add_user(User::new(2, "eve", 666));
let test_group = Group::new(100, "folk").add_member("eve");
users.add_group(test_group);
let table = new_table(&columns, &details, users);
let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(31).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn overflow() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_not_yours = Blue.underline();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let group = f::Group(2_147_483_648);
let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_group(group));
}
}

View File

@ -17,3 +17,4 @@ mod tree;
pub mod file_name; pub mod file_name;
mod escape; mod escape;
mod users; mod users;
mod groups;

View File

@ -1,12 +1,11 @@
use users::{Users, Groups}; use users::Users;
use fs::fields as f; use fs::fields as f;
use output::cell::TextCell; use output::cell::TextCell;
use output::details::Table; use output::details::Table;
impl<'a, U: Users+Groups+'a> Table<'a, U> { impl<'a, U: Users+'a> Table<'a, U> {
pub fn render_user(&self, user: f::User) -> TextCell { pub fn render_user(&self, user: f::User) -> TextCell {
let users = self.env.users(); let users = self.env.users();
@ -19,204 +18,90 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
else { self.opts.colours.users.user_someone_else }; else { self.opts.colours.users.user_someone_else };
TextCell::paint(style, user_name) TextCell::paint(style, user_name)
} }
pub fn render_group(&self, group: f::Group) -> TextCell {
use users::os::unix::GroupExt;
let mut style = self.opts.colours.users.group_not_yours;
let users = self.env.users();
let group = match users.get_group_by_gid(group.0) {
Some(g) => (*g).clone(),
None => return TextCell::paint(style, group.0.to_string()),
};
let current_uid = users.get_current_uid();
if let Some(current_user) = users.get_user_by_uid(current_uid) {
if current_user.primary_group_id() == group.gid()
|| group.members().contains(&current_user.name().to_owned()) {
style = self.opts.colours.users.group_yours;
}
}
TextCell::paint(style, group.name().to_owned())
}
} }
#[cfg(test)] #[cfg(test)]
#[allow(unused_results)]
pub mod test { pub mod test {
pub use output::details::{Table, Environment, Details}; use output::details::Details;
pub use output::details::test::new_table; use output::details::test::new_table;
pub use std::sync::Mutex;
pub use fs::{File, fields as f}; use fs::fields as f;
pub use output::column::{Column, Columns}; use output::column::Columns;
pub use output::cell::TextCell; use output::cell::TextCell;
pub use users::{User, Group, uid_t, gid_t}; use users::User;
pub use users::mock::MockUsers; use users::mock::MockUsers;
pub use users::os::unix::{UserExt, GroupExt}; use ansi_term::Colour::*;
pub use datetime::fmt::DateFormat;
pub use ansi_term::Style;
pub use ansi_term::Colour::*;
mod users { #[test]
#![allow(unused_results)] fn named() {
use super::*; let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_you = Red.bold();
#[test] let mut users = MockUsers::with_current_uid(1000);
fn named() { users.add_user(User::new(1000, "enoch", 100));
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_you = Red.bold();
let mut users = MockUsers::with_current_uid(1000); let table = new_table(&columns, &details, users);
users.add_user(User::new(1000, "enoch", 100));
let table = new_table(&columns, &details, users); let user = f::User(1000);
let expected = TextCell::paint_str(Red.bold(), "enoch");
let user = f::User(1000); assert_eq!(expected, table.render_user(user))
let expected = TextCell::paint_str(Red.bold(), "enoch");
assert_eq!(expected, table.render_user(user))
}
#[test]
fn unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_you = Cyan.bold();
let users = MockUsers::with_current_uid(1000);
let table = new_table(&columns, &details, users);
let user = f::User(1000);
let expected = TextCell::paint_str(Cyan.bold(), "1000");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn different_named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_someone_else = Green.bold();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
table.env.users().add_user(User::new(1000, "enoch", 100));
let user = f::User(1000);
let expected = TextCell::paint_str(Green.bold(), "enoch");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn different_unnamed() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_someone_else = Red.normal();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let user = f::User(1000);
let expected = TextCell::paint_str(Red.normal(), "1000");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn overflow() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_someone_else = Blue.underline();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let user = f::User(2_147_483_648);
let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_user(user));
}
} }
mod groups { #[test]
#![allow(unused_results)] fn unnamed() {
use super::*; let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.user_you = Cyan.bold();
#[test] let users = MockUsers::with_current_uid(1000);
fn named() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(101).normal();
let mut users = MockUsers::with_current_uid(1000); let table = new_table(&columns, &details, users);
users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users);
let group = f::Group(100); let user = f::User(1000);
let expected = TextCell::paint_str(Fixed(101).normal(), "folk"); let expected = TextCell::paint_str(Cyan.bold(), "1000");
assert_eq!(expected, table.render_group(group)) assert_eq!(expected, table.render_user(user));
} }
#[test] #[test]
fn unnamed() { fn different_named() {
let columns = Columns::default().for_dir(None); let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_not_yours = Fixed(87).normal(); details.colours.users.user_someone_else = Green.bold();
let users = MockUsers::with_current_uid(1000); let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let table = new_table(&columns, &details, users); table.env.users().add_user(User::new(1000, "enoch", 100));
let group = f::Group(100); let user = f::User(1000);
let expected = TextCell::paint_str(Fixed(87).normal(), "100"); let expected = TextCell::paint_str(Green.bold(), "enoch");
assert_eq!(expected, table.render_group(group)); assert_eq!(expected, table.render_user(user));
} }
#[test] #[test]
fn primary() { fn different_unnamed() {
let columns = Columns::default().for_dir(None); let columns = Columns::default().for_dir(None);
let mut details = Details::default(); let mut details = Details::default();
details.colours.users.group_yours = Fixed(64).normal(); details.colours.users.user_someone_else = Red.normal();
let mut users = MockUsers::with_current_uid(2); let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
users.add_user(User::new(2, "eve", 100));
users.add_group(Group::new(100, "folk"));
let table = new_table(&columns, &details, users); let user = f::User(1000);
let expected = TextCell::paint_str(Red.normal(), "1000");
assert_eq!(expected, table.render_user(user));
}
let group = f::Group(100); #[test]
let expected = TextCell::paint_str(Fixed(64).normal(), "folk"); fn overflow() {
assert_eq!(expected, table.render_group(group)) let columns = Columns::default().for_dir(None);
} let mut details = Details::default();
details.colours.users.user_someone_else = Blue.underline();
#[test] let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
fn secondary() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_yours = Fixed(31).normal();
let mut users = MockUsers::with_current_uid(2); let user = f::User(2_147_483_648);
users.add_user(User::new(2, "eve", 666)); let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_user(user));
let test_group = Group::new(100, "folk").add_member("eve");
users.add_group(test_group);
let table = new_table(&columns, &details, users);
let group = f::Group(100);
let expected = TextCell::paint_str(Fixed(31).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn overflow() {
let columns = Columns::default().for_dir(None);
let mut details = Details::default();
details.colours.users.group_not_yours = Blue.underline();
let table = new_table(&columns, &details, MockUsers::with_current_uid(0));
let group = f::Group(2_147_483_648);
let expected = TextCell::paint_str(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_group(group));
}
} }
} }