Skip to content

Commit dfe7a22

Browse files
committed
Improve file rename test in AuthFileWatcher unit tests
1 parent 4018edd commit dfe7a22

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

src/auth_file_watcher.test.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env::temp_dir;
22

33
use crate::auth_file_watcher::AuthFileWatcher;
4-
use crate::test_utils::test_file::{create_log_line, rename_file, TestFile};
4+
use crate::test_utils::test_file::{create_log_line, TestFile};
55

66
const TEST_MESSAGES: [&str; 6] = [
77
"workstation sudo: pam_unix(sudo:auth): authentication failure; logname=john uid=1000 euid=0 tty=/dev/pts/7 ruser=john rhost= user=john",
@@ -83,7 +83,6 @@ fn when_new_file_was_created_after_old_was_deleted_then_changes_in_new_file_are_
8383

8484
file.create();
8585
expect_no_update_callback_call(&mut auth_file_watcher);
86-
8786
expect_update_callback_is_called_when_file_is_modified(&mut file, &mut auth_file_watcher);
8887
}
8988

@@ -96,17 +95,18 @@ fn expect_no_update_callback_call(auth_file_watcher: &mut AuthFileWatcher) {
9695
#[test]
9796
fn when_new_file_has_been_created_after_old_was_renamed_then_changes_in_new_file_are_monitored() {
9897
let mut file = TestFile::with_unique_name();
98+
let filepath = String::from(file.path());
9999
let mut auth_file_watcher =
100-
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
100+
AuthFileWatcher::new(&filepath).expect("Error creating AuthFileWatcher");
101101
expect_no_update_callback_call(&mut auth_file_watcher);
102102

103-
rename_file(file.path(), "auth-monitor-test.bak");
103+
let new_filepath = format!("{}.bak", file.path());
104+
file.rename(&new_filepath);
104105
expect_no_update_callback_call(&mut auth_file_watcher);
105106

106-
file.create();
107+
let mut new_file = TestFile::new(&filepath);
107108
expect_no_update_callback_call(&mut auth_file_watcher);
108-
109-
expect_update_callback_is_called_when_file_is_modified(&mut file, &mut auth_file_watcher);
109+
expect_update_callback_is_called_when_file_is_modified(&mut new_file, &mut auth_file_watcher);
110110
}
111111

112112
#[test]
@@ -118,6 +118,5 @@ fn when_monitored_file_has_been_truncated_then_changes_are_still_monitored() {
118118

119119
file.truncate();
120120
expect_no_update_callback_call(&mut auth_file_watcher);
121-
122121
expect_update_callback_is_called_when_file_is_modified(&mut file, &mut auth_file_watcher);
123122
}

src/test_utils/test_file.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::env::temp_dir;
22
use std::fs::{remove_file, rename, File};
33
use std::io::Write;
4-
use std::path::Path;
54
use std::sync::atomic::{AtomicUsize, Ordering};
65

76
use chrono::Local;
@@ -59,6 +58,12 @@ impl TestFile {
5958
self.file.set_len(0).expect("Error truncating file");
6059
}
6160

61+
pub fn rename(&mut self, new_path: &str) {
62+
println!("Renaming test file {} to {}", self.path, new_path);
63+
rename(&self.path, new_path).expect("Unable to rename test file");
64+
self.path = String::from(new_path);
65+
}
66+
6267
pub fn remove(&mut self) {
6368
println!("Removing test file: {}", self.path);
6469
remove_file(&self.path).expect("Unable to remove test file");
@@ -71,16 +76,6 @@ impl Drop for TestFile {
7176
}
7277
}
7378

74-
pub fn rename_file(filepath: &str, new_filename: &str) {
75-
println!("Renaming test file {} to {}", filepath, new_filename);
76-
let new_path = Path::new(&filepath)
77-
.parent()
78-
.expect("Unable to get directory")
79-
.join(new_filename);
80-
let new_filepath = new_path.to_str().expect("Unable to build file path");
81-
rename(filepath, new_filepath).expect("Unable to rename test file");
82-
}
83-
8479
pub fn create_log_line(message: &str) -> String {
8580
let date_time = Local::now().format("%+");
8681
return format!("{} {}\n", date_time, message);

0 commit comments

Comments
 (0)