Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dacb897

Browse files
author
Sebastian Thiel
committed
Dehli/India: simple recursive copy - deletion would like depth-first though ;)
1 parent 51ce1ed commit dacb897

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

src/interactive/app_test.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use dua::{
44
ByteFormat, Color, TraversalSorting, WalkOptions,
55
};
66
use failure::Error;
7-
use jwalk::WalkDir;
7+
use jwalk::{DirEntry, WalkDir};
88
use petgraph::prelude::NodeIndex;
99
use pretty_assertions::assert_eq;
1010
use std::env::temp_dir;
11-
use std::{ffi::OsStr, ffi::OsString, fmt, io, path::Path, path::PathBuf};
11+
use std::fs::{copy, create_dir, create_dir_all};
12+
use std::{ffi::OsStr, ffi::OsString, fmt, path::Path, path::PathBuf};
1213
use termion::input::TermRead;
1314
use tui::backend::TestBackend;
1415
use tui_react::Terminal;
@@ -346,16 +347,34 @@ fn delete_recursive(_path: impl AsRef<Path>) {
346347
unimplemented!();
347348
}
348349

349-
fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), io::Error> {
350-
WalkDir::new(src).num_threads(1).into_iter();
350+
fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> {
351+
for entry in WalkDir::new(&src).num_threads(1).into_iter() {
352+
let entry: DirEntry = entry?;
353+
let entry_path = entry.path();
354+
dbg!(&entry_path);
355+
entry_path
356+
.strip_prefix(&src)
357+
.map_err(Error::from)
358+
.and_then(|relative_entry_path| {
359+
let dst = dst.as_ref().join(relative_entry_path);
360+
dbg!(&dst);
361+
if entry_path.is_dir() {
362+
create_dir(dst).map_err(Into::into)
363+
} else {
364+
copy(&entry_path, dst).map(|_| ()).map_err(Into::into)
365+
}
366+
})?;
367+
}
351368
unimplemented!();
352369
}
353370

354371
impl From<&'static str> for WritableFixture {
355372
fn from(fixture_name: &str) -> Self {
356373
const TEMP_TLD_DIRNAME: &'static str = "dua-unit";
357374
let src = fixture(fixture_name);
358-
let dst = temp_dir().join(TEMP_TLD_DIRNAME).join(fixture_name);
375+
let dst = temp_dir().join(TEMP_TLD_DIRNAME);
376+
create_dir_all(&dst).unwrap();
377+
let dst = dst.join(fixture_name);
359378
copy_recursive(src, &dst).unwrap();
360379
WritableFixture { root: dst }
361380
}
@@ -364,7 +383,8 @@ impl From<&'static str> for WritableFixture {
364383
#[test]
365384
fn basic_user_journey_with_deletion() -> Result<(), Error> {
366385
let fixture = WritableFixture::from("sample-02");
367-
let (mut terminal, mut app) = initialized_app_and_terminal_from_paths(&[fixture.root.clone()])?;
386+
let (mut _terminal, mut _app) =
387+
initialized_app_and_terminal_from_paths(&[fixture.root.clone()])?;
368388
Ok(())
369389
}
370390

0 commit comments

Comments
 (0)