summaryrefslogtreecommitdiffstats
path: root/tests/interactive.rs
blob: f5a97f5cd3f65eecf8aea779764950b76cdcf445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
mod app {
    use dua::interactive::{EntryData, TerminalApp, Tree};
    use dua::{ByteFormat, Color, Sorting, WalkOptions};
    use failure::Error;
    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 app) = initialized_app_and_terminal("sample-01")?;
        let mut expected_tree = sample_01_tree();

        assert_eq!(
            debug(app.tree.node_weights_mut().collect::<Vec<_>>()),
            debug(expected_tree.node_weights_mut().collect::<Vec<_>>()),
            "filesystem graph is stable and matches the directory structure"
        );
        Ok(())
    }

    fn initialized_app_and_terminal(
        fixture_path: &str,
    ) -> Result<(Terminal<TestBackend>, TerminalApp), 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 = TerminalApp::initialize(
            &mut terminal,
            WalkOptions {
                threads: 1,
                byte_format: ByteFormat::Metric,
                color: Color::None,
                sorting: Sorting::AlphabeticalByFileName,
            },
            vec![input],
        )?;
        Ok((terminal, app))
    }

    fn sample_01_tree() -> Tree {
        let mut t = Tree::new();
        let mut add_node = |name, size| {
            t.add_node(EntryData {
                name: OsString::from(name),
                size,
                metadata_io_error: false,
            });
        };
        add_node("", 0);
        add_node("sample-01", 0);
        add_node(".hidden.666", 666);
        add_node("a", 256);
        add_node("b.empty", 0);
        add_node("c.lnk", 1);
        add_node("dir", 0);
        add_node("1000bytes", 1000);
        add_node("dir-a.1mb", 1_000_000);
        add_node("dir-a.kb", 1024);
        add_node("empty-dir", 0);
        add_node(".gitkeep", 0);
        add_node("sub", 0);
        add_node("dir-sub-a.256kb", 256_000);
        t
    }
}