Skip to content

Commit 4627e3c

Browse files
mrochraphael-proust
authored andcommitted
add test
1 parent 48abed7 commit 4627e3c

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

test/unix/dummy.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ let read () =
2323
let write fd =
2424
assert (test_input_len = Unix.write fd test_input 0 test_input_len)
2525

26+
let printenv () =
27+
(* stdout is in text mode by default, which converts \n to \r\n on Windows.
28+
switch to binary mode to prevent this, so the output is the same across
29+
platforms. *)
30+
set_binary_mode_out stdout true;
31+
Array.iter (Printf.printf "%s\n") (Unix.unsafe_environment ());
32+
flush stdout
33+
2634
let () =
2735
match Sys.argv.(1) with
2836
| "read" -> exit @@ if read () then 0 else 1
2937
| "write" -> write Unix.stdout
3038
| "errwrite" -> write Unix.stderr
39+
| "printenv" -> printenv ()
3140
| _ -> invalid_arg "Sys.argv"

test/unix/test_lwt_process.ml

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ open Lwt.Infix
88

99
let expected_str = "the quick brown fox jumps over the lazy dog"
1010
let expected = Bytes.of_string expected_str
11-
let expected_len = Bytes.length expected
1211

1312
let check_status ?(status=(=) 0) = function
1413
| Unix.WEXITED n when status n -> Lwt.return_true
@@ -22,7 +21,8 @@ let check_status ?(status=(=) 0) = function
2221
Printf.eprintf "stopped with signal %d" x;
2322
Lwt.return_false
2423

25-
let pwrite ~stdin pout =
24+
let pwrite ~stdin pout expected =
25+
let expected_len = Bytes.length expected in
2626
let args = [|"dummy.exe"; "read"|] in
2727
let proc = Lwt_process.exec ~stdin ("./dummy.exe", args) in
2828
let write = Lwt.finalize
@@ -33,26 +33,47 @@ let pwrite ~stdin pout =
3333
assert (n = expected_len);
3434
check_status r
3535

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
36+
let read_all ic buf ofs len =
37+
let rec loop ic buf ofs len =
38+
Lwt_unix.read ic buf ofs len >>= function
39+
| 0 ->
40+
Lwt.return ofs
41+
| n ->
42+
let ofs = ofs + n in
43+
let len = len - n in
44+
if len = 0 then
45+
Lwt.return ofs
46+
else
47+
loop ic buf ofs len
4648
in
47-
let read = Lwt_unix.read pin buf 0 expected_len in
49+
loop ic buf ofs len
50+
51+
let pread ?env ?stdout ?stderr pin cmd expected =
52+
(match stdout, stderr with
53+
| Some _, None
54+
| None, Some _ ->
55+
()
56+
| _ -> assert false);
57+
let expected_len = Bytes.length expected in
58+
let buf = Bytes.create expected_len in
59+
let args = [|"dummy.exe"; cmd|] in
60+
let proc = Lwt_process.exec ?env ?stdout ?stderr ("./dummy.exe", args) in
61+
let read = read_all pin buf 0 expected_len in
4862
proc >>= fun r ->
4963
read >>= fun n ->
50-
assert (n = expected_len);
64+
(if n <> expected_len then Printf.ksprintf failwith "expected %d bytes, got %d" expected_len n);
5165
assert (Bytes.equal buf expected);
5266
Lwt_unix.read pin buf 0 1 >>= fun n ->
53-
assert (n = 0);
67+
if n <> 0 then Printf.ksprintf failwith "expected 0 bytes remaining, got %d" n;
5468
check_status r
5569

70+
let bytes_of_env env =
71+
env
72+
|> Array.map (Printf.sprintf "%s\n")
73+
|> Array.to_list
74+
|> String.concat ""
75+
|> Bytes.of_string
76+
5677
let suite = suite "lwt_process" [
5778
(* The sleep command is not available on Win32. *)
5879
test "lazy_undefined" ~only_if:(fun () -> not Sys.win32)
@@ -93,15 +114,36 @@ let suite = suite "lwt_process" [
93114
test "can write to subproc stdin"
94115
(fun () ->
95116
let pin, pout = Lwt_unix.pipe_out ~cloexec:true () in
96-
pwrite ~stdin:(`FD_move pin) pout);
117+
pwrite ~stdin:(`FD_move pin) pout expected);
97118

98119
test "can read from subproc stdout"
99120
(fun () ->
100121
let pin, pout = Lwt_unix.pipe_in ~cloexec:true () in
101-
pread ~stdout:(`FD_move pout) pin);
122+
pread ~stdout:(`FD_move pout) pin "write" expected);
102123

103124
test "can read from subproc stderr"
104125
(fun () ->
105126
let pin, perr = Lwt_unix.pipe_in ~cloexec:true () in
106-
pread ~stderr:(`FD_move perr) pin);
127+
pread ~stderr:(`FD_move perr) pin "errwrite" expected);
128+
129+
test "overrides env"
130+
(fun () ->
131+
let env = [| "FOO=1" |] in
132+
let expected = Bytes.of_string "FOO=1\n" in
133+
let pin, pout = Lwt_unix.pipe_in ~cloexec:true () in
134+
pread ~env ~stdout:(`FD_move pout) pin "printenv" expected);
135+
136+
test "passes env"
137+
(fun () ->
138+
let env = Unix.unsafe_environment () in
139+
let expected = bytes_of_env env in
140+
let pin, pout = Lwt_unix.pipe_in ~cloexec:true () in
141+
pread ~env ~stdout:(`FD_move pout) pin "printenv" expected);
142+
143+
test "inherits env"
144+
(fun () ->
145+
let env = Unix.unsafe_environment () in
146+
let expected = bytes_of_env env in
147+
let pin, pout = Lwt_unix.pipe_in ~cloexec:true () in
148+
pread ?env:None ~stdout:(`FD_move pout) pin "printenv" expected);
107149
]

0 commit comments

Comments
 (0)