1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
Avoid tests creating log files in the crate directory.
for the main test this is done by using the tempfile crate to create a temporary directory.
a couple of documentation examples are also marked as no-run to stop them running as doctests
and creating log files.
Index: simplelog/Cargo.toml
===================================================================
--- simplelog.orig/Cargo.toml
+++ simplelog/Cargo.toml
@@ -53,6 +53,9 @@ features = [
"macros",
]
+[dev-dependencies.tempfile]
+version = "3"
+
[features]
default = [
"termcolor",
Index: simplelog/src/lib.rs
===================================================================
--- simplelog.orig/src/lib.rs
+++ simplelog/src/lib.rs
@@ -101,7 +101,7 @@ mod tests {
#[test]
fn test() {
let mut i = 0;
-
+ let tempdir = tempfile::tempdir().unwrap();
CombinedLogger::init({
let mut vec = Vec::new();
let mut conf_builder = ConfigBuilder::new();
@@ -115,7 +115,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Error,
conf_thread_name,
- File::create("thread_naming.log").unwrap(),
+ File::create(tempdir.path().join("thread_naming.log")).unwrap(),
) as Box<dyn SharedLogger>);
for elem in vec![
@@ -148,7 +148,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Error,
conf.clone(),
- File::create(&format!("error_{}.log", i)).unwrap(),
+ File::create(tempdir.path().join(&format!("error_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Error, conf.clone()));
@@ -167,7 +167,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Warn,
conf.clone(),
- File::create(&format!("warn_{}.log", i)).unwrap(),
+ File::create(tempdir.path().join(&format!("warn_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Warn, conf.clone()));
@@ -186,7 +186,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Info,
conf.clone(),
- File::create(&format!("info_{}.log", i)).unwrap(),
+ File::create(tempdir.path().join(&format!("info_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Info, conf.clone()));
@@ -205,7 +205,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Debug,
conf.clone(),
- File::create(&format!("debug_{}.log", i)).unwrap(),
+ File::create(tempdir.path().join(&format!("debug_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Debug, conf.clone()));
@@ -224,7 +224,7 @@ mod tests {
vec.push(WriteLogger::new(
LevelFilter::Trace,
conf.clone(),
- File::create(&format!("trace_{}.log", i)).unwrap(),
+ File::create(tempdir.path().join(&format!("trace_{}.log", i))).unwrap(),
) as Box<dyn SharedLogger>);
#[cfg(feature = "test")]
vec.push(TestLogger::new(LevelFilter::Trace, conf.clone()));
@@ -241,7 +241,7 @@ mod tests {
trace!("Test Trace");
let mut thread_naming = String::new();
- File::open("thread_naming.log")
+ File::open(tempdir.path().join("thread_naming.log"))
.unwrap()
.read_to_string(&mut thread_naming)
.unwrap();
@@ -252,27 +252,27 @@ mod tests {
for j in 1..i {
let mut error = String::new();
- File::open(&format!("error_{}.log", j))
+ File::open(tempdir.path().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(tempdir.path().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(tempdir.path().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(tempdir.path().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(tempdir.path().join(&format!("trace_{}.log", j)))
.unwrap()
.read_to_string(&mut trace)
.unwrap();
Index: simplelog/src/loggers/comblog.rs
===================================================================
--- simplelog.orig/src/loggers/comblog.rs
+++ simplelog/src/loggers/comblog.rs
@@ -29,7 +29,7 @@ impl CombinedLogger {
/// Fails if another logger is already set globally.
///
/// # Examples
- /// ```
+ /// ``` no-run
/// # extern crate simplelog;
/// # use simplelog::*;
/// # use std::fs::File;
@@ -60,7 +60,7 @@ impl CombinedLogger {
/// All loggers need to implement log::Log.
///
/// # Examples
- /// ```
+ /// ``` no-run
/// # extern crate simplelog;
/// # use simplelog::*;
/// # use std::fs::File;
Index: simplelog/src/loggers/writelog.rs
===================================================================
--- simplelog.orig/src/loggers/writelog.rs
+++ simplelog/src/loggers/writelog.rs
@@ -27,7 +27,7 @@ impl<W: Write + Send + 'static> WriteLog
/// Fails if another Logger was already initialized.
///
/// # Examples
- /// ```
+ /// ``` no-run
/// # extern crate simplelog;
/// # use simplelog::*;
/// # use std::fs::File;
@@ -48,7 +48,7 @@ impl<W: Write + Send + 'static> WriteLog
/// Takes the desired `Level`, `Config` and `Write` struct as arguments. They cannot be changed later on.
///
/// # Examples
- /// ```
+ /// ``` no-run
/// # extern crate simplelog;
/// # use simplelog::*;
/// # use std::fs::File;
|