Skip to content

Commit

Permalink
Add for with_suffix (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borgerr authored Sep 28, 2024
1 parent 19280c5 commit d6600da
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,58 @@ impl TempDir {
Builder::new().prefix(&prefix).tempdir()
}

/// Attempts to make a temporary directory with the specified suffix inside of
/// `env::temp_dir()`. The directory and everything inside it will be automatically
/// deleted once the returned `TempDir` is destroyed.
///
/// # Errors
///
/// If the directory can not be created, `Err` is returned.
///
/// # Examples
///
/// ```
/// use std::fs::{self, File};
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// // Create a directory inside of the current directory
/// let tmp_dir = TempDir::with_suffix("-foo")?;
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
/// assert!(tmp_name.ends_with("-foo"));
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn with_suffix<S: AsRef<OsStr>>(suffix: S) -> io::Result<TempDir> {
Builder::new().suffix(&suffix).tempdir()
}
/// Attempts to make a temporary directory with the specified prefix inside
/// the specified directory. The directory and everything inside it will be
/// automatically deleted once the returned `TempDir` is destroyed.
///
/// # Errors
///
/// If the directory can not be created, `Err` is returned.
///
/// # Examples
///
/// ```
/// use std::fs::{self, File};
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// // Create a directory inside of the current directory
/// let tmp_dir = TempDir::with_suffix_in("-foo", ".")?;
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
/// assert!(tmp_name.ends_with("-foo"));
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn with_suffix_in<S: AsRef<OsStr>, P: AsRef<Path>>(
suffix: S,
dir: P,
) -> io::Result<TempDir> {
Builder::new().suffix(&suffix).tempdir_in(dir)
}

/// Attempts to make a temporary directory with the specified prefix inside
/// the specified directory. The directory and everything inside it will be
/// automatically deleted once the returned `TempDir` is destroyed.
Expand Down
27 changes: 27 additions & 0 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,33 @@ impl NamedTempFile<File> {
Builder::new().tempfile_in(dir)
}

/// Create a new named temporary file with the specified filename suffix.
///
/// See [`NamedTempFile::new()`] for details.
///
/// [`NamedTempFile::new()`]: #method.new
pub fn with_suffix<S: AsRef<OsStr>>(suffix: S) -> io::Result<NamedTempFile> {
Builder::new().suffix(&suffix).tempfile()
}
/// Create a new named temporary file with the specified filename suffix,
/// in the specified directory.
///
/// This is equivalent to:
///
/// ```ignore
/// Builder::new().suffix(&suffix).tempfile_in(directory)
/// ```
///
/// See [`NamedTempFile::new()`] for details.
///
/// [`NamedTempFile::new()`]: #method.new
pub fn with_suffix_in<S: AsRef<OsStr>, P: AsRef<Path>>(
suffix: S,
dir: P,
) -> io::Result<NamedTempFile> {
Builder::new().suffix(&suffix).tempfile_in(dir)
}

/// Create a new named temporary file with the specified filename prefix.
///
/// See [`NamedTempFile::new()`] for details.
Expand Down
7 changes: 7 additions & 0 deletions tests/namedtempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ fn test_prefix() {
assert!(name.starts_with("prefix"));
}

#[test]
fn test_suffix() {
let tmpfile = NamedTempFile::with_suffix("suffix").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.ends_with("suffix"));
}

#[test]
fn test_basic() {
let mut tmpfile = NamedTempFile::new().unwrap();
Expand Down
7 changes: 7 additions & 0 deletions tests/tempdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ fn test_prefix() {
assert!(name.starts_with("prefix"));
}

fn test_suffix() {
let tmpfile = TempDir::with_suffix_in("suffix", ".").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.ends_with("suffix"));
}

fn test_customnamed() {
let tmpfile = Builder::new()
.prefix("prefix")
Expand Down Expand Up @@ -175,6 +181,7 @@ fn test_keep() {
fn main() {
in_tmpdir(test_tempdir);
in_tmpdir(test_prefix);
in_tmpdir(test_suffix);
in_tmpdir(test_customnamed);
in_tmpdir(test_rm_tempdir);
in_tmpdir(test_rm_tempdir_close);
Expand Down

0 comments on commit d6600da

Please sign in to comment.