use std::fs;
use std::path::Path;
#[cfg(test)]
use std::fs::File;
#[cfg(test)]
use std::io::Write;
pub fn get_torrent_file_list(file_path: &str) -> Vec<String> {
let files_dir = Path::new(file_path);
if !files_dir.exists() {
fs::create_dir("files").expect("Failed to create files directory");
return vec![];
}
fs::read_dir(files_dir)
.expect("Failed to read directory")
.filter_map(|entry| {
let entry = entry.ok()?;
let file_path = entry.path();
if file_path.extension()? == "torrent" {
Some(file_path.to_string_lossy().into_owned())
} else {
None
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
fn create_test_dir(dir: &str) {
fs::create_dir_all(dir).expect("Failed to create test directory");
}
fn create_test_file(path: &str) {
let mut file = File::create(path).expect("Failed to create test file");
file.write_all(b"test content").expect("Failed to write to test file");
}
fn cleanup_test_dir(dir: &str) {
fs::remove_dir_all(dir).expect("Failed to clean up test directory");
}
#[test]
fn test_empty_directory() {
let test_dir = "test_empty";
create_test_dir(test_dir);
let files = get_torrent_file_list(test_dir);
assert!(files.is_empty());
cleanup_test_dir(test_dir);
}
#[test]
fn test_with_torrent_files() {
let test_dir = "test_torrents";
create_test_dir(test_dir);
create_test_file(&format!("{}/test1.torrent", test_dir));
create_test_file(&format!("{}/test2.torrent", test_dir));
create_test_file(&format!("{}/not_torrent.txt", test_dir));
let files = get_torrent_file_list(test_dir);
assert_eq!(files.len(), 2);
assert!(files.iter().all(|f| f.ends_with(".torrent")));
cleanup_test_dir(test_dir);
}
#[test]
fn test_nonexistent_directory() {
let files = get_torrent_file_list("nonexistent_dir");
assert!(files.is_empty());
assert!(Path::new("files").exists());
fs::remove_dir("files").expect("Failed to clean up files directory");
}
}