diff options
author | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-08 16:07:20 +0530 |
---|---|---|
committer | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-08 16:07:20 +0530 |
commit | 82b0ced5c18ae8dbe3730434e2447a013bb35480 (patch) | |
tree | 7373fd7fd3654ced326970d4274fa2ca53575aa1 | |
parent | 406435beff334d8f0ad62560176774ede2771ecd (diff) |
Somewhere over China: preparation for splitting tests into modules
-rw-r--r-- | src/interactive/app_test/mod.rs (renamed from src/interactive/app_test.rs) | 476 |
1 files changed, 245 insertions, 231 deletions
diff --git a/src/interactive/app_test.rs b/src/interactive/app_test/mod.rs index 3a02e7c..bb9ce79 100644 --- a/src/interactive/app_test.rs +++ b/src/interactive/app_test/mod.rs @@ -1,32 +1,15 @@ -use crate::interactive::{SortMode, TerminalApp}; -use dua::{ - traverse::{EntryData, Tree, TreeIndex}, - ByteFormat, Color, TraversalSorting, WalkOptions, +use crate::interactive::app_test::utils::{ + debug, fixture_str, index_by_name, initialized_app_and_terminal_from_fixture, + initialized_app_and_terminal_from_paths, node_by_index, node_by_name, sample_01_tree, + sample_02_tree, WritableFixture, }; -use failure::{Error, ResultExt}; -use itertools::Itertools; -use jwalk::{DirEntry, WalkDir}; -use petgraph::prelude::NodeIndex; +use crate::interactive::SortMode; +use failure::Error; use pretty_assertions::assert_eq; -use std::{ - env::temp_dir, - ffi::OsStr, - ffi::OsString, - fmt, - fs::{copy, create_dir_all, remove_dir, remove_file}, - io::ErrorKind, - path::Path, - path::PathBuf, -}; +use std::ffi::OsString; use termion::input::TermRead; -use tui::backend::TestBackend; -use tui_react::Terminal; - -const FIXTURE_PATH: &'static str = "tests/fixtures"; -fn debug(item: impl fmt::Debug) -> String { - format!("{:?}", item) -} +pub const FIXTURE_PATH: &'static str = "tests/fixtures"; #[test] fn it_can_handle_ending_traversal_reaching_top_but_skipping_levels() -> Result<(), Error> { @@ -54,44 +37,6 @@ fn it_can_handle_ending_traversal_without_reaching_the_top() -> Result<(), Error Ok(()) } -fn node_by_index(app: &TerminalApp, id: TreeIndex) -> &EntryData { - app.traversal.tree.node_weight(id).unwrap() -} - -fn node_by_name(app: &TerminalApp, name: impl AsRef<OsStr>) -> &EntryData { - node_by_index(app, index_by_name(&app, name)) -} - -fn index_by_name_and_size( - app: &TerminalApp, - name: impl AsRef<OsStr>, - size: Option<u64>, -) -> TreeIndex { - let name = name.as_ref(); - let t: Vec<_> = app - .traversal - .tree - .node_indices() - .map(|idx| (idx, node_by_index(app, idx))) - .filter_map(|(idx, e)| { - if e.name == name && size.map(|s| s == e.size).unwrap_or(true) { - Some(idx) - } else { - None - } - }) - .collect(); - match t.len() { - 1 => t[0], - 0 => panic!("Node named '{}' not found in tree", name.to_string_lossy()), - n => panic!("Node named '{}' found {} times", name.to_string_lossy(), n), - } -} - -fn index_by_name(app: &TerminalApp, name: impl AsRef<OsStr>) -> TreeIndex { - index_by_name_and_size(app, name, None) -} - #[test] fn simple_user_journey_read_only() -> Result<(), Error> { let long_root = "sample-02/dir"; @@ -341,214 +286,283 @@ fn simple_user_journey_read_only() -> Result<(), Error> { Ok(()) } -struct WritableFixture { - root: PathBuf, +#[test] +fn basic_user_journey_with_deletion() -> Result<(), Error> { + let fixture = WritableFixture::from("sample-02"); + let (mut _terminal, mut _app) = + initialized_app_and_terminal_from_paths(&[fixture.root.clone()])?; + Ok(()) } -impl Drop for WritableFixture { - fn drop(&mut self) { - delete_recursive(&self.root).unwrap(); +mod utils { + use crate::interactive::app_test::FIXTURE_PATH; + use crate::interactive::TerminalApp; + use dua::{ + traverse::{EntryData, Tree, TreeIndex}, + ByteFormat, Color, TraversalSorting, WalkOptions, + }; + use failure::{Error, ResultExt}; + use itertools::Itertools; + use jwalk::{DirEntry, WalkDir}; + use petgraph::prelude::NodeIndex; + use std::{ + env::temp_dir, + ffi::OsStr, + ffi::OsString, + fmt, + fs::{copy, create_dir_all, remove_dir, remove_file}, + io::ErrorKind, + path::Path, + path::PathBuf, + }; + use tui::backend::TestBackend; + use tui_react::Terminal; + + pub fn node_by_index(app: &TerminalApp, id: TreeIndex) -> &EntryData { + app.traversal.tree.node_weight(id).unwrap() } -} -fn delete_recursive(path: impl AsRef<Path>) -> Result<(), Error> { - let mut files: Vec<_> = Vec::new(); - let mut dirs: Vec<_> = Vec::new(); + pub fn node_by_name(app: &TerminalApp, name: impl AsRef<OsStr>) -> &EntryData { + node_by_index(app, index_by_name(&app, name)) + } - for entry in WalkDir::new(&path).num_threads(1).into_iter() { - let entry: DirEntry = entry.unwrap(); - let p = entry.path(); - match p.is_dir() { - true => dirs.push(p), - false => files.push(p), + pub fn index_by_name_and_size( + app: &TerminalApp, + name: impl AsRef<OsStr>, + size: Option<u64>, + ) -> TreeIndex { + let name = name.as_ref(); + let t: Vec<_> = app + .traversal + .tree + .node_indices() + .map(|idx| (idx, node_by_index(app, idx))) + .filter_map(|(idx, e)| { + if e.name == name && size.map(|s| s == e.size).unwrap_or(true) { + Some(idx) + } else { + None + } + }) + .collect(); + match t.len() { + 1 => t[0], + 0 => panic!("Node named '{}' not found in tree", name.to_string_lossy()), + n => panic!("Node named '{}' found {} times", name.to_string_lossy(), n), } } - files - .iter() - .map(|f| remove_file(f).map_err(Error::from)) - .chain( - dirs.iter() - .sorted_by_key(|p| p.components().count()) - .rev() - .map(|d| { - remove_dir(d) - .with_context(|_| format!("Could not delete '{}'", d.display())) - .map_err(Error::from) - }), - ) - .collect::<Result<_, _>>() -} + pub fn index_by_name(app: &TerminalApp, name: impl AsRef<OsStr>) -> TreeIndex { + index_by_name_and_size(app, name, None) + } -fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> { - for entry in WalkDir::new(&src).num_threads(1).into_iter() { - let entry: DirEntry = entry?; - let entry_path = entry.path(); - entry_path - .strip_prefix(&src) - .map_err(Error::from) - .and_then(|relative_entry_path| { - let dst = dst.as_ref().join(relative_entry_path); - if entry_path.is_dir() { - create_dir_all(dst).map_err(Into::into) - } else { - copy(&entry_path, dst) - .map(|_| ()) - .or_else(|e| match e.kind() { - ErrorKind::AlreadyExists => Ok(()), - _ => Err(e), - }) - .map_err(Into::into) - } - })?; + pub struct WritableFixture { + pub root: PathBuf, } - Ok(()) -} -impl From<&'static str> for WritableFixture { - fn from(fixture_name: &str) -> Self { - const TEMP_TLD_DIRNAME: &'static str = "dua-unit"; + impl Drop for WritableFixture { + fn drop(&mut self) { + delete_recursive(&self.root).unwrap(); + } + } + + fn delete_recursive(path: impl AsRef<Path>) -> Result<(), Error> { + let mut files: Vec<_> = Vec::new(); + let mut dirs: Vec<_> = Vec::new(); - let src = fixture(fixture_name); - let dst = temp_dir().join(TEMP_TLD_DIRNAME); - create_dir_all(&dst).unwrap(); + for entry in WalkDir::new(&path).num_threads(1).into_iter() { + let entry: DirEntry = entry.unwrap(); + let p = entry.path(); + match p.is_dir() { + true => dirs.push(p), + false => files.push(p), + } + } - let dst = dst.join(fixture_name); - copy_recursive(src, &dst).unwrap(); - WritableFixture { root: dst } + files + .iter() + .map(|f| remove_file(f).map_err(Error::from)) + .chain( + dirs.iter() + .sorted_by_key(|p| p.components().count()) + .rev() + .map(|d| { + remove_dir(d) + .with_context(|_| format!("Could not delete '{}'", d.display())) + .map_err(Error::from) + }), + ) + .collect::<Result<_, _>>() } -} -#[test] -fn basic_user_journey_with_deletion() -> Result<(), Error> { - let fixture = WritableFixture::from("sample-02"); - let (mut _terminal, mut _app) = - initialized_app_and_terminal_from_paths(&[fixture.root.clone()])?; - Ok(()) -} + fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Error> { + for entry in WalkDir::new(&src).num_threads(1).into_iter() { + let entry: DirEntry = entry?; + let entry_path = entry.path(); + entry_path + .strip_prefix(&src) + .map_err(Error::from) + .and_then(|relative_entry_path| { + let dst = dst.as_ref().join(relative_entry_path); + if entry_path.is_dir() { + create_dir_all(dst).map_err(Into::into) + } else { + copy(&entry_path, dst) + .map(|_| ()) + .or_else(|e| match e.kind() { + ErrorKind::AlreadyExists => Ok(()), + _ => Err(e), + }) + .map_err(Into::into) + } + })?; + } + Ok(()) + } -fn fixture(p: impl AsRef<Path>) -> PathBuf { - Path::new(FIXTURE_PATH).join(p) -} + impl From<&'static str> for WritableFixture { + fn from(fixture_name: &str) -> Self { + const TEMP_TLD_DIRNAME: &'static str = "dua-unit"; -fn fixture_str(p: impl AsRef<Path>) -> String { - fixture(p).to_str().unwrap().to_owned() -} + let src = fixture(fixture_name); + let dst = temp_dir().join(TEMP_TLD_DIRNAME); + create_dir_all(&dst).unwrap(); -fn initialized_app_and_terminal_with_closure<P: AsRef<Path>>( - fixture_paths: &[P], - mut convert: impl FnMut(&Path) -> PathBuf, -) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { - let mut terminal = Terminal::new(TestBackend::new(40, 20))?; - std::env::set_current_dir(Path::new(env!("CARGO_MANIFEST_DIR")))?; - - let input = fixture_paths.iter().map(|c| convert(c.as_ref())).collect(); - let app = TerminalApp::initialize( - &mut terminal, - WalkOptions { - threads: 1, - byte_format: ByteFormat::Metric, - color: Color::None, - sorting: TraversalSorting::AlphabeticalByFileName, - }, - input, - )?; - Ok((terminal, app)) -} -fn initialized_app_and_terminal_from_paths( - fixture_paths: &[PathBuf], -) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { - fn to_path_buf(p: &Path) -> PathBuf { - p.to_path_buf() + let dst = dst.join(fixture_name); + copy_recursive(src, &dst).unwrap(); + WritableFixture { root: dst } + } } - initialized_app_and_terminal_with_closure(fixture_paths, to_path_buf) -} -fn initialized_app_and_terminal_from_fixture( - fixture_paths: &[&str], -) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { - initialized_app_and_terminal_with_closure(fixture_paths, |p| fixture(p)) -} -fn sample_01_tree() -> Tree { - let mut t = Tree::new(); - { - let mut add_node = make_add_node(&mut t); - let root_size = 1259070; - let r = add_node("", root_size, None); + pub fn fixture(p: impl AsRef<Path>) -> PathBuf { + Path::new(FIXTURE_PATH).join(p) + } + + pub fn fixture_str(p: impl AsRef<Path>) -> String { + fixture(p).to_str().unwrap().to_owned() + } + + pub fn initialized_app_and_terminal_with_closure<P: AsRef<Path>>( + fixture_paths: &[P], + mut convert: impl FnMut(&Path) -> PathBuf, + ) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { + let mut terminal = Terminal::new(TestBackend::new(40, 20))?; + std::env::set_current_dir(Path::new(env!("CARGO_MANIFEST_DIR")))?; + + let input = fixture_paths.iter().map(|c| convert(c.as_ref())).collect(); + let app = TerminalApp::initialize( + &mut terminal, + WalkOptions { + threads: 1, + byte_format: ByteFormat::Metric, + color: Color::None, + sorting: TraversalSorting::AlphabeticalByFileName, + }, + input, + )?; + Ok((terminal, app)) + } + + pub fn initialized_app_and_terminal_from_paths( + fixture_paths: &[PathBuf], + ) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { + fn to_path_buf(p: &Path) -> PathBuf { + p.to_path_buf() + } + initialized_app_and_terminal_with_closure(fixture_paths, to_path_buf) + } + + pub fn initialized_app_and_terminal_from_fixture( + fixture_paths: &[&str], + ) -> Result<(Terminal<TestBackend>, TerminalApp), Error> { + initialized_app_and_terminal_with_closure(fixture_paths, |p| fixture(p)) + } + + pub fn sample_01_tree() -> Tree { + let mut t = Tree::new(); { - let s = add_node(&fixture_str("sample-01"), root_size, Some(r)); + let mut add_node = make_add_node(&mut t); + let root_size = 1259070; + let r = add_node("", root_size, None); { - add_node(".hidden.666", 666, Some(s)); - add_node("a", 256, Some(s)); - add_node("b.empty", 0, Some(s)); - add_node("c.lnk", 1, Some(s)); - let d = add_node("dir", 1258024, Some(s)); + let s = add_node(&fixture_str("sample-01"), root_size, Some(r)); { - add_node("1000bytes", 1000, Some(d)); - add_node("dir-a.1mb", 1_000_000, Some(d)); - add_node("dir-a.kb", 1024, Some(d)); - let e = add_node("empty-dir", 0, Some(d)); - { - add_node(".gitkeep", 0, Some(e)); - } - let sub = add_node("sub", 256_000, Some(d)); + add_node(".hidden.666", 666, Some(s)); + add_node("a", 256, Some(s)); + add_node("b.empty", 0, Some(s)); + add_node("c.lnk", 1, Some(s)); + let d = add_node("dir", 1258024, Some(s)); { - add_node("dir-sub-a.256kb", 256_000, Some(sub)); + add_node("1000bytes", 1000, Some(d)); + add_node("dir-a.1mb", 1_000_000, Some(d)); + add_node("dir-a.kb", 1024, Some(d)); + let e = add_node("empty-dir", 0, Some(d)); + { + add_node(".gitkeep", 0, Some(e)); + } + let sub = add_node("sub", 256_000, Some(d)); + { + add_node("dir-sub-a.256kb", 256_000, Some(sub)); + } } + add_node("z123.b", 123, Some(s)); } - add_node("z123.b", 123, Some(s)); } } + t } - t -} -fn sample_02_tree() -> Tree { - let mut t = Tree::new(); - { - let mut add_node = make_add_node(&mut t); - let root_size = 1540; - let r = add_node("", root_size, None); + pub fn sample_02_tree() -> Tree { + let mut t = Tree::new(); { - let s = add_node( - format!("{}/{}", FIXTURE_PATH, "sample-02").as_str(), - root_size, - Some(r), - ); + let mut add_node = make_add_node(&mut t); + let root_size = 1540; + let r = add_node("", root_size, None); { - add_node("a", 256, Some(s)); - add_node("b", 1, Some(s)); - let d = add_node("dir", 1283, Some(s)); + let s = add_node( + format!("{}/{}", FIXTURE_PATH, "sample-02").as_str(), + root_size, + Some(r), + ); { - add_node("c", 257, Some(d)); - add_node("d", 2, Some(d)); - let e = add_node("empty-dir", 0, Some(d)); - { - add_node(".gitkeep", 0, Some(e)); - } - let sub = add_node("sub", 1024, Some(d)); + add_node("a", 256, Some(s)); + add_node("b", 1, Some(s)); + let d = add_node("dir", 1283, Some(s)); { - add_node("e", 1024, Some(sub)); + add_node("c", 257, Some(d)); + add_node("d", 2, Some(d)); + let e = add_node("empty-dir", 0, Some(d)); + { + add_node(".gitkeep", 0, Some(e)); + } + let sub = add_node("sub", 1024, Some(d)); + { + add_node("e", 1024, Some(sub)); + } } } } } + t } - t -} -fn make_add_node<'a>( - t: &'a mut Tree, -) -> impl FnMut(&str, u64, Option<NodeIndex>) -> NodeIndex + 'a { - move |name, size, maybe_from_idx| { - let n = t.add_node(EntryData { - name: OsString::from(name), - size, - metadata_io_error: false, - }); - if let Some(from) = maybe_from_idx { - t.add_edge(from, n, ()); + pub fn make_add_node<'a>( + t: &'a mut Tree, + ) -> impl FnMut(&str, u64, Option<NodeIndex>) -> NodeIndex + 'a { + move |name, size, maybe_from_idx| { + let n = t.add_node(EntryData { + name: OsString::from(name), + size, + metadata_io_error: false, + }); + if let Some(from) = maybe_from_idx { + t.add_edge(from, n, ()); + } + n } - n + } + + pub fn debug(item: impl fmt::Debug) -> String { + format!("{:?}", item) } } |