Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eio-luv: improve process.md test #414

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib_eio_luv/tests/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ module Process = Eio_luv.Low_level.Process
A helper function for reading all of the bytes from a handle.

```ocaml
let read_all handle buf =
let rec read acc =
let read_all handle =
let buf = Luv.Buffer.create 32 in
let acc_buffer = Buffer.create 42 in
let rec read () =
match Eio_luv.Low_level.Stream.read_into handle buf with
| i -> read (acc + i)
| exception End_of_file -> acc
in read 0
| i ->
Buffer.add_string acc_buffer
(Luv.Buffer.to_string (Luv.Buffer.sub buf ~offset:0 ~length:i));
read ()
| exception End_of_file -> Buffer.contents acc_buffer
in read ()
```

A simple `echo hello` process redirects to stdout.
Expand All @@ -39,14 +44,13 @@ Using a pipe to redirect output to a buffer.
# Eio_luv.run @@ fun _env ->
Switch.run @@ fun sw ->
let parent_pipe = Eio_luv.Low_level.Pipe.init ~sw () in
let buf = Luv.Buffer.create 32 in
let redirect = Eio_luv.Low_level.Process.[
to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe ()
] in
let t = Process.spawn ~sw ~redirect "echo" [ "echo"; "Hello,"; "World!" ] in
let read = read_all parent_pipe buf in
let result = read_all parent_pipe in
let _ = Process.await_exit t in
Luv.Buffer.to_string (Luv.Buffer.sub buf ~offset:0 ~length:read);;
result;;
- : string = "Hello, World!\n"
```

Expand Down