aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fixtures/mod.rs
blob: 08f09c1f0d6f74fd0c8e35ec952b2861b8206d1a (plain) (blame)
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
use pty_process::Command as _;
use std::io::{BufRead as _, Read as _};
use std::os::unix::io::AsRawFd as _;

pub struct Fixture {
    name: String,
    features: String,
    screenguard: bool,

    tempdir: assert_fs::TempDir,
}

impl Fixture {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            features: String::new(),
            screenguard: true,
            tempdir: assert_fs::TempDir::new().unwrap(),
        }
    }

    #[allow(dead_code)]
    pub fn features(&mut self, features: &str) {
        self.features = features.to_string();
    }

    #[allow(dead_code)]
    pub fn screenguard(&mut self, screenguard: bool) {
        self.screenguard = screenguard;
    }

    pub fn build(self) -> BuiltFixture {
        let Self {
            name,
            features,
            screenguard,
            tempdir,
        } = self;
        let run = escargot::CargoBuild::new()
            .bin(name)
            .current_release()
            .current_target()
            .manifest_path("tests/fixtures/bin/Cargo.toml")
            .target_dir(tempdir.path())
            .features(features)
            .run()
            .unwrap();

        BuiltFixture {
            _tempdir: tempdir,
            run,
            screenguard,
        }
    }
}

pub struct BuiltFixture {
    _tempdir: assert_fs::TempDir,
    run: escargot::CargoRun,
    screenguard: bool,
}

impl BuiltFixture {
    pub fn run<F: FnOnce(&mut std::fs::File)>(
        &mut self,
        args: &[&str],
        f: F,
    ) {
        let mut cmd = self.run.command();
        let mut child = cmd
            .args(args)
            .spawn_pty(Some(&pty_process::Size::new(24, 80)))
            .unwrap();

        if self.screenguard {
            assert!(read_ready(child.pty().as_raw_fd()));
            let mut buf = vec![0u8; 1024];
            let bytes = child.pty().read(&mut buf).unwrap();
            buf.truncate(bytes);
            assert_eq!(&buf[..], b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h");
        } else {
            std::thread::sleep(std::time::Duration::from_millis(100));
        }

        f(child.pty_mut());

        if self.screenguard {
            assert!(read_ready(child.pty().as_raw_fd()));
            let mut buf = vec![0u8; 1024];
            let bytes = child.pty().read(&mut buf).unwrap();
            buf.truncate(bytes);
            assert_eq!(&buf[..], b"\x1b[?47l\x1b8\x1b[?25h");
        }

        let status = child.wait().unwrap();
        assert!(status.success());
    }
}

#[allow(dead_code)]
#[track_caller]
pub fn read(f: &mut std::fs::File) -> Vec<u8> {
    assert!(read_ready(f.as_raw_fd()));
    let mut buf = vec![0u8; 1024];
    let bytes = f.read(&mut buf).unwrap();
    buf.truncate(bytes);
    buf
}

#[allow(dead_code)]
#[track_caller]
pub fn read_line(f: &mut std::io::BufReader<&mut std::fs::File>) -> Vec<u8> {
    assert!(!f.buffer().is_empty() || read_ready(f.get_ref().as_raw_fd()));
    let mut buf = vec![];
    f.read_until(b'\n', &mut buf).unwrap();
    buf
}

#[allow(dead_code)]
pub fn read_ready(fd: std::os::unix::io::RawFd) -> bool {
    let mut set = nix::sys::select::FdSet::new();
    set.insert(fd);
    let timeout = libc::timeval {
        tv_sec: 0,
        tv_usec: 100_000,
    };
    let timeout = &mut nix::sys::time::TimeVal::from(timeout);
    match nix::sys::select::select(
        None,
        Some(&mut set),
        None,
        None,
        Some(timeout),
    ) {
        Ok(n) => {
            if n > 0 {
                set.contains(fd)
            } else {
                false
            }
        }
        Err(_) => false,
    }
}