From 6b731d1ccd75243a31d866ca5188213a5b467cc7 Mon Sep 17 00:00:00 2001 From: Marc Lasson Date: Tue, 25 Jun 2019 11:22:12 +0200 Subject: [PATCH] Adds a new 'instrumentation' setting in env stanza Signed-off-by: Marc Lasson --- CHANGES.md | 4 +++ doc/dune-files.rst | 5 ++++ src/dune_env.ml | 28 +++++++++++++++++++ src/dune_env.mli | 11 ++++++++ src/env_node.ml | 20 ++++++++++++- src/env_node.mli | 2 ++ src/super_context.ml | 15 +++++++++- .../test-cases/inline_tests/run.t | 4 ++- .../test-cases/inline_tests/simple/dune | 2 +- 9 files changed, 87 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 61072bc9358d..9b6abca244cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,9 +8,13 @@ - Do not put the `.install` files in the source tree unless `-p` or `--promote-install-files` is passed on the command line (#..., @diml) +- Add a new `inline-tests` field in the env stanza to control inline-tests + framework with a variable (#2313, @mlasson review and original idea by @diml) + - Change `implicit_transive_deps` to be false. Implicit transitive deps now must be manually enabled (#2306, @rgrinberg) + 1.11.0 (unreleased) ------------------- diff --git a/doc/dune-files.rst b/doc/dune-files.rst index 101f4c39e4f9..412328b947b1 100644 --- a/doc/dune-files.rst +++ b/doc/dune-files.rst @@ -804,6 +804,11 @@ Fields supported in ```` are: be inferred from the basename of ```` by dropping the ``.exe`` suffix if it exists. +- ``(inline-tests )`` where state is either ``enabled``, ``disabled`` or + ``ignored``. This field controls the value of the variable ``%{inline-tests}`` + that is read by the inline test framework. The default value is ``disabled`` + for the ``release`` profile and ``enabled`` otherwise. + .. _dune-subdirs: dirs (since 1.6) diff --git a/src/dune_env.ml b/src/dune_env.ml index 18f00820b09d..5e9e942996f9 100644 --- a/src/dune_env.ml +++ b/src/dune_env.ml @@ -14,11 +14,31 @@ module Stanza = struct in C.Kind.Dict.make ~c ~cxx + module Inline_tests = struct + type t = + | Enabled + | Disabled + | Ignored + + let decode = + enum + [ "enabled", Enabled + ; "disabled", Disabled + ; "ignored", Ignored ] + + let to_string = function + | Enabled -> "enabled" + | Disabled -> "disabled" + | Ignored -> "ignored" + + end + type config = { flags : Ocaml_flags.Spec.t ; c_flags : Ordered_set_lang.Unexpanded.t C.Kind.Dict.t ; env_vars : Env.t ; binaries : File_binding.Unexpanded.t list + ; inline_tests : Inline_tests.t option } type pattern = @@ -30,6 +50,12 @@ module Stanza = struct ; rules : (pattern * config) list } + let inline_tests_field = + field_o + "inline-tests" + (Syntax.since Stanza.syntax (1, 11) >>> + Inline_tests.decode) + let env_vars_field = field "env-vars" @@ -48,11 +74,13 @@ module Stanza = struct and+ binaries = field ~default:[] "binaries" (Syntax.since Stanza.syntax (1, 6) >>> File_binding.Unexpanded.L.decode) + and+ inline_tests = inline_tests_field in { flags ; c_flags ; env_vars ; binaries + ; inline_tests } let rule = diff --git a/src/dune_env.mli b/src/dune_env.mli index 558d7ff3e6d9..5a33aeef1735 100644 --- a/src/dune_env.mli +++ b/src/dune_env.mli @@ -3,11 +3,22 @@ open! Stdune type stanza = Stanza.t = .. module Stanza : sig + + module Inline_tests: sig + type t = + | Enabled + | Disabled + | Ignored + val decode: t Dune_lang.Decoder.t + val to_string: t -> string + end + type config = { flags : Ocaml_flags.Spec.t ; c_flags : Ordered_set_lang.Unexpanded.t C.Kind.Dict.t ; env_vars : Env.t ; binaries : File_binding.Unexpanded.t list + ; inline_tests : Inline_tests.t option } type pattern = diff --git a/src/env_node.ml b/src/env_node.ml index e83bf5c85f21..094070d418ab 100644 --- a/src/env_node.ml +++ b/src/env_node.ml @@ -9,7 +9,8 @@ type t = ; mutable ocaml_flags : Ocaml_flags.t option ; mutable c_flags : (unit, string list) Build.t C.Kind.Dict.t option ; mutable external_ : Env.t option - ; mutable bin_artifacts : Artifacts.Bin.t option + ; mutable bin_artifacts : Artifacts.Bin.t option + ; mutable inline_tests : Dune_env.Stanza.Inline_tests.t option; } let scope t = t.scope @@ -24,6 +25,7 @@ let make ~dir ~inherit_from ~scope ~config = ; external_ = None ; bin_artifacts = None ; local_binaries = None + ; inline_tests = None } let find_config t ~profile = @@ -121,6 +123,22 @@ let rec ocaml_flags t ~profile ~expander = t.ocaml_flags <- Some flags; flags +let inline_tests t ~profile = + match t.inline_tests with + | Some x -> x + | None -> + let state : Dune_env.Stanza.Inline_tests.t = + match find_config t ~profile with + | None | Some {inline_tests = None; _} -> + if profile = "release" then + Disabled + else + Enabled + | Some {inline_tests = Some s; _} -> s + in + t.inline_tests <- Some state; + state + let rec c_flags t ~profile ~expander ~default_context_flags = match t.c_flags with | Some x -> x diff --git a/src/env_node.mli b/src/env_node.mli index 9da9d480f2d1..e8c1eb98afdc 100644 --- a/src/env_node.mli +++ b/src/env_node.mli @@ -18,6 +18,8 @@ val external_ : t -> profile:string -> default:Env.t -> Env.t val ocaml_flags : t -> profile:string -> expander:Expander.t -> Ocaml_flags.t +val inline_tests : t -> profile:string -> Dune_env.Stanza.Inline_tests.t + val c_flags : t -> profile:string diff --git a/src/super_context.ml b/src/super_context.ml index ab57831c7d26..83334df81e6d 100644 --- a/src/super_context.ml +++ b/src/super_context.ml @@ -150,6 +150,10 @@ end = struct in Env_node.local_binaries node ~profile:t.profile ~expander + let inline_tests ({profile; _} as t) ~dir = + let node = get t ~dir in + Env_node.inline_tests node ~profile + let bin_artifacts t ~dir = let expander = expander_for_artifacts t ~context_expander:t.expander ~dir @@ -174,7 +178,16 @@ end = struct expander_for_artifacts t ~context_expander:t.expander ~dir in let bin_artifacts_host = bin_artifacts_host t ~dir in - Expander.set_bin_artifacts expander ~bin_artifacts_host + let bindings = + let str = + inline_tests t ~dir + |> Dune_env.Stanza.Inline_tests.to_string + in + Pform.Map.singleton "inline-tests" (Values [String str]) + in + expander + |> Expander.add_bindings ~bindings + |> Expander.set_bin_artifacts ~bin_artifacts_host let ocaml_flags t ~dir = Env_node.ocaml_flags (get t ~dir) diff --git a/test/blackbox-tests/test-cases/inline_tests/run.t b/test/blackbox-tests/test-cases/inline_tests/run.t index 651f410960c3..a1d490d369b9 100644 --- a/test/blackbox-tests/test-cases/inline_tests/run.t +++ b/test/blackbox-tests/test-cases/inline_tests/run.t @@ -1,9 +1,11 @@ $ env -u OCAMLRUNPARAM dune runtest simple run alias simple/runtest (exit 2) (cd _build/default/simple && .foo_simple.inline-tests/run.exe) - Fatal error: exception File "simple/.foo_simple.inline-tests/run.ml-gen", line 1, characters 10-16: Assertion failed + Fatal error: exception File "simple/.foo_simple.inline-tests/run.ml-gen", line 1, characters 40-46: Assertion failed [1] + $ env -u OCAMLRUNPARAM dune runtest simple --profile release + $ dune runtest missing-backend File "missing-backend/dune", line 3, characters 1-15: 3 | (inline_tests)) diff --git a/test/blackbox-tests/test-cases/inline_tests/simple/dune b/test/blackbox-tests/test-cases/inline_tests/simple/dune index 8e2f4f992366..0a1b293f529c 100644 --- a/test/blackbox-tests/test-cases/inline_tests/simple/dune +++ b/test/blackbox-tests/test-cases/inline_tests/simple/dune @@ -2,7 +2,7 @@ (name backend_simple) (modules ()) (inline_tests.backend - (generate_runner (run sed "s/(\\*TEST:\\(.*\\)\\*)/let () = \\1;;/" %{impl-files}) + (generate_runner (run sed "s/(\\*TEST:\\(.*\\)\\*)/let () = if \"%{inline-tests}\" = \"enabled\" then \\1;;/" %{impl-files}) ))) (library