Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plumbing for with_suffix for issue #298 #299

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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