Skip to content

Commit 24f8778

Browse files
committed
Improve the generation of unique filenames used in unit tests
1 parent ad25933 commit 24f8778

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/auth_file_watcher.test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn when_monitored_file_does_not_exist_then_new_does_not_return_error() {
2121

2222
#[test]
2323
fn when_new_line_is_added_to_file_then_update_callback_is_called() {
24-
let mut file = TestFile::new("auth-monitor-test");
24+
let mut file = TestFile::with_unique_name();
2525
let mut auth_file_watcher =
2626
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
2727
expect_no_update_callback_call(&mut auth_file_watcher);
@@ -47,7 +47,7 @@ fn expect_update_callback_is_called_when_file_is_modified(
4747

4848
#[test]
4949
fn when_more_than_one_line_is_added_then_update_callback_is_called_for_each_line() {
50-
let mut file = TestFile::new("auth-monitor-test");
50+
let mut file = TestFile::with_unique_name();
5151
let mut auth_file_watcher =
5252
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
5353
expect_no_update_callback_call(&mut auth_file_watcher);
@@ -73,7 +73,7 @@ fn when_more_than_one_line_is_added_then_update_callback_is_called_for_each_line
7373

7474
#[test]
7575
fn when_new_file_was_created_after_old_was_deleted_then_changes_in_new_file_are_monitored() {
76-
let mut file = TestFile::new("auth-monitor-test");
76+
let mut file = TestFile::with_unique_name();
7777
let mut auth_file_watcher =
7878
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
7979
expect_no_update_callback_call(&mut auth_file_watcher);
@@ -95,7 +95,7 @@ fn expect_no_update_callback_call(auth_file_watcher: &mut AuthFileWatcher) {
9595

9696
#[test]
9797
fn when_new_file_has_been_created_after_old_was_renamed_then_changes_in_new_file_are_monitored() {
98-
let mut file = TestFile::new("auth-monitor-test");
98+
let mut file = TestFile::with_unique_name();
9999
let mut auth_file_watcher =
100100
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
101101
expect_no_update_callback_call(&mut auth_file_watcher);
@@ -111,7 +111,7 @@ fn when_new_file_has_been_created_after_old_was_renamed_then_changes_in_new_file
111111

112112
#[test]
113113
fn when_monitored_file_has_been_truncated_then_changes_are_still_monitored() {
114-
let mut file = TestFile::new("auth-monitor-test");
114+
let mut file = TestFile::with_unique_name();
115115
let mut auth_file_watcher =
116116
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
117117
expect_no_update_callback_call(&mut auth_file_watcher);

src/auth_monitor.test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const OTHER_TEST_MESSAGES: [&str; 4] = [
2121
#[test]
2222
pub fn when_max_failed_attempts_limit_is_reached_then_update_callback_is_called() {
2323
for max_failed_attempts in 2..10 {
24-
let mut file = TestFile::new("auth-monitor");
24+
let mut file = TestFile::with_unique_name();
2525
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
2626
filepath: file.filepath.clone(),
2727
options: AuthMonitorOptions {
@@ -101,7 +101,7 @@ pub fn when_reset_after_seconds_passed_then_failed_attempts_count_is_reset() {
101101
max_failed_attempts: 3,
102102
reset_after_seconds: 5,
103103
};
104-
let mut file = TestFile::new("auth-monitor");
104+
let mut file = TestFile::with_unique_name();
105105
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
106106
filepath: file.filepath.clone(),
107107
options,
@@ -135,7 +135,7 @@ pub fn when_reset_after_seconds_passed_then_failed_attempts_count_is_reset() {
135135
pub fn when_max_failed_attempts_limit_is_reached_before_update_is_called_then_update_callback_is_called(
136136
) {
137137
for max_failed_attempts in 2..10 {
138-
let mut file = TestFile::new("auth-monitor");
138+
let mut file = TestFile::with_unique_name();
139139
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
140140
filepath: file.filepath.clone(),
141141
options: AuthMonitorOptions {

src/test_utils/test_file.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env::temp_dir;
22
use std::fs::{remove_file, rename, File};
33
use std::io::Write;
44
use std::path::Path;
5+
use std::sync::atomic::{AtomicUsize, Ordering};
56

67
use chrono::Local;
78

@@ -11,14 +12,27 @@ pub struct TestFile {
1112
}
1213

1314
impl TestFile {
14-
pub fn new(prefix: &str) -> TestFile {
15-
let filename = format!("{}-{}.log", prefix, Local::now().timestamp_micros());
15+
pub fn with_unique_name() -> TestFile {
16+
let filename = format!(
17+
"auth-monitor-test-{}-{}.log",
18+
Self::next_id(),
19+
Local::now().timestamp_micros()
20+
);
1621
let filepath_buffer = temp_dir().join(filename);
1722
let filepath = filepath_buffer.to_str().expect("Error creating filepath");
18-
println!("Creating test file: {}", filepath);
23+
return Self::new(filepath);
24+
}
25+
26+
fn next_id() -> usize {
27+
static ID: AtomicUsize = AtomicUsize::new(1);
28+
return ID.fetch_add(1, Ordering::Relaxed);
29+
}
30+
31+
pub fn new(path: &str) -> TestFile {
32+
println!("Creating test file: {}", path);
1933
return TestFile {
20-
filepath: String::from(filepath),
21-
file: File::create(filepath).expect("Error creating test file"),
34+
filepath: String::from(path),
35+
file: File::create(path).expect("Error creating test file"),
2236
};
2337
}
2438

0 commit comments

Comments
 (0)