From 301496b6a10c0d562cf0e0fa516c313025f07e79 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 24 Aug 2026 23:44:14 +0100 Subject: [PATCH] refactor(cram): abstract test names Represent cram test names with an abstract Cram_test.Name.t and convert them to strings only at alias and predicate boundaries. Signed-off-by: Rudi Grinberg --- bin/runtest_common.ml | 2 +- src/dune_rules/cram/cram_rules.ml | 11 ++++++++--- src/source/cram_test.ml | 7 +++++++ src/source/cram_test.mli | 10 +++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/bin/runtest_common.ml b/bin/runtest_common.ml index 134b0b78033..876a29e36f7 100644 --- a/bin/runtest_common.ml +++ b/bin/runtest_common.ml @@ -15,7 +15,7 @@ module Test_kind = struct let alias ~contexts = function | Cram (dir, cram) -> - let name = Dune_engine.Alias.Name.of_string (Source.Cram_test.name cram) in + let name = Source.Cram_test.name cram |> Source.Cram_test.Name.to_alias in Alias.in_dir ~name ~recursive:false ~contexts dir | Test_executable { dir; exe_name } -> let name = Dune_engine.Alias.Name.of_string ("runtest-" ^ exe_name) in diff --git a/src/dune_rules/cram/cram_rules.ml b/src/dune_rules/cram/cram_rules.ml index 996b2ca29e3..a3b5cb13668 100644 --- a/src/dune_rules/cram/cram_rules.ml +++ b/src/dune_rules/cram/cram_rules.ml @@ -256,7 +256,7 @@ let spec_for_test ~stanzas test = | Ok test -> Cram_test.name test | Error (Missing_run_t test) -> Cram_test.name test in - let test_name_alias = Alias.Name.of_string name in + let test_name_alias = Cram_test.Name.to_alias name in let init = None, Spec.make_empty ~test_name_alias in let* runtest_alias, acc = Memo.List.fold_left @@ -267,7 +267,10 @@ let spec_for_test ~stanzas test = match stanza.applies_to with | Whole_subtree -> true | Files_matching_in_this_dir pred -> - Predicate_lang.Glob.test pred ~standard:Predicate_lang.true_ name + Predicate_lang.Glob.test + pred + ~standard:Predicate_lang.true_ + (Cram_test.Name.to_string name) with | false -> Memo.return (runtest_alias, acc) | true -> @@ -308,7 +311,9 @@ let spec_for_test ~stanzas test = [ Pp.text "enabling or disabling the runtest alias for a cram test may only \ be set once." - ; Pp.textf "It's already set for the test %S" name + ; Pp.textf + "It's already set for the test %S" + (Cram_test.Name.to_string name) ] in let compound = diff --git a/src/source/cram_test.ml b/src/source/cram_test.ml index ab58c669101..74db2736253 100644 --- a/src/source/cram_test.ml +++ b/src/source/cram_test.ml @@ -7,6 +7,13 @@ type t = ; dir : Path.Source.t } +module Name = struct + type t = string + + let to_string s = s + let to_alias = Alias_name.of_string +end + let fname_in_dir_test = Filename.run_t let suffix = ".t" let is_cram_suffix fn = String.ends_with (Filename.to_string fn) ~suffix diff --git a/src/source/cram_test.mli b/src/source/cram_test.mli index e1048d643a3..7b737cb0cb3 100644 --- a/src/source/cram_test.mli +++ b/src/source/cram_test.mli @@ -10,6 +10,14 @@ type t = val to_dyn : t -> Dyn.t +module Name : sig + (** The basename used to identify a cram test. *) + type t + + val to_string : t -> string + val to_alias : t -> Alias_name.t +end + (** Checks if a filename has the ".t" suffix for a cram test. *) val is_cram_suffix : Filename.t -> bool @@ -19,7 +27,7 @@ val fname_in_dir_test : Filename.t (** The [name] of a cram test. If this is a file test, then it will be the file name without the cram suffix. If this is a directory test, then it will be the directory name without the cram suffix. *) -val name : t -> string +val name : t -> Name.t (** The [path] associated to a cram test. If this is a file test, then it will be the file. If this is a directory test, then it will be the directory. *)