|
| 1 | +use std::path; |
| 2 | + |
| 3 | +use tempfile; |
| 4 | + |
| 5 | +use super::errors::*; |
| 6 | + |
| 7 | +/// A directory in the filesystem that is automatically deleted when |
| 8 | +/// it goes out of scope. |
| 9 | +/// |
| 10 | +/// The [`TempDir`] type creates a directory on the file system that |
| 11 | +/// is deleted once it goes out of scope. At construction, the |
| 12 | +/// `TempDir` creates a new directory with a randomly generated name. |
| 13 | +/// |
| 14 | +/// The constructor, [`TempDir::new()`], creates directories in |
| 15 | +/// the location returned by [`std::env::temp_dir()`]. |
| 16 | +/// |
| 17 | +/// After creating a `TempDir`, work with the file system by doing |
| 18 | +/// standard [`std::fs`] file system operations on its [`Path`], |
| 19 | +/// which can be retrieved with [`TempDir::path()`]. Once the `TempDir` |
| 20 | +/// value is dropped, the directory at the path will be deleted, along |
| 21 | +/// with any files and directories it contains. It is your responsibility |
| 22 | +/// to ensure that no further file system operations are attempted |
| 23 | +/// inside the temporary directory once it has been deleted. |
| 24 | +/// |
| 25 | +/// # Resource Leaking |
| 26 | +/// |
| 27 | +/// Various platform-specific conditions may cause `TempDir` to fail |
| 28 | +/// to delete the underlying directory. It's important to ensure that |
| 29 | +/// handles (like [`File`] and [`ReadDir`]) to files inside the |
| 30 | +/// directory are dropped before the `TempDir` goes out of scope. The |
| 31 | +/// `TempDir` destructor will silently ignore any errors in deleting |
| 32 | +/// the directory; to instead handle errors call [`TempDir::close()`]. |
| 33 | +/// |
| 34 | +/// Note that if the program exits before the `TempDir` 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 directory with a generated name: |
| 42 | +/// |
| 43 | +/// ``` |
| 44 | +/// use assert_fs::fixture::TempDir; |
| 45 | +/// |
| 46 | +/// let tmp_dir = TempDir::new().unwrap(); |
| 47 | +/// |
| 48 | +/// // Ensure deletion happens. |
| 49 | +/// tmp_dir.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 | +/// [`TempDir::close()`]: struct.TempDir.html#method.close |
| 56 | +/// [`TempDir::new()`]: struct.TempDir.html#method.new |
| 57 | +/// [`TempDir::path()`]: struct.TempDir.html#method.path |
| 58 | +/// [`TempDir`]: struct.TempDir.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 TempDir { |
| 63 | + temp: Inner, |
| 64 | +} |
| 65 | + |
| 66 | +enum Inner { |
| 67 | + Temp(tempfile::TempDir), |
| 68 | + Persisted(path::PathBuf), |
| 69 | +} |
| 70 | + |
| 71 | +impl TempDir { |
| 72 | + /// Attempts to make a temporary directory inside of `env::temp_dir()`. |
| 73 | + /// |
| 74 | + /// The directory and everything inside it will be automatically deleted |
| 75 | + /// once the returned `TempDir` is destroyed. |
| 76 | + /// |
| 77 | + /// # Errors |
| 78 | + /// |
| 79 | + /// If the directory can not be created, `Err` is returned. |
| 80 | + /// |
| 81 | + /// # Examples |
| 82 | + /// |
| 83 | + /// ``` |
| 84 | + /// use assert_fs::fixture::TempDir; |
| 85 | + /// |
| 86 | + /// let tmp_dir = TempDir::new().unwrap(); |
| 87 | + /// |
| 88 | + /// // Ensure deletion happens. |
| 89 | + /// tmp_dir.close().unwrap(); |
| 90 | + /// ``` |
| 91 | + pub fn new() -> Result<Self, FixtureError> { |
| 92 | + let temp = tempfile::TempDir::new().chain(FixtureError::new(FixtureKind::CreateDir))?; |
| 93 | + let temp = Inner::Temp(temp); |
| 94 | + Ok(Self { temp }) |
| 95 | + } |
| 96 | + |
| 97 | + /// Conditionally persist the temporary directory for debug purposes. |
| 98 | + /// |
| 99 | + /// # Examples |
| 100 | + /// |
| 101 | + /// ```no_run |
| 102 | + /// use assert_fs::fixture::TempDir; |
| 103 | + /// |
| 104 | + /// let tmp_dir = TempDir::new().unwrap().persist_if(true); |
| 105 | + /// |
| 106 | + /// // Ensure deletion happens. |
| 107 | + /// tmp_dir.close().unwrap(); |
| 108 | + /// ``` |
| 109 | + pub fn persist_if(self, yes: bool) -> Self { |
| 110 | + if !yes { |
| 111 | + return self; |
| 112 | + } |
| 113 | + |
| 114 | + let path = match self.temp { |
| 115 | + Inner::Temp(temp) => temp.into_path(), |
| 116 | + Inner::Persisted(path) => path, |
| 117 | + }; |
| 118 | + let temp = Inner::Persisted(path); |
| 119 | + Self { temp } |
| 120 | + } |
| 121 | + |
| 122 | + /// Accesses the [`Path`] to the temporary directory. |
| 123 | + /// |
| 124 | + /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html |
| 125 | + /// |
| 126 | + /// # Examples |
| 127 | + /// |
| 128 | + /// ``` |
| 129 | + /// use assert_fs::fixture::TempDir; |
| 130 | + /// |
| 131 | + /// let tmp_dir = TempDir::new().unwrap(); |
| 132 | + /// |
| 133 | + /// println!("{}", tmp_dir.path().display()); |
| 134 | + /// |
| 135 | + /// // Ensure deletion happens. |
| 136 | + /// tmp_dir.close().unwrap(); |
| 137 | + /// ``` |
| 138 | + pub fn path(&self) -> &path::Path { |
| 139 | + match self.temp { |
| 140 | + Inner::Temp(ref temp) => temp.path(), |
| 141 | + Inner::Persisted(ref path) => path.as_path(), |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// Closes and removes the temporary directory, returing a `Result`. |
| 146 | + /// |
| 147 | + /// Although `TempDir` removes the directory on drop, in the destructor |
| 148 | + /// any errors are ignored. To detect errors cleaning up the temporary |
| 149 | + /// directory, call `close` instead. |
| 150 | + /// |
| 151 | + /// # Errors |
| 152 | + /// |
| 153 | + /// This function may return a variety of [`std::io::Error`]s that result from deleting |
| 154 | + /// the files and directories contained with the temporary directory, |
| 155 | + /// as well as from deleting the temporary directory itself. These errors |
| 156 | + /// may be platform specific. |
| 157 | + /// |
| 158 | + /// [`std::io::Error`]: http://doc.rust-lang.org/std/io/struct.Error.html |
| 159 | + /// |
| 160 | + /// # Examples |
| 161 | + /// |
| 162 | + /// ``` |
| 163 | + /// use assert_fs::fixture::TempDir; |
| 164 | + /// |
| 165 | + /// let tmp_dir = TempDir::new().unwrap(); |
| 166 | + /// |
| 167 | + /// // Ensure deletion happens. |
| 168 | + /// tmp_dir.close().unwrap(); |
| 169 | + /// ``` |
| 170 | + pub fn close(self) -> Result<(), FixtureError> { |
| 171 | + match self.temp { |
| 172 | + Inner::Temp(temp) => temp |
| 173 | + .close() |
| 174 | + .chain(FixtureError::new(FixtureKind::Cleanup))?, |
| 175 | + Inner::Persisted(_) => (), |
| 176 | + } |
| 177 | + Ok(()) |
| 178 | + } |
| 179 | +} |
0 commit comments