let bracket_chdir dir test_ctxt =
  let () =
    OUnitLogger.infof test_ctxt.logger "Change directory to %S" dir;
  in
  let () =
    try
      OUnitShared.Mutex.lock test_ctxt.shared chdir_mutex;
    with OUnitShared.Lock_failure ->
      failwith "Trying to do a nested chdir."
  in
  let cur_pwd =
    Sys.getcwd ()
  in
  create
    (fun test_ctxt -> Unix.chdir dir)
    (fun () test_ctxt ->
       Unix.chdir cur_pwd;
       OUnitShared.Mutex.unlock test_ctxt.shared chdir_mutex)
    test_ctxt