Skip to content

Commit d922a26

Browse files
committed
Move TestFile to test_utils module
1 parent 9a3cbab commit d922a26

File tree

4 files changed

+59
-51
lines changed

4 files changed

+59
-51
lines changed

src/auth_file_watcher.test.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::env::temp_dir;
2-
use std::fs::File;
3-
use std::fs::{remove_file, rename};
4-
use std::io::Write;
2+
use std::fs::rename;
53
use std::path::Path;
64

75
use chrono::Local;
86

97
use crate::auth_file_watcher::AuthFileWatcher;
8+
use crate::test_utils::test_file::TestFile;
109

1110
const AUTH_FAILED_MESSAGES: [&str; 6] = [
1211
"workstation sudo: pam_unix(sudo:auth): authentication failure; logname=john uid=1000 euid=0 tty=/dev/pts/7 ruser=john rhost= user=john",
@@ -17,54 +16,6 @@ const AUTH_FAILED_MESSAGES: [&str; 6] = [
1716
"workstation PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0)",
1817
];
1918

20-
struct TestFile {
21-
pub filepath: String,
22-
file: File,
23-
}
24-
25-
impl TestFile {
26-
pub fn new(prefix: &str) -> TestFile {
27-
let filename = format!("{}-{}.log", prefix, Local::now().timestamp_micros());
28-
let filepath_buffer = temp_dir().join(filename);
29-
let filepath = filepath_buffer.to_str().expect("Error creating filepath");
30-
println!("Creating test file: {}", filepath);
31-
return TestFile {
32-
filepath: String::from(filepath),
33-
file: File::create(filepath).expect("Error creating test file"),
34-
};
35-
}
36-
37-
pub fn create(&mut self) {
38-
println!("Creating test file: {}", &self.filepath);
39-
self.file = File::create(&self.filepath).expect("Error creating test file");
40-
}
41-
42-
pub fn write(&mut self, message: &str) {
43-
let bytes_to_add = message.as_bytes();
44-
let bytes_written = self
45-
.file
46-
.write(bytes_to_add)
47-
.expect("Error writing to file");
48-
assert_eq!(bytes_written, bytes_to_add.len());
49-
}
50-
51-
pub fn truncate(&mut self) {
52-
println!("Truncating test file: {}", self.filepath);
53-
self.file.set_len(0).expect("Error truncating file");
54-
}
55-
56-
pub fn remove(&mut self) {
57-
println!("Removing test file: {}", self.filepath);
58-
remove_file(&self.filepath).expect("Unable to remove test file");
59-
}
60-
}
61-
62-
impl Drop for TestFile {
63-
fn drop(&mut self) {
64-
self.remove();
65-
}
66-
}
67-
6819
#[test]
6920
fn when_monitored_file_does_not_exist_then_new_does_not_return_error() {
7021
let filepath_buffer = temp_dir().join("auth-monitor-non-existing-file.log");

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ mod auth_monitor_params;
2020
mod file_event_filter;
2121
mod file_path;
2222

23+
#[cfg(test)]
24+
mod test_utils;
25+
2326
const SLEEP_DURATION: Duration = Duration::from_millis(500);
2427

2528
fn main() -> ExitCode {

src/test_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod test_file;

src/test_utils/test_file.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::env::temp_dir;
2+
use std::fs::{remove_file, File};
3+
use std::io::Write;
4+
5+
use chrono::Local;
6+
7+
pub struct TestFile {
8+
pub filepath: String,
9+
file: File,
10+
}
11+
12+
impl TestFile {
13+
pub fn new(prefix: &str) -> TestFile {
14+
let filename = format!("{}-{}.log", prefix, Local::now().timestamp_micros());
15+
let filepath_buffer = temp_dir().join(filename);
16+
let filepath = filepath_buffer.to_str().expect("Error creating filepath");
17+
println!("Creating test file: {}", filepath);
18+
return TestFile {
19+
filepath: String::from(filepath),
20+
file: File::create(filepath).expect("Error creating test file"),
21+
};
22+
}
23+
24+
pub fn create(&mut self) {
25+
println!("Creating test file: {}", &self.filepath);
26+
self.file = File::create(&self.filepath).expect("Error creating test file");
27+
}
28+
29+
pub fn write(&mut self, message: &str) {
30+
let bytes_to_add = message.as_bytes();
31+
let bytes_written = self
32+
.file
33+
.write(bytes_to_add)
34+
.expect("Error writing to file");
35+
assert_eq!(bytes_written, bytes_to_add.len());
36+
}
37+
38+
pub fn truncate(&mut self) {
39+
println!("Truncating test file: {}", self.filepath);
40+
self.file.set_len(0).expect("Error truncating file");
41+
}
42+
43+
pub fn remove(&mut self) {
44+
println!("Removing test file: {}", self.filepath);
45+
remove_file(&self.filepath).expect("Unable to remove test file");
46+
}
47+
}
48+
49+
impl Drop for TestFile {
50+
fn drop(&mut self) {
51+
self.remove();
52+
}
53+
}

0 commit comments

Comments
 (0)