From 67e1aeebd083e8a19e8b47c70510e7e8f6ab8f4e Mon Sep 17 00:00:00 2001 From: Torben Ewert Date: Sun, 14 Jul 2024 15:26:08 +0200 Subject: [PATCH] feat: add progress counter --- lib/OSnap.ml | 1 + lib/OSnap_Test/OSnap_Test_Printer.ml | 49 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/OSnap.ml b/lib/OSnap.ml index 3159a34..d633a9e 100644 --- a/lib/OSnap.ml +++ b/lib/OSnap.ml @@ -85,6 +85,7 @@ let run ~env t = parallelism (fun () -> Browser.Target.make browser) in + Test.Printer.Progress.set_total (List.length tests_to_run); let*? test_results = tests_to_run |> ResultList.map_p_until_first_error (fun test -> diff --git a/lib/OSnap_Test/OSnap_Test_Printer.ml b/lib/OSnap_Test/OSnap_Test_Printer.ml index 05f84c4..25468da 100644 --- a/lib/OSnap_Test/OSnap_Test_Printer.ml +++ b/lib/OSnap_Test/OSnap_Test_Printer.ml @@ -1,18 +1,48 @@ open Fmt open OSnap_Test_Types +module Progress = struct + type t = + { mutable current : int + ; mutable total : int + ; mutable total_length : int + } + + let progress = { current = 0; total = 0; total_length = 0 } + let progress_mutex = Mutex.create () + + let get_and_incr () = + Mutex.lock progress_mutex; + progress.current <- succ progress.current; + Mutex.unlock progress_mutex; + Fmt.str_like + Fmt.stdout + "%a" + (styled `Faint string) + (Printf.sprintf "%*i / %i " progress.total_length progress.current progress.total) + ;; + + let set_total i = + Mutex.lock progress_mutex; + progress.total <- i; + progress.total_length <- String.length (string_of_int i); + Mutex.unlock progress_mutex + ;; +end + let test_name ~name ~width ~height = Fmt.str_like Fmt.stdout "%s %a" name (styled `Faint string) - (Printf.sprintf "(%ix%i)" width height) + (Printf.sprintf "@ %i×%i" width height) ;; let created_message ~name ~width ~height = Fmt.pr - "%a\t%s @." + "%s %a %s @." + (Progress.get_and_incr ()) (styled `Bold (styled `Blue string)) "CREATE" (test_name ~name ~width ~height) @@ -20,7 +50,8 @@ let created_message ~name ~width ~height = let skipped_message ~name ~width ~height = Fmt.pr - "%a\t%s @." + "%s %a %s @." + (Progress.get_and_incr ()) (styled `Bold (styled `Yellow string)) "SKIP" (test_name ~name ~width ~height) @@ -28,7 +59,8 @@ let skipped_message ~name ~width ~height = let success_message ~name ~width ~height = Fmt.pr - "%a\t%s @." + "%s %a %s @." + (Progress.get_and_incr ()) (styled `Bold (styled `Green string)) "PASS" (test_name ~name ~width ~height) @@ -38,7 +70,8 @@ let layout_message ~print_head ~name ~width ~height = if print_head then Fmt.pr - "%a\t%s %a @." + "%s %a %s %a @." + (Progress.get_and_incr ()) (styled `Bold (styled `Red string)) "FAIL" (test_name ~name ~width ~height) @@ -56,7 +89,8 @@ let diff_message ~print_head ~name ~width ~height ~diffCount ~diffPercentage = if print_head then Fmt.pr - "%a\t%s %a @." + "%s %a %s %a @." + (Progress.get_and_incr ()) (styled `Bold (styled `Red string)) "FAIL" (test_name ~name ~width ~height) @@ -74,7 +108,8 @@ let corrupted_message ~print_head ~name ~width ~height = if print_head then Fmt.pr - "%a\t%s %a @." + "%s %a %s %a @." + (Progress.get_and_incr ()) (styled `Bold (styled `Red string)) "FAIL" (test_name ~name ~width ~height) -- 2.51.2