Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching lib.rs tests mod to use std::env::temp_dir and create test files in temp folder. #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/lib.rs
Expand Up @@ -92,12 +92,14 @@ pub trait SharedLogger: Log {
mod tests {
use std::fs::File;
use std::io::Read;
use std::env::{temp_dir};

use super::*;

#[test]
fn test() {
let mut i = 0;
let tmpdir = temp_dir();

CombinedLogger::init({
let mut vec = Vec::new();
Expand All @@ -112,7 +114,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Error,
conf_thread_name,
File::create("thread_naming.log").unwrap(),
File::create(tmpdir.join("thread_naming.log")).unwrap(),
) as Box<dyn SharedLogger>);

for elem in vec![
Expand Down Expand Up @@ -145,7 +147,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Error,
conf.clone(),
File::create(&format!("error_{}.log", i)).unwrap(),
File::create(tmpdir.join(&format!("error_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Error, conf.clone()));
Expand All @@ -164,7 +166,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Warn,
conf.clone(),
File::create(&format!("warn_{}.log", i)).unwrap(),
File::create(tmpdir.join(&format!("warn_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Warn, conf.clone()));
Expand All @@ -183,7 +185,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Info,
conf.clone(),
File::create(&format!("info_{}.log", i)).unwrap(),
File::create(tmpdir.join(&format!("info_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Info, conf.clone()));
Expand All @@ -202,7 +204,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Debug,
conf.clone(),
File::create(&format!("debug_{}.log", i)).unwrap(),
File::create(tmpdir.join(&format!("debug_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Debug, conf.clone()));
Expand All @@ -221,7 +223,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Trace,
conf.clone(),
File::create(&format!("trace_{}.log", i)).unwrap(),
File::create(tmpdir.join(&format!("trace_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Trace, conf.clone()));
Expand All @@ -238,7 +240,7 @@ mod tests {
trace!("Test Trace");

let mut thread_naming = String::new();
File::open("thread_naming.log")
File::open(tmpdir.join("thread_naming.log"))
.unwrap()
.read_to_string(&mut thread_naming)
.unwrap();
Expand All @@ -249,27 +251,27 @@ mod tests {

for j in 1..i {
let mut error = String::new();
File::open(&format!("error_{}.log", j))
File::open(tmpdir.join(&format!("error_{}.log", j)))
.unwrap()
.read_to_string(&mut error)
.unwrap();
let mut warn = String::new();
File::open(&format!("warn_{}.log", j))
File::open(tmpdir.join(&format!("warn_{}.log", j)))
.unwrap()
.read_to_string(&mut warn)
.unwrap();
let mut info = String::new();
File::open(&format!("info_{}.log", j))
File::open(tmpdir.join(&format!("info_{}.log", j)))
.unwrap()
.read_to_string(&mut info)
.unwrap();
let mut debug = String::new();
File::open(&format!("debug_{}.log", j))
File::open(tmpdir.join(&format!("debug_{}.log", j)))
.unwrap()
.read_to_string(&mut debug)
.unwrap();
let mut trace = String::new();
File::open(&format!("trace_{}.log", j))
File::open(tmpdir.join(&format!("trace_{}.log", j)))
.unwrap()
.read_to_string(&mut trace)
.unwrap();
Expand Down