Skip to content

Commit 59b58ef

Browse files
committed
feature(cram): allow overriding default alias
Introduce a [(default_aliases ..)] that allows to set the default alias for a cram test. Unlike, the existing [alias] field, we're only allowed to set the default once. If the default isn't set, it's just [runtest]. Signed-off-by: Rudi Grinberg <[email protected]> <!-- ps-id: 23568b34-517f-464d-93e2-ac2c0f05f885 -->
1 parent 564f2eb commit 59b58ef

File tree

5 files changed

+148
-46
lines changed

5 files changed

+148
-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 `default_aliases` to the `cram` stanza. This allows overriding
2+
the `runtest` default we currently have. (@rgrinberg, #8887)

src/dune_rules/cram/cram_rules.ml

Lines changed: 90 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,96 @@ 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+ default_aliases, acc =
157+
Memo.List.fold_left
158+
stanzas
159+
~init
160+
~f:(fun (default_aliases, (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 (default_aliases, 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 default_aliases =
187+
match stanza.default_aliases with
188+
| None -> default_aliases
189+
| Some (loc, names) ->
190+
(match default_aliases with
191+
| None -> Some (loc, names)
192+
| Some (loc', _) ->
193+
let main_message =
194+
Pp.concat
195+
~sep:Pp.newline
196+
[ Pp.text
197+
"the default alias for a cram test may only be set once."
198+
; Pp.textf "It's already set for the test %S" name
199+
]
200+
in
201+
let annots =
202+
let main = User_message.make ~loc:loc' [ main_message ] in
203+
let related =
204+
[ User_message.make ~loc [ Pp.text "Already defined here" ] ]
205+
in
206+
User_message.Annots.singleton
207+
Compound_user_error.annot
208+
[ Compound_user_error.make ~main ~related ]
209+
in
210+
User_error.raise
211+
~annots
212+
~loc
213+
[ main_message
214+
; Pp.text "The first definition is at:"
215+
; Pp.text (Loc.to_file_colon_line loc')
216+
])
217+
in
218+
let enabled_if = stanza.enabled_if :: acc.enabled_if in
219+
let alias =
220+
match stanza.alias with
221+
| None -> acc.alias
222+
| Some a -> Alias.Name.Set.add acc.alias a
223+
in
224+
let packages =
225+
match stanza.package with
226+
| None -> acc.packages
227+
| Some (p : Package.t) ->
228+
Package.Name.Set.add acc.packages (Package.Id.name p.id)
229+
in
230+
( default_aliases
231+
, { acc with enabled_if; locks; deps; alias; packages; sandbox } ))
232+
in
233+
let alias =
234+
let to_add =
235+
match default_aliases with
236+
| None -> Alias.Name.Set.singleton Alias0.runtest
237+
| Some (_loc, aliases) -> Alias.Name.Set.of_list aliases
238+
in
239+
Alias.Name.Set.union to_add acc.alias
240+
in
241+
{ acc with alias }
197242
in
198243
with_package_mask spec.packages (fun () -> test_rule ~sctx ~expander ~dir spec test))
199244
;;

src/dune_rules/cram/cram_stanza.ml

Lines changed: 7 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+
; default_aliases : (Loc.t * Alias.Name.t list) option
3132
}
3233

3334
type Stanza.t += T of t
@@ -42,6 +43,11 @@ 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+ default_aliases =
47+
field_o
48+
"default_aliases"
49+
(Dune_lang.Syntax.since Stanza.syntax (3, 11)
50+
>>> located (repeat Dune_lang.Alias.decode))
4551
in
46-
{ loc; alias; deps; enabled_if; locks; applies_to; package })
52+
{ loc; alias; deps; enabled_if; locks; applies_to; package; default_aliases })
4753
;;

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+
; default_aliases : (Loc.t * Alias.Name.t list) option
1516
}
1617

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

0 commit comments

Comments
 (0)