Skip to content

Commit

Permalink
test: add simple test for file log
Browse files Browse the repository at this point in the history
  • Loading branch information
FreerGit committed Sep 5, 2024
1 parent 5e129ec commit dd0a56f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl RingBuffer {
}
}

#[derive(Clone)]
pub struct LoggerFileOptions {
pub file_path: String,
pub append_mode: bool,
Expand Down Expand Up @@ -208,3 +209,32 @@ impl Logger {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;

fn setup() {
fs::File::options()
.read(true)
.write(true)
.create(true)
.open("log.txt")
.unwrap();
}

#[test]
fn simple_to_file() {
setup();
let o = LoggerFileOptions {
file_path: "log.txt".to_owned(),
append_mode: false,
};
let logger = Logger::new(1024, Some(o.clone()));
logger.log_f(|| "to file".to_owned());
logger.shutdown();
let bytes = fs::read(o.file_path).unwrap();
assert_eq!(String::from_utf8(bytes).unwrap(), "to file\n".to_owned());
}
}

0 comments on commit dd0a56f

Please sign in to comment.