Skip to main content

FakeFilesystem

Struct FakeFilesystem 

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

In-memory filesystem for tests.

Files are stored as byte vectors keyed by their (absolute or relative) path. Directory entries are derived automatically from file paths. All methods that mutate state accept &self via interior mutability (RefCell) so that test helpers and trait implementations share the same borrow.

Implementations§

Source§

impl FakeFilesystem

Source

pub fn new() -> Self

Source

pub fn set_fail_on_write(&self, path: impl Into<PathBuf>)

Cause the next write() to the given path to return an error.

Source

pub fn set_fail_on_rename(&self, path: impl Into<PathBuf>)

Cause rename() targeting the given destination path to return an error.

Source

pub fn set_fail_on_copy(&self, fail: bool)

Cause all copy_file() calls to return an error.

Source

pub fn add_file(&self, path: impl Into<PathBuf>, content: impl Into<Vec<u8>>)

Insert a file with the given content, automatically registering all ancestor directories.

Source

pub fn add_dir(&self, path: impl Into<PathBuf>)

Explicitly register a directory path.

Source

pub fn get_file_content(&self, path: &Path) -> Option<Vec<u8>>

Return the stored content for a path, or None if absent.

Source

pub fn file_exists(&self, path: &Path) -> bool

Return true if the path has been added as a file.

Source

pub fn snapshot_files(&self) -> BTreeMap<PathBuf, Vec<u8>>

Return a sorted snapshot of every file currently stored in the fake filesystem.

Trait Implementations§

Source§

impl Default for FakeFilesystem

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Filesystem for FakeFilesystem

Source§

fn exists(&self, path: &Path) -> bool

Source§

fn is_dir(&self, path: &Path) -> bool

Source§

fn is_file(&self, path: &Path) -> bool

Source§

fn read_to_string(&self, path: &Path) -> Result<String>

Source§

fn read(&self, path: &Path) -> Result<Vec<u8>>

Source§

fn write(&self, path: &Path, contents: &str) -> Result<()>

Source§

fn write_bytes(&self, path: &Path, contents: &[u8]) -> Result<()>

Source§

fn create_dir_all(&self, path: &Path) -> Result<()>

Source§

fn copy_file(&self, src: &Path, dest: &Path) -> Result<()>

Source§

fn rename(&self, from: &Path, to: &Path) -> Result<()>

Atomically rename from to to, replacing to if it already exists.
Source§

fn remove_file(&self, path: &Path) -> Result<()>

Source§

fn remove_dir_all(&self, path: &Path) -> Result<()>

Source§

fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>>

Source§

fn canonicalize(&self, path: &Path) -> Result<PathBuf>

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> Same for T

Source§

type Output = T

Should always be Self
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.