Skip to content

Commit c6f8332

Browse files
committed
feat(fixture): Auto-create directories
1 parent e5209df commit c6f8332

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/fixture/tools.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,38 @@ impl PathCopy for ChildPath {
218218
}
219219
}
220220

221+
fn ensure_parent_dir(path: &path::Path) -> io::Result<()> {
222+
if let Some(parent) = path.parent() {
223+
fs::create_dir_all(parent)?;
224+
}
225+
Ok(())
226+
}
227+
221228
fn create_dir_all(path: &path::Path) -> io::Result<()> {
222229
fs::create_dir_all(path)?;
223230
Ok(())
224231
}
225232

226233
fn touch(path: &path::Path) -> io::Result<()> {
234+
ensure_parent_dir(path)?;
227235
fs::File::create(path)?;
228236
Ok(())
229237
}
230238

231239
fn write_binary(path: &path::Path, data: &[u8]) -> io::Result<()> {
240+
ensure_parent_dir(path)?;
232241
let mut file = fs::File::create(path)?;
233242
file.write_all(data)?;
234243
Ok(())
235244
}
236245

237246
fn write_str(path: &path::Path, data: &str) -> io::Result<()> {
247+
ensure_parent_dir(path)?;
238248
write_binary(path, data.as_bytes())
239249
}
240250

241251
fn write_file(path: &path::Path, data: &path::Path) -> io::Result<()> {
252+
ensure_parent_dir(path)?;
242253
fs::copy(data, path)?;
243254
Ok(())
244255
}

0 commit comments

Comments
 (0)