Skip to content

Commit

Permalink
add: skip_build in test stanza
Browse files Browse the repository at this point in the history
Fixes ocaml#6938

The semantics of `(enabled_if)` in `(test)` can be confusing:
`(test)` can be seen as the combination of `(executable)` and a `(rule
(alias runtest))`; but `(enabled_if)` actually only controls the
"running" part, not the "building" one.

This adds a new `(skip_build)` field in `(test)`. When present, the
semantics of `(enabled_if)` extend to building the test executable.

In other words, if the `(enabled_if)` field of such a stanza evaluates
to `false`, it is as if the stanza was not present.

Signed-off-by: Etienne Millon <me@emillon.org>
  • Loading branch information
emillon committed Jun 7, 2023
1 parent 4fb0fef commit e89d29e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Unreleased
- Switch back to threaded console for all systems; fix unresponsive console on
Windows (#7906, @nojb)

- Add `(skip_build)` to the `(test)` stanza. When set, `(enabled_if)` also
controls whether the executable is built. (#...., fixes #6938, @emillon)

3.8.1 (2023-06-05)
------------------

Expand Down
4 changes: 4 additions & 0 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,7 @@ module Tests = struct
; package : Package.t option
; deps : Dep_conf.t Bindings.t
; enabled_if : Blang.t
; skip_build : bool
; action : Dune_lang.Action.t option
}

Expand Down Expand Up @@ -1936,6 +1937,8 @@ module Tests = struct
(Dune_lang.Syntax.since Stanza.syntax (2, 0)
>>> repeat (located Lib_name.decode))
~default:[]
and+ skip_build =
field_b "skip_build" ~check:(Syntax.since Stanza.syntax (3, 9))
in
{ exes =
{ Executables.link_flags
Expand All @@ -1957,6 +1960,7 @@ module Tests = struct
; package
; deps
; enabled_if
; skip_build
; action
}))

Expand Down
1 change: 1 addition & 0 deletions src/dune_rules/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ module Tests : sig
; package : Package.t option
; deps : Dep_conf.t Bindings.t
; enabled_if : Blang.t
; skip_build : bool
; action : Dune_lang.Action.t option
}
end
Expand Down
20 changes: 13 additions & 7 deletions src/dune_rules/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,20 @@ end = struct
let+ () = Simple_rules.alias sctx alias ~dir ~expander in
empty_none
| Tests tests ->
let+ cctx, merlin =
Test_rules.rules tests ~sctx ~dir ~scope ~expander ~dir_contents
let* enabled =
if tests.skip_build then Expander.eval_blang expander tests.enabled_if
else Memo.return true
in
{ merlin = Some merlin
; cctx = Some (tests.exes.buildable.loc, cctx)
; js = None
; source_dirs = None
}
if enabled then
let+ cctx, merlin =
Test_rules.rules tests ~sctx ~dir ~scope ~expander ~dir_contents
in
{ merlin = Some merlin
; cctx = Some (tests.exes.buildable.loc, cctx)
; js = None
; source_dirs = None
}
else Memo.return empty_none
| Copy_files { files = glob; _ } ->
let* source_dirs =
let loc = String_with_vars.loc glob in
Expand Down
40 changes: 40 additions & 0 deletions test/blackbox-tests/test-cases/test-skip-build.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
$ cat > dune-project << EOF
> (lang dune 3.8)
> EOF

The enabled_if field evaluates to true then BUILD=true is passed.

$ cat > dune << EOF
> (test
> (name t)
> (enabled_if %{env:BUILD=false})
> (skip_build))
> EOF

$ cat > t.ml << EOF
> broken
> EOF

Version check: this only works on 3.9+.

$ dune build
File "dune", line 4, characters 1-13:
4 | (skip_build))
^^^^^^^^^^^^
Error: 'skip_build' is only available since version 3.9 of the dune language.
Please update your dune-project file to have (lang dune 3.9).
[1]

$ cat > dune-project << EOF
> (lang dune 3.9)
> EOF

The build is attempted only when the enabled_if clause evaluates to true.

$ dune build
$ BUILD=true dune build
File "t.ml", line 1, characters 0-6:
1 | broken
^^^^^^
Error: Unbound value broken
[1]

0 comments on commit e89d29e

Please sign in to comment.