Skip to content

Commit fd05a75

Browse files
committed
Migrate the lib-unix/common tests to ocamltest
1 parent 02326ad commit fd05a75

19 files changed

Lines changed: 241 additions & 140 deletions

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ _ocamltest
277277

278278
/testsuite/tests/lib-threads/*.byt
279279

280-
/testsuite/tests/lib-unix/common/utimes.txt
281-
282280
/testsuite/tests/lib-unix/win-stat/*-file
283281
/testsuite/tests/lib-unix/win-symlink/link*
284282
/testsuite/tests/lib-unix/win-symlink/test.txt

testsuite/tests/lib-unix/common/Makefile

Lines changed: 0 additions & 37 deletions
This file was deleted.

testsuite/tests/lib-unix/common/cloexec.ml

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
1+
(* TEST
2+
3+
(*
4+
This test is temporarily disabled on the MinGW and MSVC ports,
5+
because since fdstatus has been wrapped in an OCaml program,
6+
it does not work as well as before.
7+
Presumably this is because the OCaml runtime opens files, so that handles
8+
that have actually been closed at execution look open and make the
9+
test fail.
10+
11+
One possible fix for this would be to make it possible for ocamltest to
12+
compile C-only programs, which will be a bit of work to handle the
13+
output of msvc and will also duplicate what the ocaml compiler itslef
14+
already does.
15+
*)
16+
17+
include unix
18+
files = "fdstatus_aux.c fdstatus_main.ml"
19+
20+
*libunix
21+
** setup-ocamlc.byte-build-env
22+
program = "${test_build_directory}/cloexec.byte"
23+
*** ocamlc.byte
24+
program = "${test_build_directory}/fdstatus.exe"
25+
all_modules = "fdstatus_aux.c fdstatus_main.ml"
26+
**** ocamlc.byte
27+
program = "${test_build_directory}/cloexec.byte"
28+
all_modules= "cloexec.ml"
29+
***** check-ocamlc.byte-output
30+
****** run
31+
******* check-program-output
32+
33+
** setup-ocamlopt.byte-build-env
34+
program = "${test_build_directory}/cloexec.opt"
35+
*** ocamlopt.byte
36+
program = "${test_build_directory}/fdstatus.exe"
37+
all_modules = "fdstatus_aux.c fdstatus_main.ml"
38+
**** ocamlopt.byte
39+
program = "${test_build_directory}/cloexec.opt"
40+
all_modules= "cloexec.ml"
41+
***** check-ocamlopt.byte-output
42+
****** run
43+
******* check-program-output
44+
45+
*)
46+
147
(* This is a terrible hack that plays on the internal representation
248
of file descriptors. The result is a number (as a string)
349
that the fdstatus.exe auxiliary program can use to check whether
4-
the fd is open. *)
50+
the fd is open. Moreover, since fdstatus.exe is an OCaml program,
51+
we must take into account that the Windows OCaml runtime opens a few handles
52+
for its own use, hence we do likewise to try to get handle numbers
53+
Windows will not allocate to the OCaml runtime of fdstatus.exe *)
554

655
let string_of_fd (fd: Unix.file_descr) : string =
756
match Sys.os_type with
@@ -13,8 +62,15 @@ let string_of_fd (fd: Unix.file_descr) : string =
1362
Int64.to_string (Obj.magic fd : int64)
1463
| _ -> assert false
1564

65+
let status_checker = "fdstatus.exe"
66+
1667
let _ =
1768
let f0 = Unix.(openfile "tmp.txt" [O_WRONLY; O_CREAT; O_TRUNC] 0o600) in
69+
let untested1 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
70+
let untested2 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
71+
let untested3 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
72+
let untested4 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
73+
let untested5 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
1874
let f1 = Unix.(openfile "tmp.txt" [O_RDONLY; O_KEEPEXEC] 0) in
1975
let f2 = Unix.(openfile "tmp.txt" [O_RDONLY; O_CLOEXEC] 0) in
2076
let d0 = Unix.dup f0 in
@@ -41,11 +97,16 @@ let _ =
4197
p0;p0';p1;p1';p2;p2';
4298
s0;s1;s2;
4399
x0;x0';x1;x1';x2;x2' |] in
100+
let untested =
101+
[untested1; untested2; untested3; untested4; untested5]
102+
in
44103
let pid =
45104
Unix.create_process
46-
(Filename.concat Filename.current_dir_name "fdstatus.exe")
47-
(Array.append [| "fdstatus" |] (Array.map string_of_fd fds))
105+
(Filename.concat Filename.current_dir_name status_checker)
106+
(Array.append [| status_checker |] (Array.map string_of_fd fds))
48107
Unix.stdin Unix.stdout Unix.stderr in
49108
ignore (Unix.waitpid [] pid);
50-
Array.iter (fun fd -> try Unix.close fd with Unix.Unix_error _ -> ()) fds;
109+
let close fd = try Unix.close fd with Unix.Unix_error _ -> () in
110+
Array.iter close fds;
111+
List.iter close untested;
51112
Sys.remove "tmp.txt"

testsuite/tests/lib-unix/common/cmdline_prog.c

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let () =
2+
for i = 1 to (Array.length Sys.argv) - 1 do
3+
Printf.printf "%s\n" Sys.argv.(i)
4+
done

testsuite/tests/lib-unix/common/dup.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
(* TEST
2+
include unix
3+
*)
4+
15
let _ =
26
let f = Unix.dup ~cloexec:true Unix.stdout in
37
let txt = "Some output\n" in

testsuite/tests/lib-unix/common/dup2.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
(* TEST
2+
include unix
3+
stderr = "/dev/null"
4+
*)
5+
16
let cat file =
27
let fd = Unix.openfile file [Unix.O_RDONLY] 0 in
38
let buf = Bytes.create 1024 in

testsuite/tests/lib-unix/common/fdstatus.c renamed to testsuite/tests/lib-unix/common/fdstatus_aux.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ void process_fd(char * s)
6262

6363
#endif
6464

65-
int main(int argc, char ** argv)
65+
#include "caml/mlvalues.h"
66+
#include "caml/memory.h"
67+
68+
CAMLprim value caml_process_fd(value CAMLnum, value CAMLfd)
6669
{
67-
int i;
68-
for (i = 1; i < argc; i++) {
69-
printf("#%d: ", i);
70-
process_fd(argv[i]);
71-
}
72-
return 0;
70+
CAMLparam2(CAMLnum, CAMLfd);
71+
printf("#%d: ", Int_val(CAMLnum));
72+
process_fd(String_val(CAMLfd));
73+
CAMLreturn(Val_unit);
7374
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
external process_fd : int -> string -> unit = "caml_process_fd"
2+
3+
let () =
4+
for i = 1 to (Array.length Sys.argv) -1
5+
do
6+
process_fd i Sys.argv.(i);
7+
done
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cloexec.ml
2+
dup2.ml
3+
dup.ml
4+
pipe_eof.ml
5+
redirections.ml
6+
rename.ml
7+
test_unix_cmdline.ml
8+
utimes.ml
9+
wait_nohang.ml

0 commit comments

Comments
 (0)