66open Test
77open Lwt.Infix
88
9- let expected = " the quick brown fox jumps over the lazy dog"
9+ let expected_str = " the quick brown fox jumps over the lazy dog"
10+ let expected = Bytes. of_string expected_str
11+ let expected_len = Bytes. length expected
12+
13+ let check_status ?(status =(= ) 0 ) = function
14+ | Unix. WEXITED n when status n -> Lwt. return_true
15+ | Unix. WEXITED n ->
16+ Printf. eprintf " exited with code %d" n;
17+ Lwt. return_false
18+ | Unix. WSIGNALED x ->
19+ Printf. eprintf " failed with signal %d" x;
20+ Lwt. return_false
21+ | Unix. WSTOPPED x ->
22+ Printf. eprintf " stopped with signal %d" x;
23+ Lwt. return_false
24+
25+ let pwrite ~stdin pout =
26+ let args = [|" dummy.exe" ; " read" |] in
27+ let proc = Lwt_process. exec ~stdin (" ./dummy.exe" , args) in
28+ let write = Lwt. finalize
29+ (fun () -> Lwt_unix. write pout expected 0 expected_len)
30+ (fun () -> Lwt_unix. close pout) in
31+ proc >> = fun r ->
32+ write >> = fun n ->
33+ assert (n = expected_len);
34+ check_status r
35+
36+ let pread ?stdout ?stderr pin =
37+ let buf = Bytes. create expected_len in
38+ let proc = match stdout, stderr with
39+ | Some stdout , None ->
40+ let args = [|" dummy.exe" ; " write" |] in
41+ Lwt_process. exec ~stdout (" ./dummy.exe" , args)
42+ | None , Some stderr ->
43+ let args = [|" dummy.exe" ; " errwrite" |] in
44+ Lwt_process. exec ~stderr (" ./dummy.exe" , args)
45+ | _ -> assert false
46+ in
47+ let read = Lwt_unix. read pin buf 0 expected_len in
48+ proc >> = fun r ->
49+ read >> = fun n ->
50+ assert (n = expected_len);
51+ assert (Bytes. equal buf expected);
52+ Lwt_unix. read pin buf 0 1 >> = fun n ->
53+ assert (n = 0 );
54+ check_status r
1055
1156let suite = suite " lwt_process" [
1257 (* The sleep command is not available on Win32. *)
@@ -20,42 +65,43 @@ let suite = suite "lwt_process" [
2065 (fun _ -> Lwt. return " " ))
2166 >> = fun _ -> Lwt. return_true);
2267
23- test " pread "
68+ test " subproc stdout can be redirected to null "
2469 (fun () ->
2570 let args = [|" dummy.exe" ; " write" |] in
26- Lwt_process. pread ~stdin: `Close ~stderr: `Close (" ./dummy.exe" , args)
27- > |= fun actual ->
28- actual = expected);
71+ Lwt_process. exec ~stdout: `Dev_null (" ./dummy.exe" , args)
72+ >> = check_status);
2973
30- test " pread keep "
74+ test " subproc stderr can be redirected to null "
3175 (fun () ->
32- let args = [|" dummy.exe" ; " write" |] in
33- Lwt_process. pread ~stdin: `Keep ~stderr: `Keep (" ./dummy.exe" , args)
34- > |= fun actual ->
35- actual = expected);
76+ let args = [|" dummy.exe" ; " errwrite" |] in
77+ Lwt_process. exec ~stderr: `Dev_null (" ./dummy.exe" , args)
78+ >> = check_status);
3679
37- test " pread nul "
80+ test " subproc cannot write on closed stdout "
3881 (fun () ->
3982 let args = [|" dummy.exe" ; " write" |] in
40- Lwt_process. pread ~stdin: `Dev_null ~stderr: `Dev_null (" ./dummy.exe" , args)
41- > |= fun actual ->
42- actual = expected);
83+ let stderr = `Dev_null (* mask subproc stderr *) in
84+ Lwt_process. exec ~stdout: `Close ~stderr (" ./dummy.exe" , args)
85+ >> = check_status ~status: ((<> ) 0 ));
86+
87+ test " subproc cannot write on closed stderr"
88+ (fun () ->
89+ let args = [|" dummy.exe" ; " errwrite" |] in
90+ Lwt_process. exec ~stderr: `Close (" ./dummy.exe" , args)
91+ >> = check_status ~status: ((<> ) 0 ));
4392
44- test " pwrite "
93+ test " can write to subproc stdin "
4594 (fun () ->
46- let args = [|" dummy.exe" ; " read" |] in
47- Lwt_process. pwrite ~stdout: `Close ~stderr: `Close (" ./dummy.exe" , args) expected
48- >> = fun () -> Lwt. return_true);
95+ let pin, pout = Lwt_unix. pipe_out ~cloexec: true () in
96+ pwrite ~stdin: (`FD_move pin) pout);
4997
50- test " pwrite keep "
98+ test " can read from subproc stdout "
5199 (fun () ->
52- let args = [|" dummy.exe" ; " read" |] in
53- Lwt_process. pwrite ~stdout: `Keep ~stderr: `Keep (" ./dummy.exe" , args) expected
54- >> = fun () -> Lwt. return_true);
100+ let pin, pout = Lwt_unix. pipe_in ~cloexec: true () in
101+ pread ~stdout: (`FD_move pout) pin);
55102
56- test " pwrite nul "
103+ test " can read from subproc stderr "
57104 (fun () ->
58- let args = [|" dummy.exe" ; " read" |] in
59- Lwt_process. pwrite ~stdout: `Dev_null ~stderr: `Dev_null (" ./dummy.exe" , args) expected
60- >> = fun () -> Lwt. return_true);
105+ let pin, perr = Lwt_unix. pipe_in ~cloexec: true () in
106+ pread ~stderr: (`FD_move perr) pin);
61107]
0 commit comments