pub fn list_dir_files(path: &str) -> Vec<String> {
let dir = match std::fs::read_dir(path) {
Ok(d) => d,
Err(_) => return Vec::new(),
};
let mut files: Vec<String> = dir
.filter_map(|entry| entry.ok())
.filter(|entry| {
let fname = entry.file_name();
let name = fname.to_string_lossy();
if name.starts_with('.') {
return false;
}
entry.file_type().map(|t| t.is_file()).unwrap_or(false)
})
.filter_map(|entry| entry.path().to_str().map(|s| s.to_string()))
.collect();
files.sort();
files
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_list_dir_files_basic() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
std::fs::write(dir.join("b.log"), b"b").unwrap();
std::fs::write(dir.join("a.log"), b"a").unwrap();
let files = list_dir_files(dir.to_str().unwrap());
assert_eq!(files.len(), 2);
assert!(files[0].ends_with("a.log"));
assert!(files[1].ends_with("b.log"));
}
#[test]
fn test_list_dir_files_excludes_hidden() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
std::fs::write(dir.join("visible.log"), b"v").unwrap();
std::fs::write(dir.join(".hidden"), b"h").unwrap();
let files = list_dir_files(dir.to_str().unwrap());
assert_eq!(files.len(), 1);
assert!(files[0].ends_with("visible.log"));
}
#[test]
fn test_list_dir_files_excludes_subdirs() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
std::fs::write(dir.join("file.log"), b"f").unwrap();
std::fs::create_dir(dir.join("subdir")).unwrap();
let files = list_dir_files(dir.to_str().unwrap());
assert_eq!(files.len(), 1);
assert!(files[0].ends_with("file.log"));
}
#[test]
fn test_list_dir_files_empty_dir() {
let tmp = tempfile::tempdir().unwrap();
let files = list_dir_files(tmp.path().to_str().unwrap());
assert!(files.is_empty());
}
#[test]
fn test_list_dir_files_nonexistent() {
let files = list_dir_files("/nonexistent/path/xyz123");
assert!(files.is_empty());
}
}