Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate expect tests to ocamltest #1519

Merged
merged 13 commits into from Dec 12, 2017
9 changes: 9 additions & 0 deletions ocamltest/actions_helpers.ml
Expand Up @@ -33,6 +33,9 @@ let test_source_directory env =
let test_build_directory env =
Environments.safe_lookup Builtin_variables.test_build_directory env

let test_build_directory_prefix env =
Environments.safe_lookup Builtin_variables.test_build_directory_prefix env

let words_of_variable env variable =
String.words (Environments.safe_lookup variable env)

Expand Down Expand Up @@ -62,6 +65,12 @@ let setup_build_env add_testfile additional_files (_log : out_channel) env =
Sys.chdir build_dir;
Pass env

let setup_simple_build_env add_testfile additional_files log env =
let build_env = Environments.add
Builtin_variables.test_build_directory
(test_build_directory_prefix env) env in
setup_build_env add_testfile additional_files log build_env

let run_cmd
?(environment=[||])
?(stdin_variable=Builtin_variables.stdin)
Expand Down
2 changes: 2 additions & 0 deletions ocamltest/actions_helpers.mli
Expand Up @@ -31,6 +31,8 @@ val setup_symlinks : string -> string -> string list -> unit

val setup_build_env : bool -> string list -> Actions.code

val setup_simple_build_env : bool -> string list -> Actions.code

val run_cmd :
?environment : string array ->
?stdin_variable : Variables.t ->
Expand Down
4 changes: 4 additions & 0 deletions ocamltest/builtin_actions.ml
Expand Up @@ -63,6 +63,10 @@ let setup_build_env = make
"setup-build-env"
(Actions_helpers.setup_build_env true [])

let setup_simple_build_env = make
"setup-simple-build-env"
(Actions_helpers.setup_simple_build_env true [])

let run = make
"run"
Actions_helpers.run_program
Expand Down
4 changes: 4 additions & 0 deletions ocamltest/builtin_actions.mli
Expand Up @@ -24,6 +24,10 @@ val dumpenv : Actions.t
val unix : Actions.t
val windows : Actions.t

val setup_build_env : Actions.t

val setup_simple_build_env : Actions.t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is setup_simple_build_env doing compared to setup_build_env?


val run : Actions.t
val script : Actions.t

Expand Down
57 changes: 53 additions & 4 deletions ocamltest/ocaml_actions.ml
Expand Up @@ -79,6 +79,15 @@ let ocaml_dot_opt ocamlsrcdir =
let cmpbyt ocamlsrcdir =
Filename.make_path [ocamlsrcdir; "tools"; "cmpbyt"]

let expect_program ocamlsrcdir =
Filename.make_path
[ocamlsrcdir; "testsuite"; "tools"; Filename.mkexe "expect_test"]

let expect_command ocamlsrcdir =
let ocamlrun = ocamlrun ocamlsrcdir in
let expect_test = expect_program ocamlsrcdir in
ocamlrun ^ " " ^ expect_test

let stdlib ocamlsrcdir =
Filename.make_path [ocamlsrcdir; "stdlib"]

Expand Down Expand Up @@ -450,11 +459,51 @@ let ocamlopt_opt = Actions.make
"ocamlopt.opt"
(compile_test_program Builtin_variables.program2 ocamlopt_opt_compiler)

let run_expect_once ocamlsrcdir input_file principal log env =
let expect_flags = try Sys.getenv "EXPECT_FLAGS" with Not_found -> "" in
let repo_root = "-repo-root " ^ ocamlsrcdir in
let principal_flag = if principal then "-principal" else "" in
let commandline =
[
expect_command ocamlsrcdir;
expect_flags;
flags env;
repo_root;
principal_flag;
input_file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original invocation command

TERM=dumb $(EXPECT_TEST) $(EXPECT_FLAGS) -repo-root $(OTOPDIR) $$file

also starts with a TERM=dumb setting to control the behavior of toplevel location printing (I suppose). Is it reproduced here, for example by being globally set by ocamltest, or by Actions_helpers.run_cmd?

] in
let exit_status = Actions_helpers.run_cmd log env commandline in
if exit_status=0 then Pass env
else Fail (Actions_helpers.mkreason
"expect" (String.concat " " commandline) exit_status)

let run_expect_twice ocamlsrcdir input_file log env =
let corrected filename = Filename.make_filename filename "corrected" in
let first_run = run_expect_once ocamlsrcdir input_file false log env in
match first_run with
| Pass env1 ->
let intermediate_file = corrected input_file in
let second_run =
run_expect_once ocamlsrcdir intermediate_file true log env1 in
(match second_run with
| Pass env2 ->
let output_file = corrected intermediate_file in
let output_env = Environments.add_bindings
[
Builtin_variables.reference, input_file;
Builtin_variables.output, output_file
] env2 in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An intermediary file input_file ^ ".corrected" is generated by running the command twice. Is this intermediary file simply ignored, or will it get automatically cleaned by ocamltest?

Pass output_env
| Skip _ | Fail _ -> second_run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is minor, but I find the code more readable if the short cases are first (Skip _ | Fail _ before Pass env). Otherwise when I read the short cases, I don't remember well what was being matched.

)
| Skip _ | Fail _ -> first_run

let run_expect log env =
let newenv = Environments.apply_modifiers env Ocaml_modifiers.expect in
Actions_helpers.run_script log newenv
let ocamlsrcdir = ocamlsrcdir () in
let input_file = Actions_helpers.testfile env in
run_expect_twice ocamlsrcdir input_file log env
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When are the result and reference file actually compared? I don't see a call to Actions_helpers.check_output, which I thought was used for this.


let expect = Actions.make "expect" run_expect
let run_expect = Actions.make "run-expect" run_expect

let make_check_compiler_output name compiler = Actions.make
name
Expand Down Expand Up @@ -640,7 +689,7 @@ let _ =
setup_ocamlopt_opt_build_env;
ocamlopt_opt;
check_ocamlopt_opt_output;
expect;
run_expect;
compare_bytecode_programs;
compare_native_programs;
setup_ocaml_build_env;
Expand Down
2 changes: 1 addition & 1 deletion ocamltest/ocaml_actions.mli
Expand Up @@ -27,7 +27,7 @@ val check_ocamlopt_byte_output : Actions.t
val setup_ocamlopt_opt_build_env : Actions.t
val ocamlopt_opt : Actions.t
val check_ocamlopt_opt_output : Actions.t
val expect : Actions.t
val run_expect : Actions.t
val compare_bytecode_programs : Actions.t
val compare_native_programs : Actions.t
val setup_ocaml_build_env : Actions.t
Expand Down
25 changes: 10 additions & 15 deletions ocamltest/ocaml_modifiers.ml
Expand Up @@ -18,12 +18,6 @@
open Ocamltest_stdlib
open Environments

let expect =
[
Add (Builtin_variables.script,
"bash ${OCAMLSRCDIR}/testsuite/tools/expect");
]

let principal =
[
Append (Ocaml_variables.flags, " -principal ");
Expand Down Expand Up @@ -57,18 +51,19 @@ let bigarray =
let str = make_library_modifier
"str" (compiler_subdir ["otherlibs"; "str"])

let compilerlibs_subdirs =
[
"utils"; "parsing"; "typing"; "bytecomp"; "compilerlibs";
]

let add_compiler_subdir subdir =
Append (Ocaml_variables.directories, (wrap (compiler_subdir [subdir])))

let ocamlcommon =
[
Append (Ocaml_variables.directories, wrap (compiler_subdir ["utils"]));
Append (Ocaml_variables.directories, wrap (compiler_subdir ["parsing"]));
Append (Ocaml_variables.directories, wrap (compiler_subdir ["typing"]));
Append (Ocaml_variables.directories, wrap (compiler_subdir ["bytecomp"]));
Append (Ocaml_variables.directories, wrap (compiler_subdir ["compilerlibs"]));
Append (Ocaml_variables.libraries, wrap "ocamlcommon");
]
(Append (Ocaml_variables.libraries, wrap "ocamlcommon")) ::
(List.map add_compiler_subdir compilerlibs_subdirs)

let _ =
register_modifiers "expect" expect;
register_modifiers "principal" principal;
register_modifiers "testing" testing;
register_modifiers "unix" unix;
Expand Down
2 changes: 0 additions & 2 deletions ocamltest/ocaml_modifiers.mli
Expand Up @@ -15,8 +15,6 @@

(* Definition of a few OCaml-specific environment modifiers *)

val expect : Environments.modifiers

val principal : Environments.modifiers

val testing : Environments.modifiers
Expand Down
13 changes: 13 additions & 0 deletions ocamltest/ocaml_tests.ml
Expand Up @@ -73,10 +73,23 @@ let toplevel = {
]
}

let expect =
{
test_name = "expect";
test_run_by_default = false;
test_actions =
[
setup_simple_build_env;
run_expect;
check_program_output
]
}

let _ =
List.iter register
[
bytecode;
toplevel;
expect;
];
if (Ocamltest_config.arch <> "none") then register native
2 changes: 2 additions & 0 deletions ocamltest/ocaml_tests.mli
Expand Up @@ -20,3 +20,5 @@ val bytecode : Tests.t
val native : Tests.t

val toplevel : Tests.t

val expect : Tests.t