Skip to content

Commit

Permalink
Add enabled_if to running inline_tests
Browse files Browse the repository at this point in the history
This is an improvement over enabled_if on the library itself as it still
allows to compile the library.

Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Sep 27, 2021
1 parent 050bde3 commit 83acf23
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased
----------

- Add the `enabled_if` field to `inline_tests` within the `library` stanza.
This allows us to disable executing the inline tests while still allowing for
compilation (#4939, @rgrinberg)

- Generate a `dune-project` when initializing projects with `dune init proj ...`
(#4881, closes #4367, @shonfeder)

Expand Down
19 changes: 16 additions & 3 deletions src/dune_rules/inline_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ include Sub_system.Register_end_point (struct
module Mode_conf = Inline_tests_info.Mode_conf
module Info = Inline_tests_info.Tests

let gen_rules c ~(info : Info.t) ~backends =
let open Memo.Build.O in
let gen_rules c ~expander ~(info : Info.t) ~backends =
let { Sub_system.Library_compilation_context.super_context = sctx
; dir
; stanza = lib
Expand All @@ -104,8 +103,8 @@ include Sub_system.Register_end_point (struct
let src_dir = Path.build inline_test_dir in
Module.generated ~src_dir name
in
let open Memo.Build.O in
let modules = Modules.singleton_exe main_module in
let* expander = Super_context.expander sctx ~dir in
let runner_libs =
let open Resolve.Build.O in
let* libs =
Expand Down Expand Up @@ -258,6 +257,20 @@ include Sub_system.Register_end_point (struct
(Path.Build.extend_basename
(Path.as_in_build_dir_exn fn)
~suffix:".corrected")))))

let gen_rules c ~(info : Info.t) ~backends =
let open Memo.Build.O in
let { dir; Sub_system.Library_compilation_context.super_context = sctx; _ }
=
c
in
let* expander = Super_context.expander sctx ~dir in
let* enabled_if = Expander.eval_blang expander info.enabled_if in
if enabled_if then
gen_rules c ~expander ~info ~backends
else
let alias = Alias.runtest ~dir in
Simple_rules.Alias_rules.add_empty sctx ~alias ~loc:(Some info.loc)
end)

let linkme = ()
7 changes: 6 additions & 1 deletion src/dune_rules/inline_tests_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module Tests = struct
; executable : Ocaml_flags.Spec.t
; backend : (Loc.t * Lib_name.t) option
; libraries : (Loc.t * Lib_name.t) list
; enabled_if : Blang.t
}

type Sub_system_info.t += T of t
Expand Down Expand Up @@ -122,8 +123,12 @@ module Tests = struct
field "modes"
(Dune_lang.Syntax.since syntax (1, 11) >>> Mode_conf.Set.decode)
~default:Mode_conf.Set.default
and+ enabled_if =
Enabled_if.decode ~allowed_vars:Any ~is_error:true
~since:(Some (3, 0))
()
in
{ loc; deps; flags; executable; backend; libraries; modes })
{ loc; deps; flags; executable; backend; libraries; modes; enabled_if })

(* We don't use this at the moment, but we could implement it for debugging
purposes *)
Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/inline_tests_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module Tests : sig
; executable : Ocaml_flags.Spec.t
; backend : (Loc.t * Lib_name.t) option
; libraries : (Loc.t * Lib_name.t) list
; enabled_if : Blang.t
}

val backends : t -> (Loc.t * Lib_name.t) list option
Expand Down
22 changes: 22 additions & 0 deletions test/blackbox-tests/test-cases/inline_tests/enabled-if.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
enabled_if inside the inline_tests field in the library stanza

$ mkdir tmp && cd tmp
$ cat >dune-project <<EOF
> (lang dune 3.0)
> EOF
$ cat >dune <<EOF
> (library
> (name backend_mbc)
> (modules ())
> (inline_tests.backend
> (generate_runner (echo "print_endline \"backend_mbc\""))))
>
> (library
> (name foo_mbc)
> (inline_tests
> (enabled_if false)
> (backend backend_mbc))
> (libraries backend_mbc))
> EOF

$ dune runtest

0 comments on commit 83acf23

Please sign in to comment.