Skip to content

Commit

Permalink
chore: modernize doctests (#282)
Browse files Browse the repository at this point in the history
We no longer need wrapping "main" blocks.
  • Loading branch information
Stebalien authored May 10, 2024
1 parent 73e92b3 commit e5418bd
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 403 deletions.
71 changes: 15 additions & 56 deletions src/dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ use crate::Builder;
/// ```
/// use tempfile::tempdir;
/// use std::fs::File;
/// use std::io::{self, Write};
///
/// # fn main() {
/// # if let Err(_) = run() {
/// # ::std::process::exit(1);
/// # }
/// # }
/// # fn run() -> Result<(), io::Error> {
/// use std::io::Write;
///
/// // Create a directory inside of `std::env::temp_dir()`
/// let tmp_dir = tempdir()?;
///
Expand All @@ -56,8 +50,7 @@ use crate::Builder;
/// // `tmp_file` will be deleted here.
/// drop(tmp_file);
/// tmp_dir.close()?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`TempDir`]: struct.TempDir.html
Expand Down Expand Up @@ -86,14 +79,8 @@ pub fn tempdir() -> io::Result<TempDir> {
/// ```
/// use tempfile::tempdir_in;
/// use std::fs::File;
/// use std::io::{self, Write};
///
/// # fn main() {
/// # if let Err(_) = run() {
/// # ::std::process::exit(1);
/// # }
/// # }
/// # fn run() -> Result<(), io::Error> {
/// use std::io::Write;
///
/// // Create a directory inside of the current directory.
/// let tmp_dir = tempdir_in(".")?;
///
Expand All @@ -105,8 +92,7 @@ pub fn tempdir() -> io::Result<TempDir> {
/// // `tmp_file` will be deleted here.
/// drop(tmp_file);
/// tmp_dir.close()?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`TempDir`]: struct.TempDir.html
Expand Down Expand Up @@ -158,12 +144,9 @@ pub fn tempdir_in<P: AsRef<Path>>(dir: P) -> io::Result<TempDir> {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`
/// let tmp_dir = TempDir::new()?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// Create a temporary directory with a prefix in its name:
Expand All @@ -173,13 +156,10 @@ pub fn tempdir_in<P: AsRef<Path>>(dir: P) -> io::Result<TempDir> {
/// use std::io::Write;
/// use tempfile::Builder;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`,
/// // whose name will begin with 'example'.
/// let tmp_dir = Builder::new().prefix("example").tempdir()?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`File`]: http://doc.rust-lang.org/std/fs/struct.File.html
Expand Down Expand Up @@ -216,8 +196,6 @@ impl TempDir {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`
/// let tmp_dir = TempDir::new()?;
///
Expand All @@ -227,8 +205,7 @@ impl TempDir {
///
/// // `tmp_dir` goes out of scope, the directory as well as
/// // `tmp_file` will be deleted here.
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`Builder`]: struct.Builder.html
Expand All @@ -251,15 +228,12 @@ impl TempDir {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of the current directory
/// let tmp_dir = TempDir::new_in(".")?;
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
/// let mut tmp_file = File::create(file_path)?;
/// writeln!(tmp_file, "Brian was here. Briefly.")?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn new_in<P: AsRef<Path>>(dir: P) -> io::Result<TempDir> {
Builder::new().tempdir_in(dir)
Expand All @@ -280,14 +254,11 @@ impl TempDir {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of the current directory
/// let tmp_dir = TempDir::with_prefix("foo-")?;
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
/// assert!(tmp_name.starts_with("foo-"));
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn with_prefix<S: AsRef<OsStr>>(prefix: S) -> io::Result<TempDir> {
Builder::new().prefix(&prefix).tempdir()
Expand All @@ -308,14 +279,11 @@ impl TempDir {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of the current directory
/// let tmp_dir = TempDir::with_prefix_in("foo-", ".")?;
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
/// assert!(tmp_name.starts_with("foo-"));
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn with_prefix_in<S: AsRef<OsStr>, P: AsRef<Path>>(
prefix: S,
Expand All @@ -333,8 +301,6 @@ impl TempDir {
/// ```
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// let tmp_path;
///
/// {
Expand All @@ -349,8 +315,7 @@ impl TempDir {
///
/// // Temp directory should be deleted by now
/// assert_eq!(tmp_path.exists(), false);
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
#[must_use]
pub fn path(&self) -> &path::Path {
Expand All @@ -371,8 +336,6 @@ impl TempDir {
/// use std::fs;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// let tmp_dir = TempDir::new()?;
///
/// // Persist the temporary directory to disk,
Expand All @@ -381,8 +344,7 @@ impl TempDir {
///
/// // Delete the temporary directory ourselves.
/// fs::remove_dir_all(tmp_path)?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
#[must_use]
pub fn into_path(self) -> PathBuf {
Expand Down Expand Up @@ -416,8 +378,6 @@ impl TempDir {
/// use std::io::Write;
/// use tempfile::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`.
/// let tmp_dir = TempDir::new()?;
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
Expand All @@ -431,8 +391,7 @@ impl TempDir {
/// // succeeded.
/// drop(tmp_file);
/// tmp_dir.close()?;
/// # Ok(())
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn close(mut self) -> io::Result<()> {
let result = remove_dir_all(self.path()).with_err_path(|| self.path());
Expand Down
Loading

0 comments on commit e5418bd

Please sign in to comment.