Skip to content

Commit

Permalink
fix(persist): Clarify API behavior
Browse files Browse the repository at this point in the history
Closes #51

BREAKING CHANGE: `persist_if` was renamed to `into_persist_if`.
  • Loading branch information
epage committed Nov 29, 2019
1 parent 4186172 commit 5411d07
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assert_fs"
version = "0.11.3"
version = "0.12.0"
authors = ["Ed Page <[email protected]>"]
description = "Filesystem fixtures and assertions for testing."
license = "MIT OR Apache-2.0"
Expand Down
28 changes: 26 additions & 2 deletions src/fixture/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,45 @@ impl TempDir {

/// Conditionally persist the temporary directory for debug purposes.
///
/// Note: this operation is not reversible, i.e. `into_persist_if(false)` is a no-op.
///
/// # Examples
///
/// ```no_run
/// use assert_fs::fixture::TempDir;
///
/// let tmp_dir = TempDir::new().unwrap().persist_if(true);
/// let tmp_dir = TempDir::new()
/// .unwrap()
/// .into_persist_if(std::env::var_os("TEST_PERSIST_FILES").is_some());
///
/// // Ensure deletion happens.
/// tmp_dir.close().unwrap();
/// ```
pub fn persist_if(self, yes: bool) -> Self {
pub fn into_persist_if(self, yes: bool) -> Self {
if !yes {
return self;
}

self.into_persist()
}

/// Persist the temporary directory for debug purposes.
///
/// Note: this operation is not reversible, i.e. `into_persist_if(false)` is a no-op.
///
/// # Examples
///
/// ```no_run
/// use assert_fs::fixture::TempDir;
///
/// let tmp_dir = TempDir::new()
/// .unwrap()
/// .into_persist();
///
/// // Ensure deletion happens.
/// tmp_dir.close().unwrap();
/// ```
pub fn into_persist(self) -> Self {
let path = match self.temp {
Inner::Temp(temp) => temp.into_path(),
Inner::Persisted(path) => path,
Expand Down
28 changes: 26 additions & 2 deletions src/fixture/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,45 @@ impl NamedTempFile {

/// Conditionally persist the temporary file for debug purposes.
///
/// Note: this operation is not reversible, i.e. `into_persist_if(false)` is a no-op.
///
/// # Examples
///
/// ```no_run
/// use assert_fs::fixture::NamedTempFile;
///
/// let tmp_file = NamedTempFile::new("foo.rs").unwrap().persist_if(true);
/// let tmp_file = NamedTempFile::new("foo.rs")
/// .unwrap()
/// .into_persist_if(std::env::var_os("TEST_PERSIST_FILES").is_some());
///
/// // Ensure deletion happens.
/// tmp_file.close().unwrap();
/// ```
pub fn persist_if(mut self, yes: bool) -> Self {
pub fn into_persist_if(self, yes: bool) -> Self {
if !yes {
return self;
}

self.into_persist()
}

/// Persist the temporary file for debug purposes.
///
/// Note: this operation is not reversible, i.e. `into_persist_if(false)` is a no-op.
///
/// # Examples
///
/// ```no_run
/// use assert_fs::fixture::NamedTempFile;
///
/// let tmp_file = NamedTempFile::new("foo.rs")
/// .unwrap()
/// .into_persist();
///
/// // Ensure deletion happens.
/// tmp_file.close().unwrap();
/// ```
pub fn into_persist(mut self) -> Self {
let mut temp = Inner::Persisted;
::std::mem::swap(&mut self.temp, &mut temp);
if let Inner::Temp(temp) = temp {
Expand Down

0 comments on commit 5411d07

Please sign in to comment.