@@ -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;;
0 commit comments