From 5d0517127f1d28a7905471fde09cc2ff0fd9e53f Mon Sep 17 00:00:00 2001 From: Marek Kubica Date: Fri, 23 Aug 2024 14:06:09 +0200 Subject: [PATCH] RFC: Make it possible to disable line wrapping in tests (#10841) * feat(test): Make it possible to disable line wrapping Sometimes cram tests are not reproducible if different platforms output different length errors, in which case even editing out the errors with common tools like `sed` does not help as the wrapping happens before the `sed` invocation runs, thus the message is split over multiple lines. This introduces an option to disable the automatic wrapping of lines. --------- Signed-off-by: Marek Kubica --- otherlibs/stdune/src/ansi_color.ml | 8 ++++ otherlibs/stdune/src/format.ml | 4 ++ otherlibs/stdune/src/format.mli | 3 ++ otherlibs/stdune/src/int.ml | 1 + otherlibs/stdune/src/int.mli | 1 + otherlibs/stdune/src/stdune.ml | 1 + test/blackbox-tests/test-cases/long-lines.t | 47 +++++++++++++++++++++ 7 files changed, 65 insertions(+) create mode 100644 otherlibs/stdune/src/format.ml create mode 100644 otherlibs/stdune/src/format.mli create mode 100644 test/blackbox-tests/test-cases/long-lines.t diff --git a/otherlibs/stdune/src/ansi_color.ml b/otherlibs/stdune/src/ansi_color.ml index 3da20d05158..76dd4e4b3fd 100644 --- a/otherlibs/stdune/src/ansi_color.ml +++ b/otherlibs/stdune/src/ansi_color.ml @@ -494,6 +494,13 @@ let rec tag_handler buf current_styles ppf (styles : Style.t list) pp = (Style.escape_sequence_buf buf (current_styles :> Style.t list)) ;; +let skip_line_break = + lazy + (match Sys.getenv_opt "DUNE_CONFIG__SKIP_LINE_BREAK" with + | Some "enabled" -> true + | _ -> false) +;; + let make_printer supports_color ppf = let f = lazy @@ -504,6 +511,7 @@ let make_printer supports_color ppf = else Pp.to_fmt ppf) in Staged.stage (fun pp -> + if Lazy.force skip_line_break then Format.pp_set_margin ppf Format.pp_infinity; Lazy.force f pp; Format.pp_print_flush ppf ()) ;; diff --git a/otherlibs/stdune/src/format.ml b/otherlibs/stdune/src/format.ml new file mode 100644 index 00000000000..964d6e19281 --- /dev/null +++ b/otherlibs/stdune/src/format.ml @@ -0,0 +1,4 @@ +(* added in OCaml 5.2 *) +let[@warning "-32"] pp_infinity = Int.max_int + +include Stdlib.Format diff --git a/otherlibs/stdune/src/format.mli b/otherlibs/stdune/src/format.mli new file mode 100644 index 00000000000..c78f84bd6b4 --- /dev/null +++ b/otherlibs/stdune/src/format.mli @@ -0,0 +1,3 @@ +val pp_infinity : int + +include module type of Stdlib.Format diff --git a/otherlibs/stdune/src/int.ml b/otherlibs/stdune/src/int.ml index 59f40c4e4ef..0a45642fa60 100644 --- a/otherlibs/stdune/src/int.ml +++ b/otherlibs/stdune/src/int.ml @@ -47,3 +47,4 @@ module Infix = Comparator.Operators (T) let of_string s = int_of_string_opt s let shift_left = Stdlib.Int.shift_left let shift_right = Stdlib.Int.shift_right +let max_int = Stdlib.Int.max_int diff --git a/otherlibs/stdune/src/int.mli b/otherlibs/stdune/src/int.mli index 923850cdb6f..fede737a938 100644 --- a/otherlibs/stdune/src/int.mli +++ b/otherlibs/stdune/src/int.mli @@ -15,3 +15,4 @@ module Infix : Comparator.OPS with type t = t val shift_left : t -> t -> t val shift_right : t -> t -> t +val max_int : t diff --git a/otherlibs/stdune/src/stdune.ml b/otherlibs/stdune/src/stdune.ml index b6b27187765..a8bcc2c6ece 100644 --- a/otherlibs/stdune/src/stdune.ml +++ b/otherlibs/stdune/src/stdune.ml @@ -13,6 +13,7 @@ module Exn = Exn module Exn_with_backtrace = Exn_with_backtrace module Filename = Filename module Filename_set = Filename_set +module Format = Format module Hashtbl = Hashtbl module Table = Table module Int = Int diff --git a/test/blackbox-tests/test-cases/long-lines.t b/test/blackbox-tests/test-cases/long-lines.t new file mode 100644 index 00000000000..898a6fe191d --- /dev/null +++ b/test/blackbox-tests/test-cases/long-lines.t @@ -0,0 +1,47 @@ +Test the behavior of long lines + +In the default output, user input affects on which lines which of the output +lines appear. For example, with a short project name, the line wraps later: + + $ cat > dune-project < (lang dune 3.17) + > (package + > (name short)) + > EOF + $ dune build + Error: The package short does not have any user defined stanzas attached to + it. If this is intentional, add (allow_empty) to the package definition in + the dune-project file + -> required by _build/default/short.install + -> required by alias all + -> required by alias default + [1] + +With a long project name, there are less words on the line and thus it wraps in +a different position: + + $ cat > dune-project < (lang dune 3.17) + > (package + > (name verylongnamethatcauseslinewrappingincases)) + > EOF + $ dune build + Error: The package verylongnamethatcauseslinewrappingincases does not have + any user defined stanzas attached to it. If this is intentional, add + (allow_empty) to the package definition in the dune-project file + -> required by + _build/default/verylongnamethatcauseslinewrappingincases.install + -> required by alias all + -> required by alias default + [1] + +Disabling line breaks should thus only break lines where there are explicit +line breaks in the input and not when the line gets too long. + + $ export DUNE_CONFIG__SKIP_LINE_BREAK=enabled + $ dune build + Error: The package verylongnamethatcauseslinewrappingincases does not have any user defined stanzas attached to it. If this is intentional, add (allow_empty) to the package definition in the dune-project file + -> required by _build/default/verylongnamethatcauseslinewrappingincases.install + -> required by alias all + -> required by alias default + [1]