Skip to content

Commit

Permalink
First failing test - even though just a guess :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 2, 2019
1 parent 396ab0b commit 68569c6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,3 +31,6 @@ name="dua"
panic = 'unwind'
incremental = false
overflow-checks = false

[dev-dependencies]
pretty_assertions = "0.6.1"
2 changes: 1 addition & 1 deletion src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 7 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,6 +57,7 @@ pub struct WalkOptions {
pub threads: usize,
pub byte_format: ByteFormat,
pub color: Color,
pub sorting: Sorting,
}

impl WalkOptions {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions src/interactive.rs
Original file line number Diff line number Diff line change
@@ -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>(
Expand All @@ -29,7 +41,8 @@ mod app {
where
B: Backend,
{
Ok(App {})
let tree = Tree::new();
Ok(App { tree })
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -28,6 +28,7 @@ fn run() -> Result<(), Error> {
} else {
Color::None
},
sorting: Sorting::None,
};
let res = match opt.command {
Some(Interactive { input }) => {
Expand Down
42 changes: 36 additions & 6 deletions tests/interactive.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 68569c6

Please sign in to comment.