Skip to content

Commit 5de5c1f

Browse files
committed
feat(dir): Accept more path types for template paths
1 parent 0099305 commit 5de5c1f

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

crates/snapbox/src/dir/fixture.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// Collection of files
2+
pub trait DirFixture {
3+
/// Initialize a test fixture directory `root`
4+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error>;
5+
}
6+
7+
#[cfg(feature = "dir")] // for documentation purposes only
8+
impl DirFixture for std::path::Path {
9+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
10+
super::copy_template(self, root)
11+
}
12+
}
13+
14+
#[cfg(feature = "dir")] // for documentation purposes only
15+
impl DirFixture for &'_ std::path::Path {
16+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
17+
std::path::Path::new(self).write_to_path(root)
18+
}
19+
}
20+
21+
#[cfg(feature = "dir")] // for documentation purposes only
22+
impl DirFixture for &'_ std::path::PathBuf {
23+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
24+
std::path::Path::new(self).write_to_path(root)
25+
}
26+
}
27+
28+
#[cfg(feature = "dir")] // for documentation purposes only
29+
impl DirFixture for std::path::PathBuf {
30+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
31+
std::path::Path::new(self).write_to_path(root)
32+
}
33+
}
34+
35+
#[cfg(feature = "dir")] // for documentation purposes only
36+
impl DirFixture for str {
37+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
38+
std::path::Path::new(self).write_to_path(root)
39+
}
40+
}
41+
42+
#[cfg(feature = "dir")] // for documentation purposes only
43+
impl DirFixture for &'_ str {
44+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
45+
std::path::Path::new(self).write_to_path(root)
46+
}
47+
}
48+
49+
#[cfg(feature = "dir")] // for documentation purposes only
50+
impl DirFixture for &'_ String {
51+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
52+
std::path::Path::new(self).write_to_path(root)
53+
}
54+
}
55+
56+
#[cfg(feature = "dir")] // for documentation purposes only
57+
impl DirFixture for String {
58+
fn write_to_path(&self, root: &std::path::Path) -> Result<(), crate::assert::Error> {
59+
std::path::Path::new(self).write_to_path(root)
60+
}
61+
}

crates/snapbox/src/dir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Initialize working directories and assert on how they've changed
22
33
mod diff;
4+
mod fixture;
45
mod ops;
56
mod root;
67
#[cfg(test)]
78
mod tests;
89

910
pub use diff::FileType;
1011
pub use diff::PathDiff;
12+
pub use fixture::DirFixture;
1113
#[cfg(feature = "dir")]
1214
pub use ops::copy_template;
1315
pub use ops::resolve_dir;

crates/snapbox/src/dir/root.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl DirRoot {
4545
#[cfg(feature = "dir")]
4646
pub fn with_template(
4747
self,
48-
template_root: &std::path::Path,
48+
template: &dyn crate::dir::DirFixture,
4949
) -> Result<Self, crate::assert::Error> {
5050
match &self.0 {
5151
DirRootInner::None | DirRootInner::Immutable(_) => {
@@ -57,7 +57,7 @@ impl DirRoot {
5757
path.display(),
5858
template_root.display()
5959
);
60-
super::copy_template(template_root, path)?;
60+
template.write_to_path(path)?;
6161
}
6262
}
6363

0 commit comments

Comments
 (0)