use anyhow::Context;
use std::fs;
use std::fs::OpenOptions;
use std::path::Path;
pub fn create_directory_and_file(file_path: &str) -> anyhow::Result<()> {
let path = Path::new(file_path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).context("Failed to create directory")?;
}
OpenOptions::new()
.write(true)
.create(true) .truncate(false)
.open(file_path)
.context("Failed to create or open the file")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::Path;
use tempfile::tempdir;
#[test]
fn test_create_directory_and_file() {
let temp_dir = tempdir().unwrap();
let test_dir = temp_dir.path().join("test_data");
let test_file = test_dir.join("test_file.txt");
let _ = fs::remove_dir_all(&test_dir);
let result = create_directory_and_file(test_file.to_str().unwrap());
assert!(result.is_ok());
assert!(Path::new(&test_dir).exists());
assert!(Path::new(&test_file).exists());
}
}