Skip to content

Commit 166e6ad

Browse files
authored
Merge pull request #135 from sunshowers/new-in
feat: add TempDir::new_in/with_prefix/with_prefix_in
2 parents d0e9862 + b9ccf5b commit 166e6ad

File tree

4 files changed

+206
-40
lines changed

4 files changed

+206
-40
lines changed

Cargo.lock

+118-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ color = ["dep:anstream", "predicates/color"]
120120
color-auto = ["color"]
121121

122122
[dependencies]
123-
tempfile = "3.0"
123+
tempfile = "3.8"
124124
globwalk = "0.9"
125125
predicates = { version = "3.0.1", default-features = false, features = ["diff"] }
126126
predicates-core = "1.0.6"

src/fixture/dir.rs

+86
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::OsStr;
12
use std::path;
23

34
use super::errors::FixtureError;
@@ -89,6 +90,91 @@ impl TempDir {
8990
Ok(Self { temp })
9091
}
9192

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+
92178
/// Conditionally persist the temporary directory for debug purposes.
93179
///
94180
/// Note: this operation is not reversible, i.e. `into_persistent_if(false)` is a no-op.

0 commit comments

Comments
 (0)