Struct cli_test_dir::TestDir

source ·
pub struct TestDir { /* private fields */ }
Expand description

This code is inspired by the WorkDir pattern that BurntSushi uses to test CLI tools like ripgrep and xsv.

Implementations§

source§

impl TestDir

source

pub fn new(bin_name: &str, test_name: &str) -> TestDir

Create a new TestDir for the current test. You must specify bin_name (the name of a binary built by the current crate) and test_name (a unique name for the current test).

If our output directory exists from a previous test run, it will be deleted.

source

pub fn cmd(&self) -> Command

Return a std::process::Command object that can be used to execute the binary.

source

pub fn path<P: AsRef<Path>>(&self, path: P) -> PathBuf

Construct a path relative to our test directory.

let testdir = TestDir::new("touch", "path_builds_paths");
let mut cmd = testdir.cmd();
cmd
  .arg("example.txt")
  .expect_success();
assert!(testdir.path("example.txt").exists());
source

pub fn src_path<P: AsRef<Path>>(&self, path: P) -> PathBuf

Return a path relative to the source directory of the current crate. Useful for finding fixtures.

source

pub fn create_file<P, S>(&self, path: P, contents: S)
where P: AsRef<Path>, S: AsRef<[u8]>,

Create a file in our test directory with the specified contents.

source

pub fn expect_path<P: AsRef<Path>>(&self, path: P)

If path does not point to valid path, fail the current test.

source

pub fn expect_no_such_path<P: AsRef<Path>>(&self, path: P)

If path does not point to valid path, fail the current test.

source

pub fn expect_file_contents<P, S>(&self, path: P, expected: S)
where P: AsRef<Path>, S: AsRef<[u8]>,

Verify that the file contains the specified data.

source

pub fn expect_contains<P>(&self, path: P, pattern: &str)
where P: AsRef<Path>,

Verify that the contents of the file match the specified pattern. Someday this should support std::str::pattern::Pattern so that we can support both strings and regular expressions, but that hasn’t been stabilized yet.

source

pub fn expect_does_not_contain<P>(&self, path: P, pattern: &str)
where P: AsRef<Path>,

Verify that the contents of the file do not match the specified pattern. Someday this should support std::str::pattern::Pattern so that we can support both strings and regular expressions, but that hasn’t been stabilized yet.

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>,

§

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>,

§

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.