Skip to content

Commit 4018edd

Browse files
committed
Make the file path field private in TestFile
1 parent 24f8778 commit 4018edd

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/auth_file_watcher.test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn when_monitored_file_does_not_exist_then_new_does_not_return_error() {
2323
fn when_new_line_is_added_to_file_then_update_callback_is_called() {
2424
let mut file = TestFile::with_unique_name();
2525
let mut auth_file_watcher =
26-
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
26+
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
2727
expect_no_update_callback_call(&mut auth_file_watcher);
2828
expect_update_callback_is_called_when_file_is_modified(&mut file, &mut auth_file_watcher);
2929
}
@@ -49,7 +49,7 @@ fn expect_update_callback_is_called_when_file_is_modified(
4949
fn when_more_than_one_line_is_added_then_update_callback_is_called_for_each_line() {
5050
let mut file = TestFile::with_unique_name();
5151
let mut auth_file_watcher =
52-
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
52+
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
5353
expect_no_update_callback_call(&mut auth_file_watcher);
5454

5555
let mut call_count = 0;
@@ -75,7 +75,7 @@ fn when_more_than_one_line_is_added_then_update_callback_is_called_for_each_line
7575
fn when_new_file_was_created_after_old_was_deleted_then_changes_in_new_file_are_monitored() {
7676
let mut file = TestFile::with_unique_name();
7777
let mut auth_file_watcher =
78-
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
78+
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
7979
expect_no_update_callback_call(&mut auth_file_watcher);
8080

8181
file.remove();
@@ -97,10 +97,10 @@ fn expect_no_update_callback_call(auth_file_watcher: &mut AuthFileWatcher) {
9797
fn when_new_file_has_been_created_after_old_was_renamed_then_changes_in_new_file_are_monitored() {
9898
let mut file = TestFile::with_unique_name();
9999
let mut auth_file_watcher =
100-
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
100+
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
101101
expect_no_update_callback_call(&mut auth_file_watcher);
102102

103-
rename_file(&file.filepath, "auth-monitor-test.bak");
103+
rename_file(file.path(), "auth-monitor-test.bak");
104104
expect_no_update_callback_call(&mut auth_file_watcher);
105105

106106
file.create();
@@ -113,7 +113,7 @@ fn when_new_file_has_been_created_after_old_was_renamed_then_changes_in_new_file
113113
fn when_monitored_file_has_been_truncated_then_changes_are_still_monitored() {
114114
let mut file = TestFile::with_unique_name();
115115
let mut auth_file_watcher =
116-
AuthFileWatcher::new(&file.filepath).expect("Error creating AuthFileWatcher");
116+
AuthFileWatcher::new(file.path()).expect("Error creating AuthFileWatcher");
117117
expect_no_update_callback_call(&mut auth_file_watcher);
118118

119119
file.truncate();

src/auth_monitor.test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn when_max_failed_attempts_limit_is_reached_then_update_callback_is_called(
2323
for max_failed_attempts in 2..10 {
2424
let mut file = TestFile::with_unique_name();
2525
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
26-
filepath: file.filepath.clone(),
26+
filepath: String::from(file.path()),
2727
options: AuthMonitorOptions {
2828
max_failed_attempts,
2929
..AuthMonitorOptions::default()
@@ -103,7 +103,7 @@ pub fn when_reset_after_seconds_passed_then_failed_attempts_count_is_reset() {
103103
};
104104
let mut file = TestFile::with_unique_name();
105105
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
106-
filepath: file.filepath.clone(),
106+
filepath: String::from(file.path()),
107107
options,
108108
})
109109
.expect("Error creating AuthMonitor");
@@ -137,7 +137,7 @@ pub fn when_max_failed_attempts_limit_is_reached_before_update_is_called_then_up
137137
for max_failed_attempts in 2..10 {
138138
let mut file = TestFile::with_unique_name();
139139
let mut auth_monitor = AuthMonitor::new(AuthMonitorParams {
140-
filepath: file.filepath.clone(),
140+
filepath: String::from(file.path()),
141141
options: AuthMonitorOptions {
142142
max_failed_attempts,
143143
..AuthMonitorOptions::default()

src/test_utils/test_file.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
77
use chrono::Local;
88

99
pub struct TestFile {
10-
pub filepath: String,
10+
path: String,
1111
file: File,
1212
}
1313

@@ -18,9 +18,9 @@ impl TestFile {
1818
Self::next_id(),
1919
Local::now().timestamp_micros()
2020
);
21-
let filepath_buffer = temp_dir().join(filename);
22-
let filepath = filepath_buffer.to_str().expect("Error creating filepath");
23-
return Self::new(filepath);
21+
let path_buffer = temp_dir().join(filename);
22+
let path = path_buffer.to_str().expect("Error creating file path");
23+
return Self::new(path);
2424
}
2525

2626
fn next_id() -> usize {
@@ -31,14 +31,18 @@ impl TestFile {
3131
pub fn new(path: &str) -> TestFile {
3232
println!("Creating test file: {}", path);
3333
return TestFile {
34-
filepath: String::from(path),
34+
path: String::from(path),
3535
file: File::create(path).expect("Error creating test file"),
3636
};
3737
}
3838

39+
pub fn path(&self) -> &str {
40+
return &self.path;
41+
}
42+
3943
pub fn create(&mut self) {
40-
println!("Creating test file: {}", &self.filepath);
41-
self.file = File::create(&self.filepath).expect("Error creating test file");
44+
println!("Creating test file: {}", &self.path);
45+
self.file = File::create(&self.path).expect("Error creating test file");
4246
}
4347

4448
pub fn write(&mut self, message: &str) {
@@ -51,13 +55,13 @@ impl TestFile {
5155
}
5256

5357
pub fn truncate(&mut self) {
54-
println!("Truncating test file: {}", self.filepath);
58+
println!("Truncating test file: {}", self.path);
5559
self.file.set_len(0).expect("Error truncating file");
5660
}
5761

5862
pub fn remove(&mut self) {
59-
println!("Removing test file: {}", self.filepath);
60-
remove_file(&self.filepath).expect("Unable to remove test file");
63+
println!("Removing test file: {}", self.path);
64+
remove_file(&self.path).expect("Unable to remove test file");
6165
}
6266
}
6367

0 commit comments

Comments
 (0)