<commit_before>(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)



open Test
open Lwt.Infix

let assert_fd_closed = "ASSERT_FD_CLOSED"
let assert_fd_open   = "ASSERT_FD_OPEN"

let test_cloexec assertion flags =
  if Sys.win32 then Lwt.return true
  else
    Lwt_unix.openfile "/dev/zero" (Unix.O_RDONLY :: flags) 0o644 >>= fun fd ->
    let fd_ = Lwt_unix.unix_file_descr fd in
    match Lwt_unix.fork () with
      | 0 ->
          Unix.putenv assertion (string_of_int @@ Obj.magic fd_);
          (* There's no portable way to obtain the executable name (which
           * may even no longer exist at this point), but argv[0] fortunately
           * has the right value when the tests are run with "make test". *)
          Unix.execv Sys.argv.(0) [||]
      | n ->
          Lwt_unix.close fd >>= fun () ->
          Lwt_unix.waitpid [] n >>= function
            | _, Unix.WEXITED 0 -> Lwt.return_true
            | _, (Unix.WEXITED _ | Unix.WSIGNALED _ | Unix.WSTOPPED _) ->
                Lwt.return_false

let openfile_tests = [
  test "openfile: O_CLOEXEC"
    (fun () -> test_cloexec assert_fd_closed [Unix.O_CLOEXEC]);

  test "openfile: O_CLOEXEC not given"
    (fun () -> test_cloexec assert_fd_open []);

#if OCAML_VERSION >= (4, 05, 0)
  test "openfile: O_KEEPEXEC"
    (fun () -> test_cloexec assert_fd_open [Unix.O_KEEPEXEC]);

  test "openfile: O_CLOEXEC, O_KEEPEXEC"
    (fun () -> test_cloexec assert_fd_closed [Unix.O_CLOEXEC; Unix.O_KEEPEXEC]);

  test "openfile: O_KEEPEXEC, O_CLOEXEC"
    (fun () -> test_cloexec assert_fd_closed [Unix.O_KEEPEXEC; Unix.O_CLOEXEC]);
#endif
]

let utimes_tests = [
  test "utimes: basic"<commit_msg>Properly skip cloexec tests on Windows<commit_after>(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)



open Test
open Lwt.Infix

let assert_fd_closed = "ASSERT_FD_CLOSED"
let assert_fd_open   = "ASSERT_FD_OPEN"

let test_cloexec assertion flags =
    Lwt_unix.openfile "/dev/zero" (Unix.O_RDONLY :: flags) 0o644 >>= fun fd ->
    let fd_ = Lwt_unix.unix_file_descr fd in
    match Lwt_unix.fork () with
      | 0 ->
          Unix.putenv assertion (string_of_int @@ Obj.magic fd_);
          (* There's no portable way to obtain the executable name (which
           * may even no longer exist at this point), but argv[0] fortunately
           * has the right value when the tests are run with "make test". *)
          Unix.execv Sys.argv.(0) [||]
      | n ->
          Lwt_unix.close fd >>= fun () ->
          Lwt_unix.waitpid [] n >>= function
            | _, Unix.WEXITED 0 -> Lwt.return_true
            | _, (Unix.WEXITED _ | Unix.WSIGNALED _ | Unix.WSTOPPED _) ->
                Lwt.return_false

let openfile_tests = [
  test "openfile: O_CLOEXEC" ~only_if:(fun () -> not Sys.win32)
    (fun () -> test_cloexec assert_fd_closed [Unix.O_CLOEXEC]);

  test "openfile: O_CLOEXEC not given" ~only_if:(fun () -> not Sys.win32)
    (fun () -> test_cloexec assert_fd_open []);

#if OCAML_VERSION >= (4, 05, 0)
  test "openfile: O_KEEPEXEC" ~only_if:(fun () -> not Sys.win32)
    (fun () -> test_cloexec assert_fd_open [Unix.O_KEEPEXEC]);

  test "openfile: O_CLOEXEC, O_KEEPEXEC" ~only_if:(fun () -> not Sys.win32)
    (fun () -> test_cloexec assert_fd_closed [Unix.O_CLOEXEC; Unix.O_KEEPEXEC]);

  test "openfile: O_KEEPEXEC, O_CLOEXEC" ~only_if:(fun () -> not Sys.win32)
    (fun () -> test_cloexec assert_fd_closed [Unix.O_KEEPEXEC; Unix.O_CLOEXEC]);
#endif
]

let utimes_tests = [
  test "utimes: basic"
{'fraction_non_alphanumeric': 0.12381468436738012, 'fraction_numerical': 0.009753454348415064, 'mean_word_length': 2.948663101604278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904930', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1358, 'n_words': 337}