diff options
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/aggregate.rs | 2 | ||||
-rw-r--r-- | src/common.rs | 10 | ||||
-rw-r--r-- | src/interactive.rs | 21 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | tests/interactive.rs | 42 |
6 files changed, 67 insertions, 15 deletions
@@ -18,6 +18,7 @@ byte-unit = "2.1.0" termion = "1.5.2" atty = "0.2.11" tui = "0.6.0" +petgraph = "0.4.13" [[bin]] name="dua" @@ -30,3 +31,6 @@ name="dua" panic = 'unwind' incremental = false overflow-checks = false + +[dev-dependencies] +pretty_assertions = "0.6.1" diff --git a/src/aggregate.rs b/src/aggregate.rs index c62ea82..faeea14 100644 --- a/src/aggregate.rs +++ b/src/aggregate.rs @@ -24,7 +24,7 @@ pub fn aggregate( num_roots += 1; let mut num_bytes = 0u64; let mut num_errors = 0u64; - for entry in options.iter_from_path(path.as_ref(), Sorting::None) { + for entry in options.iter_from_path(path.as_ref()) { stats.files_traversed += 1; match entry { Ok(entry) => { diff --git a/src/common.rs b/src/common.rs index fb6ff03..0ed32f4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -12,8 +12,10 @@ pub enum ByteFormat { Bytes, } -pub(crate) enum Sorting { +/// Identify the kind of sorting to apply during filesystem iteration +pub enum Sorting { None, + AlphabeticalByFileName, } /// Specify the kind of color to use @@ -55,6 +57,7 @@ pub struct WalkOptions { pub threads: usize, pub byte_format: ByteFormat, pub color: Color, + pub sorting: Sorting, } impl WalkOptions { @@ -85,11 +88,12 @@ impl WalkOptions { } } - pub(crate) fn iter_from_path(&self, path: &Path, sort: Sorting) -> WalkDir { + pub(crate) fn iter_from_path(&self, path: &Path) -> WalkDir { WalkDir::new(path) .preload_metadata(true) - .sort(match sort { + .sort(match self.sorting { Sorting::None => false, + Sorting::AlphabeticalByFileName => true, }) .skip_hidden(false) .num_threads(self.threads) diff --git a/src/interactive.rs b/src/interactive.rs index 317f815..4b88b52 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -1,12 +1,24 @@ mod app { use crate::{WalkOptions, WalkResult}; use failure::Error; - use std::io; - use std::path::PathBuf; + use petgraph::{Directed, Graph}; + use std::{ffi::OsString, io, path::PathBuf}; use termion::input::{Keys, TermReadEventsAndRaw}; use tui::{backend::Backend, Terminal}; - pub struct App {} + pub type GraphIndexType = u32; + pub type Tree = Graph<ItemData, (), Directed, GraphIndexType>; + + #[derive(Eq, PartialEq, Debug)] + pub struct ItemData { + pub name: OsString, + pub size: u64, + } + + #[derive(Default, Debug)] + pub struct App { + pub tree: Tree, + } impl App { pub fn process_events<B, R>( @@ -29,7 +41,8 @@ mod app { where B: Backend, { - Ok(App {}) + let tree = Tree::new(); + Ok(App { tree }) } } } diff --git a/src/main.rs b/src/main.rs index 758d975..6a8f3b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate structopt; use structopt::StructOpt; -use dua::{ByteFormat, Color}; +use dua::{ByteFormat, Color, Sorting}; use failure::{Error, ResultExt}; use failure_tools::ok_or_exit; use std::{fs, io, io::Write, path::PathBuf, process}; @@ -28,6 +28,7 @@ fn run() -> Result<(), Error> { } else { Color::None }, + sorting: Sorting::None, }; let res = match opt.command { Some(Interactive { input }) => { diff --git a/tests/interactive.rs b/tests/interactive.rs index 52ae8bc..1ea19d4 100644 --- a/tests/interactive.rs +++ b/tests/interactive.rs @@ -1,25 +1,55 @@ mod app { - use dua::interactive::App; - use dua::{ByteFormat, Color, WalkOptions}; + use dua::interactive::{App, ItemData, Tree}; + use dua::{ByteFormat, Color, Sorting, WalkOptions}; use failure::Error; - use std::path::Path; + use pretty_assertions::assert_eq; + use std::{ffi::OsString, fmt, path::Path}; use tui::backend::TestBackend; use tui::Terminal; + fn debug(item: impl fmt::Debug) -> String { + format!("{:?}", item) + } + #[test] fn journey_with_single_path() -> Result<(), Error> { - let mut terminal = Terminal::new(TestBackend::new(40, 20))?; - let input = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/sample-01"); + let (terminal, app) = initialized_app_and_terminal("sample-01")?; + let expected_tree = sample_01_tree(); + + assert_eq!( + debug(app.tree), + debug(expected_tree), + "filesystem graph is stable and matches the directory structure" + ); + Ok(()) + } + fn initialized_app_and_terminal( + fixture_path: &str, + ) -> Result<(Terminal<TestBackend>, App), Error> { + let mut terminal = Terminal::new(TestBackend::new(40, 20))?; + let input = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("tests/fixtures") + .join(fixture_path); let app = App::initialize( &mut terminal, WalkOptions { threads: 1, byte_format: ByteFormat::Metric, color: Color::None, + sorting: Sorting::AlphabeticalByFileName, }, vec![input], )?; - Ok(()) + Ok((terminal, app)) + } + + fn sample_01_tree() -> Tree { + let mut expected_tree = Tree::new(); + expected_tree.add_node(ItemData { + name: OsString::from("foo"), + size: 231, + }); + expected_tree } } |