2019-06-06 12:18:00 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
use std::io;
|
|
|
|
|
2019-09-22 20:32:11 +00:00
|
|
|
use crate::common::{self, TestCommand};
|
2019-06-06 12:18:00 +00:00
|
|
|
|
|
|
|
// TODO: Add tests for if root user (UID == 0)
|
|
|
|
// Requires mocking
|
|
|
|
|
|
|
|
#[test]
|
2019-06-10 14:56:17 +00:00
|
|
|
fn no_env_variables() -> io::Result<()> {
|
2019-07-02 20:12:53 +00:00
|
|
|
let output = common::render_module("username").output()?;
|
2019-06-06 12:18:00 +00:00
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
2019-06-10 14:56:17 +00:00
|
|
|
assert_eq!("", actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-06-06 12:18:00 +00:00
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
#[test]
|
|
|
|
fn logname_equals_user() -> io::Result<()> {
|
2019-06-06 12:18:00 +00:00
|
|
|
let output = common::render_module("username")
|
|
|
|
.env("LOGNAME", "astronaut")
|
|
|
|
.env("USER", "astronaut")
|
|
|
|
.output()?;
|
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
2019-06-10 14:56:17 +00:00
|
|
|
assert_eq!("", actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-06-06 12:18:00 +00:00
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
#[test]
|
|
|
|
fn ssh_wo_username() -> io::Result<()> {
|
2019-06-06 12:18:00 +00:00
|
|
|
// SSH connection w/o username
|
|
|
|
let output = common::render_module("username")
|
|
|
|
.env("SSH_CONNECTION", "192.168.223.17 36673 192.168.223.229 22")
|
|
|
|
.output()?;
|
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
2019-06-10 14:56:17 +00:00
|
|
|
assert_eq!("", actual);
|
2019-06-06 12:18:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn current_user_not_logname() -> io::Result<()> {
|
|
|
|
let output = common::render_module("username")
|
|
|
|
.env("LOGNAME", "astronaut")
|
|
|
|
.env("USER", "cosmonaut")
|
|
|
|
.output()?;
|
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
let expected = format!("via {} ", Color::Yellow.bold().paint("cosmonaut"));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh_connection() -> io::Result<()> {
|
|
|
|
let output = common::render_module("username")
|
|
|
|
.env("USER", "astronaut")
|
|
|
|
.env("SSH_CONNECTION", "192.168.223.17 36673 192.168.223.229 22")
|
|
|
|
.output()?;
|
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
let expected = format!("via {} ", Color::Yellow.bold().paint("astronaut"));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-22 20:32:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_always() -> io::Result<()> {
|
|
|
|
let output = common::render_module("username")
|
|
|
|
.env("USER", "astronaut")
|
|
|
|
.use_config(toml::toml! {
|
|
|
|
[username]
|
|
|
|
show_always = true})
|
|
|
|
.output()?;
|
|
|
|
let actual = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
let expected = format!("via {} ", Color::Yellow.bold().paint("astronaut"));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
Ok(())
|
|
|
|
}
|