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
impl TestDirUtils
Sourcepub const DEFAULT_TEST_DIR: &'static str = "test_dir.tmp"
pub const DEFAULT_TEST_DIR: &'static str = "test_dir.tmp"
Directory to be used by the unit tests. Its is always “test_dir.tmp”.
Sourcepub fn new(name: &str) -> Result<Self>
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.
Sourcepub fn with_root(test_root: &Path, name: &str) -> Result<Self>
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.
Sourcepub fn delete_on_terminate(&self) -> bool
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.
Sourcepub fn set_delete_on_terminate(&mut self, delete_on_terminate: bool)
pub fn set_delete_on_terminate(&mut self, delete_on_terminate: bool)
Changes the flag delete_on_terminate.
Sourcepub fn reset(&self) -> Result<()>
pub fn reset(&self) -> Result<()>
Deletes all of the contents of the test directory without removing it.
Sourcepub fn get_test_file_path(&self, name: &str) -> OsString
pub fn get_test_file_path(&self, name: &str) -> OsString
Get the path of a file inside the test directory.
Sourcepub fn create_test_file(&self, name: &str, contents: &[u8]) -> Result<OsString>
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.
Sourcepub fn touch_test_file(&self, name: &str) -> Result<OsString>
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.
Sourcepub fn read_test_file(&self, name: &str) -> Result<Vec<u8>>
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.
Sourcepub fn delete_test_file(&self, name: &str) -> Result<()>
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;