gildor478 / ounit

Other
88 stars 18 forks source link

assert_command foutput of OUnit 2.2.6 #93

Open akr opened 2 years ago

akr commented 2 years ago

I found OUnit 2.2.6 changes the type of foutput option for assert_command. https://github.com/gildor478/ounit/commit/9345a4767b907e391ab48c6ccb3241015787e881

The sequence is created by seq_forever (same as Seq.forever for OCaml 4.14) which returns an infinite sequence. The end of the sequence is notified by End_of_file exception raised by input_char.

It is different from OUnit 2.2.5 which creates a finite channel by Stream.of_channel.

The difference between "finite" and "infinite" causes us to need to catch End_of_file exception.

We can write as follows in OUnit 2.2.5.

open OUnit2

let test_foo (ctxt : test_ctxt) : unit =
  assert_command
    ~foutput:(fun stream -> Stream.iter (fun ch -> ignore ch) stream)
    ~ctxt
    "echo" ["foo"]

let () =
  run_test_tt_main ("foo" >:: test_foo)

We need to rewrite it as follows with OUnit 2.2.6. It needs try-with addition to modifying from Stream.iter to Seq.iter.

open OUnit2

let test_foo (ctxt : test_ctxt) : unit =
  assert_command 
    ~foutput:(fun seq -> try 
                           Seq.iter (fun ch -> ignore ch) seq
                         with End_of_file ->
                           ())
    ~ctxt
    "echo" ["foo"]

let () =
  run_test_tt_main ("foo" >:: test_foo)

I'm not sure why Stream.t is changed to Seq.t.

Even if Seq.t has a good reason, I think it is better to catch End_of_file exception in OUnit and produce a finite sequence as OUnit 2.2.5.

akr commented 2 years ago

I found the reason to avoid Stream.t: OCaml 4.14.0 deprecated Stream. https://ocaml.org/releases/4.14.0

I still think that it is better to avoid End_of_file exception for foutput. I'm happy if I can adapt newer OCaml and OUnit by just changing Stream.iter to Seq.iter without try-with.