From c92c10305ee4cace6ce5ffe44af05245ee704b81 Mon Sep 17 00:00:00 2001 From: Romain Date: Wed, 4 Jun 2025 12:04:37 +0200 Subject: [PATCH] add --junit-tag --- CHANGES.md | 2 ++ lib_core/cli.ml | 35 +++++++++++++++++++++++++++++++++++ lib_core/cli.mli | 3 +++ lib_core/test.ml | 8 +++++++- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index acf5d43..c8a20f3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -34,6 +34,8 @@ - The plus sign `+` no longer needs to be quoted in TSL strings. +- Added `--junit-tag`, which allows to include test tags as properties in JUnit reports. + ### Breaking Changes - Tezt is no longer available for OCaml versions earlier than 4.13. diff --git a/lib_core/cli.ml b/lib_core/cli.ml index e80b656..ec0f7c3 100644 --- a/lib_core/cli.ml +++ b/lib_core/cli.ml @@ -473,6 +473,41 @@ module Reports = struct --mem-warn), it is also stored in JUnit files as a with \ this name." "memory.peak" + + let name_and_tag = + Clap.typ + ~name:"name and tag" + ~dummy:("", "") + ~parse:(fun str -> + (* Since tags cannot contain '=' but property names could, + use the last '=' as delimiter. *) + let name, tag = + match String.rindex_opt str '=' with + | None -> (str, str) + | Some index -> + let name = String.sub str 0 index in + let tag = + String.sub str (index + 1) (String.length str - index - 1) + in + (name, tag) + in + if TSL.is_valid_tag tag then Some (name, tag) else None) + ~show:(fun (name, tag) -> name ^ "=" ^ tag) + + let junit_tags = + Clap.list + name_and_tag + ~section + ~long:"junit-tag" + ~placeholder:"NAME_AND_TAG" + ~description: + "For each test, in the JUnit report, include a indicating \ + whether the test has a given tag. If NAME_AND_TAG is of the form \ + NAME=TAG, each test is annotated with a property named NAME with \ + value 'true' or 'false' depending on whether the test has tag TAG. \ + NAME_AND_TAG can also be of the form TAG, in which case NAME defaults \ + to TAG." + () end module Commands = struct diff --git a/lib_core/cli.mli b/lib_core/cli.mli index 9e5b7f6..7114d8c 100644 --- a/lib_core/cli.mli +++ b/lib_core/cli.mli @@ -185,6 +185,9 @@ module Reports : sig (** [--junit-mem-peak] *) val junit_mem_peak : string + + (** [--junit-tag] *) + val junit_tags : (string * string) list end module Commands : sig diff --git a/lib_core/test.ml b/lib_core/test.ml index 71594fe..5c4ebd0 100644 --- a/lib_core/test.ml +++ b/lib_core/test.ml @@ -1145,11 +1145,17 @@ let output_junit filename = Buffer.contents buffer in let title = replace_entities test.title in - let properties = + let properties_1 = match test.session_peak_memory_usage with | None -> [] | Some peak -> [(Cli.Reports.junit_mem_peak, string_of_int peak)] in + let properties_2 = + Cli.Reports.junit_tags + |> List.map @@ fun (name, tag) -> + (name, string_of_bool (List.mem tag test.tags)) + in + let properties = properties_1 @ properties_2 in echo {| |} title -- GitLab