tokio_process_tools/
lib.rs

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
mod collector;
mod inspector;
mod output_stream;
mod process_handle;
mod signal;
mod terminate_on_drop;

pub use collector::{Collector, CollectorError, Sink};
pub use inspector::{Inspector, InspectorError};
pub use output_stream::{OutputStream, OutputType, WaitError, WaitFor};
pub use process_handle::{IsRunning, ProcessHandle, TerminationError};
pub use terminate_on_drop::TerminateOnDrop;

#[cfg(test)]
mod test {
    use crate::ProcessHandle;
    use mockall::*;
    use std::fs::File;
    use std::io::Write;
    use tokio::process::Command;

    #[tokio::test]
    async fn test() {
        let cmd = Command::new("ls");
        let mut exec = ProcessHandle::spawn("ls", cmd).expect("Failed to spawn `ls` command");
        let (status, stdout, stderr) = exec.wait_with_output().await.unwrap();
        println!("{:?}", status);
        println!("{:?}", stdout);
        println!("{:?}", stderr);
    }

    #[tokio::test]
    async fn manually_await_with_sync_inspector() {
        let cmd = Command::new("ls");
        let mut exec = ProcessHandle::spawn("ls", cmd).expect("Failed to spawn `ls` command");

        #[automock]
        trait FunctionCaller {
            fn call_function(&self, input: String);
        }

        let mut out_mock = MockFunctionCaller::new();
        out_mock
            .expect_call_function()
            .with(predicate::eq("Cargo.lock".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("Cargo.toml".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("LICENSE-APACHE".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("LICENSE-MIT".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("README.md".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("src".to_string()))
            .times(1)
            .return_const(());
        out_mock
            .expect_call_function()
            .with(predicate::eq("target".to_string()))
            .times(1)
            .return_const(());
        let out_callback = move |input: String| {
            out_mock.call_function(input);
        };

        let err_mock = MockFunctionCaller::new();
        let err_callback = move |input: String| {
            err_mock.call_function(input);
        };

        let out_inspector = exec.stdout().inspect(move |line| {
            out_callback(line);
        });
        let err_inspector = exec.stderr().inspect(move |line| {
            err_callback(line);
        });

        let status = exec.wait().await.unwrap();
        let () = out_inspector.abort().await.unwrap();
        let () = err_inspector.abort().await.unwrap();

        println!("{:?}", status);
    }

    #[tokio::test]
    async fn manually_await_with_collectors() {
        let cmd = Command::new("ls");
        let mut exec = ProcessHandle::spawn("ls", cmd).expect("Failed to spawn `ls` command");

        let temp_file_out = tempfile::tempfile().unwrap();
        let temp_file_err = tempfile::tempfile().unwrap();

        let out_collector = exec.stdout().collect(temp_file_out, |line, temp_file| {
            writeln!(temp_file, "{}", line).unwrap();
        });
        let err_collector = exec.stderr().collect(temp_file_err, |line, temp_file| {
            writeln!(temp_file, "{}", line).unwrap();
        });

        let status = exec.wait().await.unwrap();
        let stdout = out_collector.abort().await.unwrap();
        let stderr = err_collector.abort().await.unwrap();

        println!("{:?}", status);
        println!("{:?}", stdout);
        println!("{:?}", stderr);
    }

    #[tokio::test]
    async fn manually_await_with_async_collectors() {
        let cmd = Command::new("ls");
        let mut exec = ProcessHandle::spawn("ls", cmd).expect("Failed to spawn `ls` command");

        let temp_file_out: File = tempfile::tempfile().unwrap();
        let temp_file_err: File = tempfile::tempfile().unwrap();

        let out_collector =
            exec.stdout()
                .collect_async(temp_file_out, |line, temp_file: &mut File| {
                    Box::pin(async move {
                        writeln!(temp_file, "{}", line).unwrap();
                        Ok(())
                    })
                });
        let err_collector =
            exec.stderr()
                .collect_async(temp_file_err, |line, temp_file: &mut File| {
                    Box::pin(async move {
                        writeln!(temp_file, "{}", line).unwrap();
                        Ok(())
                    })
                });

        let status = exec.wait().await.unwrap();
        let stdout = out_collector.abort().await.unwrap();
        let stderr = err_collector.abort().await.unwrap();

        println!("{:?}", status);
        println!("{:?}", stdout);
        println!("{:?}", stderr);
    }
}