Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_cli_interactive.rs « tests - github.com/windirstat/mft.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41c179a277381c9408b0a149f14a76e9617b2767 (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
// The interactive tests are in a separate file,
// since they use `rexpect`, which internally uses quirky fork semantics to open a pty.
// They will fail if tried to be executed concurrently any other CLI test.

#[cfg(not(target_os = "windows"))]
mod fixtures;

#[cfg(not(target_os = "windows"))]
mod cli_tests {
    use super::fixtures::*;

    use std::fs::File;
    use std::io::{Read, Write};
    use tempfile::tempdir;

    use assert_cmd::cargo::cargo_bin;
    use rexpect::spawn;

    // It should behave the same on windows, but interactive testing relies on unix pty internals.
    #[test]
    fn test_it_confirms_before_overwriting_a_file() {
        let d = tempdir().unwrap();
        let f = d.as_ref().join("test.out");

        let mut file = File::create(&f).unwrap();
        file.write_all(b"I'm a file!").unwrap();

        let sample = mft_sample();

        let cmd_string = format!(
            "{bin} -f {output_file} {sample}",
            bin = cargo_bin("mft_dump").display(),
            output_file = f.to_string_lossy(),
            sample = sample.to_str().unwrap()
        );

        let mut p = spawn(&cmd_string, Some(20000)).unwrap();
        p.exp_regex(r#"Are you sure you want to override.*"#)
            .unwrap();
        p.send_line("y").unwrap();
        p.exp_eof().unwrap();

        let mut expected = vec![];

        File::open(&f).unwrap().read_to_end(&mut expected).unwrap();
        assert!(
            !expected.len() > 100,
            "Expected output to be printed to file"
        )
    }

    #[test]
    fn test_it_confirms_before_overwriting_a_file_and_quits() {
        let d = tempdir().unwrap();
        let f = d.as_ref().join("test.out");

        let mut file = File::create(&f).unwrap();
        file.write_all(b"I'm a file!").unwrap();

        let sample = mft_sample();

        let cmd_string = format!(
            "{bin} -f {output_file} {sample}",
            bin = cargo_bin("mft_dump").display(),
            output_file = f.to_string_lossy(),
            sample = sample.to_str().unwrap()
        );

        let mut p = spawn(&cmd_string, Some(20000)).unwrap();
        p.exp_regex(r#"Are you sure you want to override.*"#)
            .unwrap();
        p.send_line("n").unwrap();
        p.exp_eof().unwrap();

        let mut expected = vec![];

        File::open(&f).unwrap().read_to_end(&mut expected).unwrap();
        assert!(
            !expected.len() > 100,
            "Expected output to be printed to file"
        )
    }
}