Skip to content
This repository was archived by the owner on May 19, 2026. It is now read-only.

Commit 646d52e

Browse files
committed
Add tests for history file filename
1 parent 48ee1ec commit 646d52e

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/history_file.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::io;
2020
use std::io::{Read, Write};
2121
use std::path::{Path, PathBuf};
2222

23+
#[derive(Debug)]
2324
pub(crate) struct HistoryFile {
2425
pub(crate) path: String,
2526
pub(crate) filename: String,
@@ -151,23 +152,37 @@ mod tests {
151152
fn test_new_creates_file_if_not_exists() {
152153
let temp_path = NamedTempFile::new().unwrap();
153154
let path = temp_path.path().to_str().unwrap().to_string();
155+
let expected_filename = temp_path
156+
.path()
157+
.file_name()
158+
.unwrap()
159+
.to_string_lossy()
160+
.into_owned();
154161
temp_path.close().unwrap(); // Delete the file
155162

156163
let history_file = HistoryFile::new(path.clone(), String::new()).unwrap();
157164

158165
assert!(fs::metadata(&path).is_ok()); // File exists
159166
assert_eq!(history_file.get_content(), ""); // Empty content
167+
assert_eq!(history_file.filename, expected_filename); // Filename is extracted correctly
160168
}
161169

162170
#[test]
163171
fn test_new_reads_existing_content() {
164172
let content = "Existing content";
165173
let temp_file = create_temp_file_with_content(content);
166174
let path = temp_file.path().to_str().unwrap().to_string();
175+
let expected_filename = temp_file
176+
.path()
177+
.file_name()
178+
.unwrap()
179+
.to_string_lossy()
180+
.into_owned();
167181

168182
let history_file = HistoryFile::new(path, String::new()).unwrap();
169183

170184
assert_eq!(history_file.get_content(), content);
185+
assert_eq!(history_file.filename, expected_filename);
171186
}
172187

173188
#[test]
@@ -299,6 +314,9 @@ mod tests {
299314
history_file.path,
300315
expected_path.to_string_lossy().to_string()
301316
);
317+
318+
// Verify the filename is extracted correctly
319+
assert_eq!(history_file.filename, "test_history.txt");
302320
}
303321

304322
#[test]
@@ -323,5 +341,26 @@ mod tests {
323341

324342
// Verify the path stored in the HistoryFile is the absolute path
325343
assert_eq!(history_file.path, absolute_path);
344+
345+
// Verify the filename is extracted correctly
346+
assert_eq!(history_file.filename, "absolute_history.txt");
347+
}
348+
349+
#[test]
350+
fn test_directory_path_handling() {
351+
// Create a temporary directory
352+
let temp_dir = tempfile::tempdir().unwrap();
353+
let dir_path = temp_dir.path().to_string_lossy().to_string();
354+
355+
// Attempt to create a history file with a directory path
356+
let result = HistoryFile::new(dir_path, String::new());
357+
358+
// Should result in an error, not a panic
359+
assert!(result.is_err());
360+
361+
// Just check that we get an error, without asserting on the specific error kind
362+
// since it can vary between operating systems
363+
let _error = result.unwrap_err();
364+
println!("Got expected error when opening directory: {:?}", _error);
326365
}
327366
}

0 commit comments

Comments
 (0)