k8s_test_framework/
temp_file.rs

1use std::path::{Path, PathBuf};
2
3use tempfile::tempdir;
4
5#[derive(Debug)]
6pub struct TempFile {
7    path: PathBuf,
8}
9
10impl TempFile {
11    pub fn new(file_name: &str, data: &str) -> std::io::Result<Self> {
12        let dir = tempdir()?.keep();
13        let path = dir.join(file_name);
14        std::fs::write(&path, data)?;
15        Ok(Self { path })
16    }
17
18    pub fn path(&self) -> &Path {
19        self.path.as_path()
20    }
21}
22
23impl Drop for TempFile {
24    fn drop(&mut self) {
25        if let Some(dir) = self.path.parent() {
26            _ = std::fs::remove_dir_all(dir);
27        }
28    }
29}