Skip to content

Commit d327a64

Browse files
dkalinichenko-jsd-kalinichenkoncik-roberts
authored
Fix include in expect tests (#2558)
* Fix `include` in expect tests * Update ocaml/testsuite/tools/expect_test.ml Co-authored-by: Nick Roberts <[email protected]> * Update ocaml/testsuite/tools/expect_test.ml Co-authored-by: Nick Roberts <[email protected]> * Fixes * Sync files * Fix * Update ocaml/testsuite/tests/tool-expect-test/include.ml Co-authored-by: Nick Roberts <[email protected]> --------- Co-authored-by: Diana Kalinichenko <[email protected]> Co-authored-by: Nick Roberts <[email protected]>
1 parent ee71ef2 commit d327a64

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

ocaml/ocamltest/ocaml_actions.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,17 +794,22 @@ let cc =
794794

795795
let run_expect_once input_file principal log env =
796796
let expect_flags = Sys.safe_getenv "EXPECT_FLAGS" in
797-
let repo_root = "-repo-root " ^ Ocaml_directories.srcdir in
798797
let principal_flag = if principal then "-principal" else "" in
799798
let commandline =
800799
[
801800
Ocaml_commands.ocamlrun_expect_test;
802801
expect_flags;
802+
Ocaml_flags.toplevel_default_flags;
803+
Ocaml_flags.stdlib;
804+
directory_flags env;
805+
Ocaml_flags.include_toplevel_directory;
803806
flags env;
804-
repo_root;
805807
principal_flag;
808+
libraries Bytecode env;
809+
binary_modules Bytecode env;
806810
input_file
807-
] in
811+
]
812+
in
808813
let exit_status =
809814
Actions_helpers.run_cmd ~environment:default_ocaml_env log env commandline
810815
in
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(* TEST
2+
include unix;
3+
hasunix;
4+
libunix;
5+
expect;
6+
*)
7+
8+
(* Test that [include] works in expect tests: the Unix module is not
9+
available by default. *)
10+
Unix.sleepf 0.01;;
11+
[%%expect {|
12+
- : unit = ()
13+
|}]

ocaml/testsuite/tools/expect_test.ml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ let process_expect_file fname =
315315

316316
let repo_root = ref None
317317
let keep_original_error_size = ref false
318+
let preload_objects = ref []
319+
let main_file = ref None
320+
321+
let read_anonymous_arg fname =
322+
if Filename.check_suffix fname ".cmo"
323+
|| Filename.check_suffix fname ".cma"
324+
then preload_objects := fname :: !preload_objects
325+
else
326+
match !main_file with
327+
| None -> main_file := Some fname
328+
| Some _ ->
329+
Printf.eprintf "expect_test: multiple input source files\n";
330+
exit 2
318331

319332
let main fname =
320333
if not !keep_original_error_size then
@@ -337,6 +350,13 @@ let main fname =
337350
end;
338351
Compmisc.init_path ~auto_include:Load_path.no_auto_include ();
339352
Toploop.initialize_toplevel_env ();
353+
let objects = List.rev (!preload_objects) in
354+
List.iter objects ~f:(fun obj_fname ->
355+
match Topeval.load_file false Format.err_formatter obj_fname with
356+
| true -> ()
357+
| false ->
358+
Printf.eprintf "expect_test: failed to load object %s\n" obj_fname;
359+
exit 2);
340360
(* We are in interactive mode and should record directive error on stdout *)
341361
Sys.interactive := true;
342362
process_expect_file fname;
@@ -347,7 +367,7 @@ module Options = Main_args.Make_bytetop_options (struct
347367
let _stdin () = (* disabled *) ()
348368
let _args = Arg.read_arg
349369
let _args0 = Arg.read_arg0
350-
let anonymous s = main s
370+
let anonymous s = read_anonymous_arg s
351371
end);;
352372

353373
let args =
@@ -366,9 +386,12 @@ let usage = "Usage: expect_test <options> [script-file [arguments]]\n\
366386
let () =
367387
Clflags.color := Some Misc.Color.Never;
368388
try
369-
Arg.parse args main usage;
370-
Printf.eprintf "expect_test: no input file\n";
371-
exit 2
389+
Arg.parse args read_anonymous_arg usage;
390+
match !main_file with
391+
| Some fname -> main fname
392+
| None ->
393+
Printf.eprintf "expect_test: no input file\n";
394+
exit 2
372395
with exn ->
373396
Location.report_exception Format.err_formatter exn;
374397
exit 2

testsuite/tools/expect_test.ml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ let process_expect_file fname =
315315

316316
let repo_root = ref None
317317
let keep_original_error_size = ref false
318+
let preload_objects = ref []
319+
let main_file = ref None
320+
321+
let read_anonymous_arg fname =
322+
if Filename.check_suffix fname ".cmo"
323+
|| Filename.check_suffix fname ".cma"
324+
then preload_objects := fname :: !preload_objects
325+
else
326+
match !main_file with
327+
| None -> main_file := Some fname
328+
| Some _ ->
329+
Printf.eprintf "expect_test: multiple input source files\n";
330+
exit 2
318331

319332
let main fname =
320333
if not !keep_original_error_size then
@@ -335,8 +348,15 @@ let main fname =
335348
Clflags.no_std_include := true;
336349
Compenv.last_include_dirs := [Filename.concat dir "stdlib"]
337350
end;
338-
Compmisc.init_path ();
351+
Compmisc.init_path ~auto_include:Load_path.no_auto_include ();
339352
Toploop.initialize_toplevel_env ();
353+
let objects = List.rev (!preload_objects) in
354+
List.iter objects ~f:(fun obj_fname ->
355+
match Topeval.load_file false Format.err_formatter obj_fname with
356+
| true -> ()
357+
| false ->
358+
Printf.eprintf "expect_test: failed to load object %s\n" obj_fname;
359+
exit 2);
340360
(* We are in interactive mode and should record directive error on stdout *)
341361
Sys.interactive := true;
342362
process_expect_file fname;
@@ -347,7 +367,7 @@ module Options = Main_args.Make_bytetop_options (struct
347367
let _stdin () = (* disabled *) ()
348368
let _args = Arg.read_arg
349369
let _args0 = Arg.read_arg0
350-
let anonymous s = main s
370+
let anonymous s = read_anonymous_arg s
351371
end);;
352372

353373
let args =
@@ -366,9 +386,12 @@ let usage = "Usage: expect_test <options> [script-file [arguments]]\n\
366386
let () =
367387
Clflags.color := Some Misc.Color.Never;
368388
try
369-
Arg.parse args main usage;
370-
Printf.eprintf "expect_test: no input file\n";
371-
exit 2
389+
Arg.parse args read_anonymous_arg usage;
390+
match !main_file with
391+
| Some fname -> main fname
392+
| None ->
393+
Printf.eprintf "expect_test: no input file\n";
394+
exit 2
372395
with exn ->
373396
Location.report_exception Format.err_formatter exn;
374397
exit 2

0 commit comments

Comments
 (0)