#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::AsyncWriteExt;
#[tokio::test]
async fn test_worker_temp_file_creation() {
let temp_file = WorkerTempFile::new(1, 2, None);
assert!(temp_file
.path()
.file_name()
.unwrap()
.to_string_lossy()
.contains("pmat_w1_2.rs"));
}
#[tokio::test]
async fn test_worker_temp_file_with_custom_extension() {
let temp_file = WorkerTempFile::new(1, 2, Some("txt"));
assert!(temp_file
.path()
.file_name()
.unwrap()
.to_string_lossy()
.contains("pmat_w1_2.txt"));
}
#[tokio::test]
async fn test_worker_temp_file_with_path() {
let path = std::env::temp_dir().join("custom_temp_file.txt");
let temp_file = WorkerTempFile::with_path(path.clone());
assert_eq!(temp_file.path(), &path);
}
#[tokio::test]
async fn test_worker_temp_file_write_read() -> Result<()> {
let temp_file = WorkerTempFile::new(3, 4, None);
temp_file.write("test content").await?;
assert!(temp_file.exists().await);
let content = temp_file.read_to_string().await?;
assert_eq!(content, "test content");
let mut temp_file = temp_file;
temp_file.cleanup().await?;
assert!(!temp_file.exists().await);
Ok(())
}
#[tokio::test]
async fn test_worker_temp_file_copy_to() -> Result<()> {
let temp_file = WorkerTempFile::new(5, 6, None);
temp_file.write("test content").await?;
let dest_file = std::env::temp_dir().join("pmat_copy_test.txt");
temp_file.copy_to(&dest_file).await?;
assert!(fs::try_exists(&dest_file).await?);
let content = fs::read_to_string(&dest_file).await?;
assert_eq!(content, "test content");
let mut temp_file = temp_file;
temp_file.cleanup().await?;
fs::remove_file(&dest_file).await?;
Ok(())
}
#[tokio::test]
async fn test_temp_dir_creation() -> Result<()> {
let temp_dir = TempDir::new("pmat_test").await?;
assert!(fs::try_exists(temp_dir.path()).await?);
let mut temp_dir = temp_dir;
temp_dir.cleanup().await?;
assert!(!fs::try_exists(temp_dir.path()).await.unwrap_or(false));
Ok(())
}
#[tokio::test]
async fn test_temp_dir_child_paths() -> Result<()> {
let temp_dir = TempDir::new("pmat_test").await?;
let child = temp_dir.child("test.txt");
let mut file = tokio::fs::File::create(&child).await?;
file.write_all(b"test content").await?;
file.flush().await?;
assert!(fs::try_exists(&child).await?);
let content = fs::read_to_string(&child).await?;
assert_eq!(content, "test content");
let mut temp_dir = temp_dir;
temp_dir.cleanup().await?;
assert!(!fs::try_exists(temp_dir.path()).await.unwrap_or(false));
assert!(!fs::try_exists(&child).await.unwrap_or(false));
Ok(())
}
#[tokio::test]
async fn test_mark_cleaned_up() -> Result<()> {
let mut temp_file = WorkerTempFile::new(7, 8, None);
temp_file.write("test content").await?;
temp_file.mark_cleaned_up();
assert!(fs::try_exists(temp_file.path()).await?);
drop(temp_file);
let path = std::env::temp_dir().join("pmat_w7_8.rs");
if fs::try_exists(&path).await? {
fs::remove_file(&path).await?;
}
let mut temp_dir = TempDir::new("pmat_test").await?;
let dir_path = temp_dir.path().to_path_buf();
temp_dir.mark_cleaned_up();
assert!(fs::try_exists(&dir_path).await?);
drop(temp_dir);
if fs::try_exists(&dir_path).await? {
fs::remove_dir_all(&dir_path).await?;
}
Ok(())
}
}