TestDirUtils

Struct TestDirUtils 

Source
pub struct TestDirUtils { /* private fields */ }
Expand description

This struct implements a set of utilities that helps with the management of test files used inside the unit tests.

By default, it creates the test files inside a directory called “test_dir.tmp”. It is recommended to add this directory to the ignore list of your version control system in order to prevent the addition of the test files into the version control by accident.

Since Rust runs unit-tests using multiple threads, this instance will create a unique subdirectory for each instance. By default, this subdirectory is deleted when the instance goes out of scope.

Implementations§

Source§

impl TestDirUtils

Source

pub const DEFAULT_TEST_DIR: &'static str = "test_dir.tmp"

Directory to be used by the unit tests. Its is always “test_dir.tmp”.

Source

pub fn new(name: &str) -> Result<Self>

Creates a new TestDirUtils with the default name. It will automatically create the test directory if it does not exist. If the default path points to a file or a symlink, it will be deleted and recreated as a directory.

Returns the new instance of an error if the test directory is invalid or cannot be created.

Source

pub fn with_root(test_root: &Path, name: &str) -> Result<Self>

Creates a new TestDirUtils. It will automatically create the test directory if it does not exist. If the path points to a file or a symlink, it will be deleted and recreated as a directory.

As a safeguard, this constructor will panic if test_dir points to a root or a prefix (see std::path::Path::parent() for further details about how the root is detected).

Arguments:

  • test_dir: The path to the test directory;

Returns the new instance of an error if the test directory is invalid or cannot be created.

Source

pub fn delete_on_terminate(&self) -> bool

Returns the current value of delete_on_terminate. If true, the test director will be destroyed when this struct is dropped. If it is set to false, the directory will not be deleted.

This flag is true by default.

Source

pub fn set_delete_on_terminate(&mut self, delete_on_terminate: bool)

Changes the flag delete_on_terminate.

Source

pub fn test_dir(&self) -> &Path

Returns the path of the test directory.

Source

pub fn reset(&self) -> Result<()>

Deletes all of the contents of the test directory without removing it.

Source

pub fn get_test_file_path(&self, name: &str) -> OsString

Get the path of a file inside the test directory.

Source

pub fn create_test_file(&self, name: &str, contents: &[u8]) -> Result<OsString>

Creates a test file with the specfied name and write something into it.

Arguments:

  • name: The name of the file to be created;
  • contents: The contents of the file;

Returns the path to the newly created file.

Source

pub fn touch_test_file(&self, name: &str) -> Result<OsString>

Creates an empty test file with the specfied name. The file will have no contents as it is equivalent to call TestDirUtils::create_test_file() with b"" as its contents.

Arguments:

  • name: The name of the file to be created;

Returns the path to the newly created file.

Source

pub fn read_test_file(&self, name: &str) -> Result<Vec<u8>>

Reads all the contents of the specified test file. It uses std::fs::read() so it is subjected to the same restrictions.

Arguments:

  • name: The name of the file to be created;

Returns the contents of the file.

Source

pub fn delete_test_file(&self, name: &str) -> Result<()>

Deletes the specified file.

This method does nothing if the test file does not exist.

Arguments:

  • name: The name of the file to be removed;

Trait Implementations§

Source§

impl Drop for TestDirUtils

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.