Skip to content

Commit b5a8395

Browse files
committed
feature(cram): allow controlling default alias for cram
Introduce a [(runtest_alias ..)] that allows to enable/disable the runtest alias for cram tests. Unlike, the existing [alias] field, we're only allowed to set the this value once. Signed-off-by: Rudi Grinberg <[email protected]> <!-- ps-id: 23568b34-517f-464d-93e2-ac2c0f05f885 -->
1 parent 7f8ee45 commit b5a8395

File tree

6 files changed

+146
-46
lines changed

6 files changed

+146
-46
lines changed

doc/changes/8887.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Introduce the `runtest_alias` field to the `cram` stanza. This allows
2+
removing default `runtest` alias from tests. (@rgrinberg, #8887)

doc/tests.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ The ``cram`` stanza accepts the following fields:
674674
- ``deps`` - dependencies of the test
675675
- ``(package <package-name>)`` - attach the tests selected by this stanza to the
676676
specified package
677+
- ``(runtest_alias <true|false>)`` - when set to ``false``, do not add the
678+
tests to the ``runtest`` alias by default.
677679
678680
A single test may be configured by more than one ``cram`` stanza. In such cases,
679681
the values from all applicable ``cram`` stanzas are merged together to get the

src/dune_rules/cram/cram_rules.ml

Lines changed: 91 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Spec = struct
1414

1515
let empty =
1616
{ loc = Loc.none
17-
; alias = Alias.Name.Set.singleton Alias0.runtest
17+
; alias = Alias.Name.Set.empty
1818
; enabled_if = [ Blang.true_ ]
1919
; locks = Path.Set.empty
2020
; deps = []
@@ -149,51 +149,97 @@ let rules ~sctx ~expander ~dir tests =
149149
| Error (Source_tree.Dir.Missing_run_t test) -> Cram_test.name test
150150
in
151151
let init =
152-
let alias = Alias.Name.of_string name |> Alias.Name.Set.add Spec.empty.alias in
153-
{ Spec.empty with alias }
152+
( None
153+
, let alias = Alias.Name.of_string name |> Alias.Name.Set.add Spec.empty.alias in
154+
{ Spec.empty with alias } )
154155
in
155-
Memo.List.fold_left
156-
stanzas
157-
~init
158-
~f:(fun (acc : Spec.t) (dir, (stanza : Cram_stanza.t)) ->
159-
match
160-
match stanza.applies_to with
161-
| Whole_subtree -> true
162-
| Files_matching_in_this_dir pred ->
163-
Predicate_lang.Glob.test pred ~standard:Predicate_lang.true_ name
164-
with
165-
| false -> Memo.return acc
166-
| true ->
167-
let+ deps, sandbox =
168-
match stanza.deps with
169-
| None -> Memo.return (acc.deps, acc.sandbox)
170-
| Some deps ->
171-
let+ (deps : unit Action_builder.t), _, sandbox =
172-
let+ expander = Super_context.expander sctx ~dir in
173-
Dep_conf_eval.named ~expander deps
174-
in
175-
deps :: acc.deps, Sandbox_config.inter acc.sandbox sandbox
176-
and+ locks =
177-
(* Locks must be relative to the cram stanza directory and not
178-
the individual tests directories *)
179-
let base = `This (Path.build dir) in
180-
Expander.expand_locks ~base expander stanza.locks
181-
>>| Path.Set.of_list
182-
>>| Path.Set.union acc.locks
183-
in
184-
let enabled_if = stanza.enabled_if :: acc.enabled_if in
185-
let alias =
186-
match stanza.alias with
187-
| None -> acc.alias
188-
| Some a -> Alias.Name.Set.add acc.alias a
189-
in
190-
let packages =
191-
match stanza.package with
192-
| None -> acc.packages
193-
| Some (p : Package.t) ->
194-
Package.Name.Set.add acc.packages (Package.Id.name p.id)
195-
in
196-
{ acc with enabled_if; locks; deps; alias; packages; sandbox })
156+
let+ runtest_alias, acc =
157+
Memo.List.fold_left
158+
stanzas
159+
~init
160+
~f:(fun (runtest_alias, (acc : Spec.t)) (dir, (stanza : Cram_stanza.t)) ->
161+
match
162+
match stanza.applies_to with
163+
| Whole_subtree -> true
164+
| Files_matching_in_this_dir pred ->
165+
Predicate_lang.Glob.test pred ~standard:Predicate_lang.true_ name
166+
with
167+
| false -> Memo.return (runtest_alias, acc)
168+
| true ->
169+
let+ deps, sandbox =
170+
match stanza.deps with
171+
| None -> Memo.return (acc.deps, acc.sandbox)
172+
| Some deps ->
173+
let+ (deps : unit Action_builder.t), _, sandbox =
174+
let+ expander = Super_context.expander sctx ~dir in
175+
Dep_conf_eval.named ~expander deps
176+
in
177+
deps :: acc.deps, Sandbox_config.inter acc.sandbox sandbox
178+
and+ locks =
179+
(* Locks must be relative to the cram stanza directory and not
180+
the individual tests directories *)
181+
let base = `This (Path.build dir) in
182+
Expander.expand_locks ~base expander stanza.locks
183+
>>| Path.Set.of_list
184+
>>| Path.Set.union acc.locks
185+
in
186+
let runtest_alias =
187+
match stanza.runtest_alias with
188+
| None -> None
189+
| Some (loc, set) ->
190+
(match runtest_alias with
191+
| None -> Some (loc, set)
192+
| Some (loc', _) ->
193+
let main_message =
194+
Pp.concat
195+
~sep:Pp.newline
196+
[ Pp.text
197+
"enabling or disabling the runtest alias for a cram test \
198+
may only be set once."
199+
; Pp.textf "It's already set for the test %S" name
200+
]
201+
in
202+
let annots =
203+
let main = User_message.make ~loc:loc' [ main_message ] in
204+
let related =
205+
[ User_message.make ~loc [ Pp.text "Already set here" ] ]
206+
in
207+
User_message.Annots.singleton
208+
Compound_user_error.annot
209+
[ Compound_user_error.make ~main ~related ]
210+
in
211+
User_error.raise
212+
~annots
213+
~loc
214+
[ main_message
215+
; Pp.text "The first definition is at:"
216+
; Pp.text (Loc.to_file_colon_line loc')
217+
])
218+
in
219+
let enabled_if = stanza.enabled_if :: acc.enabled_if in
220+
let alias =
221+
match stanza.alias with
222+
| None -> acc.alias
223+
| Some a -> Alias.Name.Set.add acc.alias a
224+
in
225+
let packages =
226+
match stanza.package with
227+
| None -> acc.packages
228+
| Some (p : Package.t) ->
229+
Package.Name.Set.add acc.packages (Package.Id.name p.id)
230+
in
231+
( runtest_alias
232+
, { acc with enabled_if; locks; deps; alias; packages; sandbox } ))
233+
in
234+
let alias =
235+
let to_add =
236+
match runtest_alias with
237+
| None | Some (_, true) -> Alias.Name.Set.singleton Alias0.runtest
238+
| Some (_, false) -> Alias.Name.Set.empty
239+
in
240+
Alias.Name.Set.union to_add acc.alias
241+
in
242+
{ acc with alias }
197243
in
198244
with_package_mask spec.packages (fun () -> test_rule ~sctx ~expander ~dir spec test))
199245
;;

src/dune_rules/cram/cram_stanza.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type t =
2828
; enabled_if : Blang.t
2929
; locks : Locks.t
3030
; package : Package.t option
31+
; runtest_alias : (Loc.t * bool) option
3132
}
3233

3334
type Stanza.t += T of t
@@ -42,6 +43,10 @@ let decode =
4243
and+ locks = Locks.field ~check:(Dune_lang.Syntax.since Stanza.syntax (2, 9)) ()
4344
and+ package =
4445
Stanza_common.Pkg.field_opt ~check:(Dune_lang.Syntax.since Stanza.syntax (2, 8)) ()
46+
and+ runtest_alias =
47+
field_o
48+
"runtest_alias"
49+
(Dune_lang.Syntax.since Stanza.syntax (3, 11) >>> located bool)
4550
in
46-
{ loc; alias; deps; enabled_if; locks; applies_to; package })
51+
{ loc; alias; deps; enabled_if; locks; applies_to; package; runtest_alias })
4752
;;

src/dune_rules/cram/cram_stanza.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type t =
1212
; enabled_if : Blang.t
1313
; locks : Locks.t
1414
; package : Package.t option
15+
; runtest_alias : (Loc.t * bool) option
1516
}
1617

1718
type Stanza.t += T of t
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Control the default runtest alias for cram tests
2+
3+
$ cat >dune-project <<EOF
4+
> (lang dune 3.11)
5+
> EOF
6+
7+
$ cat >dune <<EOF
8+
> (cram
9+
> (runtest_alias false)
10+
> (alias this))
11+
> EOF
12+
13+
$ cat >foo.t <<EOF
14+
> $ echo foo
15+
> EOF
16+
17+
This shouldn't run the test
18+
19+
$ dune runtest
20+
21+
This should run the test
22+
23+
$ dune build @this
24+
File "foo.t", line 1, characters 0-0:
25+
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
26+
[1]
27+
28+
Now we try setting runtest alias default twice. This should be impossible:
29+
30+
$ cat >dune <<EOF
31+
> (cram (runtest_alias false))
32+
> (cram (runtest_alias true))
33+
> EOF
34+
35+
$ dune build @a
36+
File "dune", line 2, characters 21-25:
37+
2 | (cram (runtest_alias true))
38+
^^^^
39+
Error: enabling or disabling the runtest alias for a cram test may only be
40+
set once.
41+
It's already set for the test "foo"
42+
The first definition is at:
43+
dune:1
44+
[1]

0 commit comments

Comments
 (0)