oUnit2 and bracket_tmpfile usage

I need to test functions that write in some files. Before I knew that OUnit.bracket_tmpfile exists, I used my own functions in order to create the files and remove them like this :

type file = {
  name: string;
  descr : Pervasives.out_channel;
}

type files = {
  ml : file;
  mli : file;
}

let file_in_create_append_mode name =
  Pervasives.open_out_gen [Open_append; Open_creat] 0o666 name

let generate_sources base_name =
  let name = base_name ^ ".ml" in
  let descr = file_in_create_append_mode name in
  let ml = {name; descr} in
  let name = base_name ^ ".mli" in
  let descr = file_in_create_append_mode name in
  let mli = {name; descr} in
  {ml; mli}

Then in my tests I used this :

let assert_file_exists filename =
  assert_equal_boolean true (Sys.file_exists filename)

let file_content_to_string in_ch =
  let rec read_line acc =
    try
      let line = input_line in_ch in read_line (line :: acc)
    with
      End_of_file -> acc
  in let lines = List.rev (read_line []) in
  String.concat "\n" lines

let check_file_and_content name content =
  assert_file_exists name;
  let input_ch = open_in name in
  let lines = file_content_to_string input_ch in
  let _ = assert_equal_string content lines in
  close_in input_ch;
  Sys.remove name

let test_writing info namespace name writer mli_content ml_content =
      let open Builder in
      let filename = String.concat "_" [namespace; name; "test"] in
      let tmp_files = Builder.generate_sources filename in
      let descrs = (tmp_files.mli.descr, tmp_files.ml.descr) in
      let _ = writer name info descrs in
      let _ = Builder.close_sources tmp_files in
      let _ = check_file_and_content tmp_files.mli.name mli_content in
      check_file_and_content tmp_files.ml.name ml_content

Everything worked fine, now I want to use OUnit.bracket_tempfile because it simplify a lot of things but now the all the tests fail. The temp files are created but it seems that they are empty. Here is the code :

let assert_file_exists filename =
  assert_equal_boolean true (Sys.file_exists filename)

let file_content_to_string in_ch =
  let rec read_line acc =
    try
      let line = input_line in_ch in read_line (line :: acc)
    with
      End_of_file -> acc
  in let lines = List.rev (read_line []) in
  String.concat "\n" lines

let check_file_and_content name content =
  assert_file_exists name;
  let input_ch = open_in name in
  let lines = file_content_to_string input_ch in
  let _ = assert_equal_string content lines in
  close_in input_ch

let test_writing test_ctxt info namespace name writer mli_content ml_content =
      let open Builder in
      let mode = [Open_creat;
                  Open_append;
                  Open_text] in
      let (mli_name, mli_descr) = bracket_tmpfile ~suffix:"mli" ~mode:mode test_ctxt in
      let (ml_name, ml_descr) = bracket_tmpfile ~suffix:"ml" ~mode:mode test_ctxt in
      let descrs = (mli_descr, ml_descr) in
      let _ = writer name info descrs in
      let _ = check_file_and_content mli_name mli_content in
      check_file_and_content ml_name ml_content

I found the problem: I don’t close the file descriptors after the writing with Pervasives.close_out. Here is the working code :

let test_writing test_ctxt info namespace name writer mli_content ml_content =
      let open Builder in
      let (mli_name, mli_descr) = bracket_tmpfile ~suffix:"mli" test_ctxt in
      let (ml_name, ml_descr) = bracket_tmpfile ~suffix:"ml" test_ctxt in
      let descrs = (mli_descr, ml_descr) in
      let _ = writer name info descrs in
      let _ = Pervasives.close_out mli_descr in
      let _ = Pervasives.close_out ml_descr in
      let _ = check_file_and_content mli_name mli_content in
      check_file_and_content ml_name ml_content