Format code

This commit is contained in:
Sergey Isaev 2020-08-07 00:11:38 +03:00
parent 2ccbf2eafe
commit 707168de0a

View File

@ -63,92 +63,110 @@ mod tests {
use super::*;
use std::{
env,
fs::{remove_dir, remove_file},
io,
};
const TEST_DIR: &str = ".test";
fn setup() -> Result<(), io::Error> {
create_dir(TEST_DIR)?;
env::set_current_dir(TEST_DIR)?;
fn setup(suffix: &str) -> Result<(), io::Error> {
let test_dir = format!("{}_{}", TEST_DIR, suffix);
create_dir(&test_dir)?;
Ok(())
}
fn clean() -> Result<(), io::Error> {
remove_file(&format!("{}/{}", LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE))?;
remove_dir(LEO_CREDENTIALS_DIR)?;
env::set_current_dir("../")?;
remove_dir(TEST_DIR)?;
fn clean(suffix: &str) -> Result<(), io::Error> {
let test_dir = format!("{}_{}", TEST_DIR, suffix);
remove_file(&format!(
"{}/{}/{}",
test_dir, LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE
))?;
remove_dir(&format!("{}/{}", test_dir, LEO_CREDENTIALS_DIR))?;
remove_dir(test_dir)?;
Ok(())
}
fn get_token() -> Result<String, io::Error> {
let mut credentials = File::open(&format!("{}/{}", LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE))?;
fn get_token(suffix: &str) -> Result<String, io::Error> {
let test_dir = format!("{}_{}", TEST_DIR, suffix);
let mut credentials = File::open(&format!(
"{}/{}/{}",
test_dir, LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE
))?;
let mut buf = String::new();
credentials.read_to_string(&mut buf)?;
Ok(buf)
}
fn create_token(token: &str) -> Result<(), io::Error> {
let mut f = File::create(&format!("{}/{}", LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE))?;
fn create_token(suffix: &str, token: &str) -> Result<(), io::Error> {
let test_dir = format!("{}_{}", TEST_DIR, suffix);
let mut f = File::create(&format!(
"{}/{}/{}",
test_dir, LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE
))?;
f.write_all(token.as_bytes())?;
Ok(())
}
#[test]
#[ignore]
fn test_credential_dir_exists() -> Result<(), io::Error> {
setup()?;
let suffix = "test1";
setup(suffix)?;
create_dir(LEO_CREDENTIALS_DIR)?;
let token = "SOME_TOKEN".to_string();
let options = Some(token.clone());
LoginCommand::output(options).unwrap();
assert_eq!(token, get_token()?);
clean()?;
assert_eq!(token, get_token(suffix)?);
clean(suffix)?;
Ok(())
}
#[test]
#[ignore]
fn test_credential_file_exists() -> Result<(), io::Error> {
setup()?;
let suffix = "test2";
setup(suffix)?;
create_dir(LEO_CREDENTIALS_DIR)?;
create_token("OLD_TOKEN")?;
create_token(suffix, "OLD_TOKEN")?;
let token = "NEW_TOKEN".to_string();
let options = Some(token.clone());
LoginCommand::output(options).unwrap();
assert_eq!(token, get_token()?);
clean()?;
assert_eq!(token, get_token(suffix)?);
clean(suffix)?;
Ok(())
}
#[test]
#[ignore]
fn test_credential_dir_does_not_exist() -> Result<(), io::Error> {
setup()?;
let suffix = "test3";
setup(suffix)?;
let token = "SOME_TOKEN".to_string();
let options = Some(token.clone());
LoginCommand::output(options).unwrap();
assert_eq!(token, get_token()?);
clean()?;
assert_eq!(token, get_token(suffix)?);
clean(suffix)?;
Ok(())
}
#[test]
#[ignore]
fn test_credential_file_does_not_exist() -> Result<(), io::Error> {
setup()?;
let suffix = "test4";
setup(suffix)?;
create_dir(LEO_CREDENTIALS_DIR)?;
let token = "SOME_TOKEN".to_string();
let options = Some(token.clone());
LoginCommand::output(options).unwrap();
assert_eq!(token, get_token()?);
clean()?;
assert_eq!(token, get_token(suffix)?);
clean(suffix)?;
Ok(())
}
}