|
| 1 | +use std::ffi; |
| 2 | +use std::path; |
| 3 | + |
| 4 | +use tempfile; |
| 5 | + |
| 6 | +use super::errors::*; |
| 7 | + |
| 8 | +/// A potential file in the filesystem that is automatically deleted when |
| 9 | +/// it goes out of scope. |
| 10 | +/// |
| 11 | +/// The [`NamedTempFile`] type creates a directory on the file system that |
| 12 | +/// is deleted once it goes out of scope. At construction, the |
| 13 | +/// `NamedTempFile` creates a new directory with a randomly generated name. |
| 14 | +/// |
| 15 | +/// The constructor, [`NamedTempFile::new(name)`], creates directories in |
| 16 | +/// the location returned by [`std::env::temp_dir()`]. |
| 17 | +/// |
| 18 | +/// After creating a `NamedTempFile`, work with the file system by doing |
| 19 | +/// standard [`std::fs`] file system operations on its [`Path`], |
| 20 | +/// which can be retrieved with [`NamedTempFile::path()`]. Once the `NamedTempFile` |
| 21 | +/// value is dropped, the parent directory will be deleted, along with the file. It is your |
| 22 | +/// responsibility to ensure that no further file system operations are attempted inside the |
| 23 | +/// temporary directory once it has been deleted. |
| 24 | +/// |
| 25 | +/// # Resource Leaking |
| 26 | +/// |
| 27 | +/// Various platform-specific conditions may cause `NamedTempFile` to fail |
| 28 | +/// to delete the underlying directory. It's important to ensure that |
| 29 | +/// handles (like [`File`] and [`ReadDir`]) to the file inside the |
| 30 | +/// directory is dropped before the `NamedTempFile` goes out of scope. The |
| 31 | +/// `NamedTempFile` destructor will silently ignore any errors in deleting |
| 32 | +/// the directory; to instead handle errors call [`NamedTempFile::close()`]. |
| 33 | +/// |
| 34 | +/// Note that if the program exits before the `NamedTempFile` destructor is |
| 35 | +/// run, such as via [`std::process::exit()`], by segfaulting, or by |
| 36 | +/// receiving a signal like `SIGINT`, then the temporary directory |
| 37 | +/// will not be deleted. |
| 38 | +/// |
| 39 | +/// # Examples |
| 40 | +/// |
| 41 | +/// Create a temporary file. |
| 42 | +/// |
| 43 | +/// ``` |
| 44 | +/// use assert_fs::fixture::NamedTempFile; |
| 45 | +/// |
| 46 | +/// let tmp_file = NamedTempFile::new("foo.rs").unwrap(); |
| 47 | +/// |
| 48 | +/// // Ensure deletion happens. |
| 49 | +/// tmp_file.close().unwrap(); |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// [`File`]: http://doc.rust-lang.org/std/fs/struct.File.html |
| 53 | +/// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html |
| 54 | +/// [`ReadDir`]: http://doc.rust-lang.org/std/fs/struct.ReadDir.html |
| 55 | +/// [`NamedTempFile::close()`]: struct.NamedTempFile.html#method.close |
| 56 | +/// [`NamedTempFile::new()`]: struct.NamedTempFile.html#method.new |
| 57 | +/// [`NamedTempFile::path()`]: struct.NamedTempFile.html#method.path |
| 58 | +/// [`NamedTempFile`]: struct.NamedTempFile.html |
| 59 | +/// [`std::env::temp_dir()`]: https://doc.rust-lang.org/std/env/fn.temp_dir.html |
| 60 | +/// [`std::fs`]: http://doc.rust-lang.org/std/fs/index.html |
| 61 | +/// [`std::process::exit()`]: http://doc.rust-lang.org/std/process/fn.exit.html |
| 62 | +pub struct NamedTempFile { |
| 63 | + temp: tempfile::TempDir, |
| 64 | + path: path::PathBuf, |
| 65 | +} |
| 66 | + |
| 67 | +impl NamedTempFile { |
| 68 | + /// Attempts to make a temporary file inside of `env::temp_dir()`. |
| 69 | + /// |
| 70 | + /// The file and parent directory will be automatically deleted once the returned |
| 71 | + /// `NamedTempFile` is destroyed. |
| 72 | + /// |
| 73 | + /// # Errors |
| 74 | + /// |
| 75 | + /// If the parent directory can not be created, `Err` is returned. |
| 76 | + /// |
| 77 | + /// # Examples |
| 78 | + /// |
| 79 | + /// ``` |
| 80 | + /// use assert_fs::fixture::NamedTempFile; |
| 81 | + /// |
| 82 | + /// let tmp_file = NamedTempFile::new("foo.rs").unwrap(); |
| 83 | + /// |
| 84 | + /// // Ensure deletion happens. |
| 85 | + /// tmp_file.close().unwrap(); |
| 86 | + /// ``` |
| 87 | + pub fn new<S>(name: S) -> Result<Self, FixtureError> |
| 88 | + where |
| 89 | + S: AsRef<ffi::OsStr> |
| 90 | + { |
| 91 | + let temp = tempfile::TempDir::new() |
| 92 | + .chain(FixtureError::new(FixtureKind::CreateDir))?; |
| 93 | + let path = temp.path().join(name.as_ref()); |
| 94 | + Ok(Self { temp, path }) |
| 95 | + } |
| 96 | + |
| 97 | + /// Accesses the [`Path`] to the temporary file. |
| 98 | + /// |
| 99 | + /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html |
| 100 | + /// |
| 101 | + /// # Examples |
| 102 | + /// |
| 103 | + /// ``` |
| 104 | + /// use assert_fs::fixture::NamedTempFile; |
| 105 | + /// |
| 106 | + /// let tmp_file = NamedTempFile::new("foo.rs").unwrap(); |
| 107 | + /// |
| 108 | + /// println!("{}", tmp_file.path().display()); |
| 109 | + /// |
| 110 | + /// // Ensure deletion happens. |
| 111 | + /// tmp_file.close().unwrap(); |
| 112 | + /// ``` |
| 113 | + pub fn path(&self) -> &path::Path { |
| 114 | + &self.path |
| 115 | + } |
| 116 | + |
| 117 | + /// Closes and removes the temporary file and parent directory, returing a `Result`. |
| 118 | + /// |
| 119 | + /// Although `NamedTempFile` removes the directory on drop, in the destructor |
| 120 | + /// any errors are ignored. To detect errors cleaning up the temporary |
| 121 | + /// directory, call `close` instead. |
| 122 | + /// |
| 123 | + /// # Errors |
| 124 | + /// |
| 125 | + /// This function may return a variety of [`std::io::Error`]s that result from deleting the |
| 126 | + /// temporary file and parent directory, These errors may be platform specific. |
| 127 | + /// |
| 128 | + /// [`std::io::Error`]: http://doc.rust-lang.org/std/io/struct.Error.html |
| 129 | + /// |
| 130 | + /// # Examples |
| 131 | + /// |
| 132 | + /// ``` |
| 133 | + /// use assert_fs::fixture::NamedTempFile; |
| 134 | + /// |
| 135 | + /// let tmp_file = NamedTempFile::new("foo.rs").unwrap(); |
| 136 | + /// |
| 137 | + /// // Ensure deletion happens. |
| 138 | + /// tmp_file.close().unwrap(); |
| 139 | + /// ``` |
| 140 | + pub fn close(self) -> Result<(), FixtureError> { |
| 141 | + self.temp.close() |
| 142 | + .chain(FixtureError::new(FixtureKind::Cleanup))?; |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | +} |
0 commit comments