Skip to content

Commit 4209ad2

Browse files
committed
feat(fixture): Copy a file
Fixes #34
1 parent f7adecd commit 4209ad2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/fixture.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,36 @@ impl FileWriteStr for ChildPath {
206206
}
207207
}
208208

209+
/// Write (copy) a file to [`ChildPath`].
210+
///
211+
/// [`ChildPath`]: struct.ChildPath.html
212+
pub trait FileWriteFile {
213+
/// Write (copy) a file to [`ChildPath`].
214+
///
215+
/// # Examples
216+
///
217+
/// ```rust
218+
/// use std::path::Path;
219+
/// use assert_fs::prelude::*;
220+
///
221+
/// let temp = assert_fs::TempDir::new().unwrap();
222+
/// temp
223+
/// .child("foo.txt")
224+
/// .write_file(Path::new("Cargo.toml"))
225+
/// .unwrap();
226+
/// temp.close().unwrap();
227+
/// ```
228+
///
229+
/// [`ChildPath`]: struct.ChildPath.html
230+
fn write_file(&self, data: &path::Path) -> io::Result<()>;
231+
}
232+
233+
impl FileWriteFile for ChildPath {
234+
fn write_file(&self, data: &path::Path) -> io::Result<()> {
235+
write_file(self.path(), data)
236+
}
237+
}
238+
209239
/// Copy files into [`TempDir`].
210240
///
211241
/// [`TempDir`]: struct.TempDir.html
@@ -268,6 +298,11 @@ fn write_str(path: &path::Path, data: &str) -> io::Result<()> {
268298
write_binary(path, data.as_bytes())
269299
}
270300

301+
fn write_file(path: &path::Path, data: &path::Path) -> io::Result<()> {
302+
fs::copy(data, path)?;
303+
Ok(())
304+
}
305+
271306
fn copy_files<S>(
272307
target: &path::Path,
273308
source: &path::Path,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub mod prelude {
8181
pub use fixture::PathCreateDir;
8282
pub use fixture::FileTouch;
8383
pub use fixture::FileWriteBin;
84+
pub use fixture::FileWriteFile;
8485
pub use fixture::FileWriteStr;
8586
pub use fixture::PathChild;
8687
pub use fixture::PathCopy;

0 commit comments

Comments
 (0)