Skip to content

Commit 65766cb

Browse files
committed
fix: respect the dir stanza for assigning cram tests to packages
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 0e10560 commit 65766cb

5 files changed

Lines changed: 43 additions & 24 deletions

File tree

doc/changes/fixed/13581.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Respect the `(dir ..)` field on packages when setting up cram tests (#13581,
2+
@rgrinberg)

src/dune_lang/dune_project.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ let stanza_parser t ~dir =
122122
Decoder.set Package_mask.key mask parser
123123
;;
124124

125+
let exclusive_package t ~dir =
126+
match Package_mask.package_env ~dir ~packages:t.exclusive_dir_packages with
127+
| Inside_package p -> Some p
128+
| Forbidden_packages _ -> None
129+
;;
130+
125131
let file t = t.project_file
126132

127133
let implicit_transitive_deps t ocaml_version =

src/dune_lang/dune_project.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type t
1515

1616
val to_dyn : t -> Dyn.t
1717
val packages : t -> Package.t Package.Name.Map.t
18+
val exclusive_package : t -> dir:Path.Source.t -> Package_id.t option
1819
val name : t -> Dune_project_name.t
1920
val version : t -> Package_version.t option
2021
val root : t -> Path.Source.t

src/dune_rules/cram/cram_rules.ml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,41 @@ let collect_stanzas =
201201
| Some dir -> collect_whole_subtree [ acc ] dir
202202
;;
203203

204-
let rules ~sctx ~dir tests =
204+
let rules ~sctx ~dir tests project =
205205
let* stanzas = collect_stanzas ~dir
206206
and* with_package_mask =
207-
Dune_load.mask ()
208-
>>| Only_packages.enumerate
209-
>>| function
210-
| `All -> fun _packages f -> f ()
211-
| `Set only ->
212-
fun packages f ->
213-
Memo.when_
214-
(Package.Name.Set.is_empty packages
215-
|| Package.Name.Set.(not (is_empty (inter only packages))))
216-
f
207+
let+ mask = Dune_load.mask () >>| Only_packages.enumerate in
208+
match
209+
Dune_project.exclusive_package project ~dir:(Path.Build.drop_build_context_exn dir)
210+
|> Option.map ~f:Package.Id.name
211+
with
212+
| None ->
213+
(match mask with
214+
| `All -> fun _packages f -> f ()
215+
| `Set only ->
216+
fun packages f ->
217+
Memo.when_
218+
(Package.Name.Set.is_empty packages
219+
|| Package.Name.Set.(not (is_empty (inter only packages))))
220+
f)
221+
| Some p ->
222+
let singleton = Package.Name.Set.singleton p in
223+
let with_validate_packages packages ~f =
224+
if Package.Name.Set.is_empty packages || Package.Name.Set.equal packages singleton
225+
then f ()
226+
else
227+
Code_error.raise
228+
"All cram tests in this directory belong to a particular package by virtue \
229+
of the dir stanza in the packge declaration itself. It's not possible to \
230+
re-assign it to another package using the cram stanza"
231+
[ "package", Package.Name.to_dyn p ]
232+
in
233+
(match mask with
234+
| `All -> fun packages f -> with_validate_packages packages ~f
235+
| `Set only ->
236+
if Package.Name.Set.mem only p
237+
then fun packages f -> with_validate_packages packages ~f
238+
else fun packages _f -> with_validate_packages packages ~f:Memo.return)
217239
in
218240
Memo.parallel_iter tests ~f:(fun test ->
219241
let* spec =
@@ -396,5 +418,5 @@ let rules ~sctx ~dir source_dir =
396418
cram_tests source_dir
397419
>>= function
398420
| [] -> Memo.return ()
399-
| tests -> rules ~sctx ~dir tests
421+
| tests -> rules ~sctx ~dir tests (Source_tree.Dir.project source_dir)
400422
;;

test/blackbox-tests/test-cases/cram/exclusive-package-test.t

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ This command should only run the packages for foo:
2222
# CR-someday rgrinberg: what happened to formatting here?!
2323

2424
$ dune runtest --only-packages foo
25-
File "bar/dir.t/run.t", line 1, characters 0-0:
26-
--- bar/dir.t/run.t
27-
+++ bar/dir.t/run.t.corrected
28-
@@ -1 +1,2 @@
29-
$ echo foo
30-
+ foo
31-
File "bar/file.t", line 1, characters 0-0:
32-
--- bar/file.t
33-
+++ bar/file.t.corrected
34-
@@ -1 +1,2 @@
35-
$ echo foo
36-
+ foo
3725
File "foo/dir.t/run.t", line 1, characters 0-0:
3826
--- foo/dir.t/run.t
3927
+++ foo/dir.t/run.t.corrected

0 commit comments

Comments
 (0)