|
| 1 | +use std::ffi::OsStr; |
1 | 2 | use std::path;
|
2 | 3 |
|
3 | 4 | use super::errors::FixtureError;
|
@@ -89,6 +90,91 @@ impl TempDir {
|
89 | 90 | Ok(Self { temp })
|
90 | 91 | }
|
91 | 92 |
|
| 93 | + /// Attempts to make a temporary directory inside of `dir`. |
| 94 | + /// |
| 95 | + /// The directory and everything inside it will be automatically deleted |
| 96 | + /// once the returned `TempDir` is destroyed. |
| 97 | + /// |
| 98 | + /// # Errors |
| 99 | + /// |
| 100 | + /// If the directory can not be created, `Err` is returned. |
| 101 | + /// |
| 102 | + /// # Examples |
| 103 | + /// |
| 104 | + /// ``` |
| 105 | + /// use assert_fs::fixture::TempDir; |
| 106 | + /// |
| 107 | + /// // Create a directory inside of the current directory. |
| 108 | + /// let tmp_dir = TempDir::new_in(".").unwrap(); |
| 109 | + /// |
| 110 | + /// // Ensure deletion happens. |
| 111 | + /// tmp_dir.close().unwrap(); |
| 112 | + /// ``` |
| 113 | + pub fn new_in<P: AsRef<path::Path>>(dir: P) -> Result<Self, FixtureError> { |
| 114 | + let temp = |
| 115 | + tempfile::TempDir::new_in(dir).chain(FixtureError::new(FixtureKind::CreateDir))?; |
| 116 | + let temp = Inner::Temp(temp); |
| 117 | + Ok(Self { temp }) |
| 118 | + } |
| 119 | + |
| 120 | + /// Attempts to make a temporary directory with the specified prefix inside |
| 121 | + /// of `env::temp_dir()`. |
| 122 | + /// |
| 123 | + /// The directory and everything inside it will be automatically deleted |
| 124 | + /// once the returned `TempDir` is destroyed. |
| 125 | + /// |
| 126 | + /// # Errors |
| 127 | + /// |
| 128 | + /// If the directory can not be created, `Err` is returned. |
| 129 | + /// |
| 130 | + /// # Examples |
| 131 | + /// |
| 132 | + /// ``` |
| 133 | + /// use assert_fs::fixture::TempDir; |
| 134 | + /// |
| 135 | + /// let tmp_dir = TempDir::with_prefix("foo-").unwrap(); |
| 136 | + /// |
| 137 | + /// // Ensure deletion happens. |
| 138 | + /// tmp_dir.close().unwrap(); |
| 139 | + /// ``` |
| 140 | + pub fn with_prefix<S: AsRef<OsStr>>(prefix: S) -> Result<Self, FixtureError> { |
| 141 | + let temp = tempfile::TempDir::with_prefix(prefix) |
| 142 | + .chain(FixtureError::new(FixtureKind::CreateDir))?; |
| 143 | + let temp = Inner::Temp(temp); |
| 144 | + Ok(Self { temp }) |
| 145 | + } |
| 146 | + |
| 147 | + /// Attempts to make a temporary directory with the specified prefix inside |
| 148 | + /// the specified directory. |
| 149 | + /// |
| 150 | + /// The directory and everything inside it will be automatically deleted |
| 151 | + /// once the returned `TempDir` is destroyed. |
| 152 | + /// |
| 153 | + /// # Errors |
| 154 | + /// |
| 155 | + /// If the directory can not be created, `Err` is returned. |
| 156 | + /// |
| 157 | + /// # Examples |
| 158 | + /// |
| 159 | + /// ``` |
| 160 | + /// use assert_fs::fixture::TempDir; |
| 161 | + /// |
| 162 | + /// // Create a directory with prefix "foo-" inside the current directory. |
| 163 | + /// let tmp_dir = TempDir::with_prefix_in("foo-", ".").unwrap(); |
| 164 | + /// |
| 165 | + /// // Ensure deletion happens. |
| 166 | + /// tmp_dir.close().unwrap(); |
| 167 | + /// ``` |
| 168 | + pub fn with_prefix_in<S: AsRef<OsStr>, P: AsRef<path::Path>>( |
| 169 | + prefix: S, |
| 170 | + dir: P, |
| 171 | + ) -> Result<Self, FixtureError> { |
| 172 | + let temp = tempfile::TempDir::with_prefix_in(prefix, dir) |
| 173 | + .chain(FixtureError::new(FixtureKind::CreateDir))?; |
| 174 | + let temp = Inner::Temp(temp); |
| 175 | + Ok(Self { temp }) |
| 176 | + } |
| 177 | + |
92 | 178 | /// Conditionally persist the temporary directory for debug purposes.
|
93 | 179 | ///
|
94 | 180 | /// Note: this operation is not reversible, i.e. `into_persistent_if(false)` is a no-op.
|
|
0 commit comments