summaryrefslogtreecommitdiffstats
path: root/tests/test_exact_output.rs
blob: 1cc06f2b64b6ae970536e58087d1ea7ab5501e70 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use assert_cmd::Command;
use std::ffi::OsStr;
use std::str;
use std::sync::Once;

static INIT: Once = Once::new();

/**
 * This file contains tests that verify the exact output of the command.
 * This output differs on Linux / Mac so the tests are harder to write and debug
 * Windows is ignored here because the results vary by host making exact testing impractical
 *
 * Despite the above problems, these tests are good as they are the closest to 'the real thing'.
 */

//  Warning: File sizes differ on both platform and on the format of the disk.
/// Copy to /tmp dir - we assume that the formatting of the /tmp partition
/// is consistent. If the tests fail your /tmp filesystem probably differs
fn copy_test_data(dir: &str) {
    // First remove the existing directory - just in case it is there and has incorrect data
    let last_slash = dir.rfind('/').unwrap();
    let last_part_of_dir = dir.chars().skip(last_slash).collect::<String>();
    let _ = Command::new("rm")
        .arg("-rf")
        .arg("/tmp/".to_owned() + &*last_part_of_dir)
        .ok();

    let _ = Command::new("cp")
        .arg("-r")
        .arg(dir)
        .arg("/tmp/")
        .ok()
        .map_err(|err| eprintln!("Error copying directory for test setup\n{:?}", err));
}

fn initialize() {
    INIT.call_once(|| {
        copy_test_data("tests/test_dir");
        copy_test_data("tests/test_dir2");
        copy_test_data("tests/test_dir_unicode");
    });
}

fn exact_output_test<T: AsRef<OsStr>>(valid_outputs: Vec<String>, command_args: Vec<T>) {
    initialize();

    let mut a = &mut Command::cargo_bin("dust").unwrap();

    for p in command_args {
        a = a.arg(p);
    }

    let output = str::from_utf8(&a.unwrap().stdout).unwrap().to_owned();

    let will_fail = valid_outputs.iter().any(|i| output.contains(i));
    if !will_fail {
        eprintln!("output:\n{}\ndoes not contain any of:\n{:?}",output, valid_outputs.iter());
    }
    assert!(will_fail)
}

// "windows" result data can vary by host (size seems to be variable by one byte); fix code vs test and re-enable
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_main_basic() {
    // -c is no color mode - This makes testing much simpler
    exact_output_test(main_output(), vec!["-c", "-B", "/tmp/test_dir/"])
}

#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_main_multi_arg() {
    let command_args = vec![
        "-c",
        "-B",
        "/tmp/test_dir/many/",
        "/tmp/test_dir",
        "/tmp/test_dir",
    ];
    exact_output_test(main_output(), command_args);
}

fn main_output() -> Vec<String> {
    // Some linux currently thought to be Manjaro, Arch
    // Although probably depends on how drive is formatted
    let mac_and_some_linux = r#"
  0B     ┌── a_file    │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │   0%
4.0K     ├── hello_file│█████████████████████████████████████████████████ │ 100%
4.0K   ┌─┴ many        │█████████████████████████████████████████████████ │ 100%
4.0K ┌─┴ test_dir      │█████████████████████████████████████████████████ │ 100%
"#
    .trim()
    .to_string();

    let ubuntu = r#"
  0B     ┌── a_file    │                ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │   0%
4.0K     ├── hello_file│                ░░░░░░░░░░░░░░░░█████████████████ │  33%
8.0K   ┌─┴ many        │                █████████████████████████████████ │  67%
 12K ┌─┴ test_dir      │█████████████████████████████████████████████████ │ 100%
  "#
    .trim()
    .to_string();

    vec![mac_and_some_linux, ubuntu]
}

#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_main_long_paths() {
    let command_args = vec!["-c", "-p", "-B", "/tmp/test_dir/"];
    exact_output_test(main_output_long_paths(), command_args);
}

fn main_output_long_paths() -> Vec<String> {
    let mac_and_some_linux = r#"
  0B     ┌── /tmp/test_dir/many/a_file    │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │   0%
4.0K     ├── /tmp/test_dir/many/hello_file│██████████████████████████████ │ 100%
4.0K   ┌─┴ /tmp/test_dir/many             │██████████████████████████████ │ 100%
4.0K ┌─┴ /tmp/test_dir                    │██████████████████████████████ │ 100%
"#
    .trim()
    .to_string();
    let ubuntu = r#"
  0B     ┌── /tmp/test_dir/many/a_file    │         ░░░░░░░░░░░░░░░░░░░░█ │   0%
4.0K     ├── /tmp/test_dir/many/hello_file│         ░░░░░░░░░░███████████ │  33%
8.0K   ┌─┴ /tmp/test_dir/many             │         █████████████████████ │  67%
 12K ┌─┴ /tmp/test_dir                    │██████████████████████████████ │ 100%
"#
    .trim()
    .to_string();
    vec![mac_and_some_linux, ubuntu]
}

// Check against directories and files whose names are substrings of each other
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_substring_of_names_and_long_names() {
    let command_args = vec!["-c", "-B", "/tmp/test_dir2"];
    exact_output_test(no_substring_of_names_output(), command_args);
}

fn no_substring_of_names_output() -> Vec<String> {
    let ubuntu = "
  0B   ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes..
4.0K   ├── dir_name_clash
4.0K   │ ┌── hello
8.0K   ├─┴ dir
4.0K   │ ┌── hello
8.0K   ├─┴ dir_substring
 24K ┌─┴ test_dir2
    "
    .trim()
    .into();

    let mac_and_some_linux = "
  0B   ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes..
4.0K   │ ┌── hello
4.0K   ├─┴ dir
4.0K   ├── dir_name_clash
4.0K   │ ┌── hello
4.0K   ├─┴ dir_substring
 12K ┌─┴ test_dir2
  "
    .trim()
    .into();
    vec![mac_and_some_linux, ubuntu]
}

#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_unicode_directories() {
    let command_args = vec!["-c", "-B", "/tmp/test_dir_unicode"];
    exact_output_test(unicode_dir(), command_args);
}

fn unicode_dir() -> Vec<String> {
    // The way unicode & asian characters are rendered on the terminal should make this line up
    let ubuntu = "
  0B   ┌── ラウトは難しいです!.japan│                                  █ │   0%
  0B   ├── 👩.unicode                │                                  █ │   0%
4.0K ┌─┴ test_dir_unicode            │███████████████████████████████████ │ 100%
    "
    .trim()
    .into();

    let mac_and_some_linux = "
0B   ┌── ラウトは難しいです!.japan│                                    █ │   0%
0B   ├── 👩.unicode                │                                    █ │   0%
0B ┌─┴ test_dir_unicode            │                                    █ │   0%
    "
    .trim()
    .into();
    vec![mac_and_some_linux, ubuntu]
}

#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_apparent_size() {
    let command_args = vec!["-c", "-s", "-b", "/tmp/test_dir"];
    exact_output_test(apparent_size_output(), command_args);
}

fn apparent_size_output() -> Vec<String> {
    // The apparent directory sizes are too unpredictable and system dependent to try and match
    let files = r#"
  0B     ┌── a_file
  6B     ├── hello_file
 "#
    .trim()
    .to_string();

    vec![files]
}