Skip to content

Commit

Permalink
feat(dir): Accept more path types for template paths
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 16, 2024
1 parent 0099305 commit 5de5c1f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
61 changes: 61 additions & 0 deletions crates/snapbox/src/dir/fixture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Collection of files
pub trait DirFixture {
/// Initialize a test fixture directory `root`
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error>;
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for std::path::Path {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
super::copy_template(self, root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for &'_ std::path::Path {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for &'_ std::path::PathBuf {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for std::path::PathBuf {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for str {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for &'_ str {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for &'_ String {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}

#[cfg(feature = "dir")] // for documentation purposes only
impl DirFixture for String {
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
std::path::Path::new(self).write_to_path(root)
}
}
2 changes: 2 additions & 0 deletions crates/snapbox/src/dir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! Initialize working directories and assert on how they've changed
mod diff;
mod fixture;
mod ops;
mod root;
#[cfg(test)]
mod tests;

pub use diff::FileType;
pub use diff::PathDiff;
pub use fixture::DirFixture;
#[cfg(feature = "dir")]
pub use ops::copy_template;
pub use ops::resolve_dir;
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/dir/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl DirRoot {
#[cfg(feature = "dir")]
pub fn with_template(
self,
template_root: &std::path::Path,
template: &dyn crate::dir::DirFixture,
) -> Result<Self, crate::assert::Error> {
match &self.0 {
DirRootInner::None | DirRootInner::Immutable(_) => {
Expand All @@ -57,7 +57,7 @@ impl DirRoot {
path.display(),
template_root.display()
);
super::copy_template(template_root, path)?;
template.write_to_path(path)?;
}
}

Expand Down

0 comments on commit 5de5c1f

Please sign in to comment.