Skip to content

Commit

Permalink
Add --sort-by-stats flag to html reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Feb 10, 2024
1 parent 004a913 commit 24765e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
42 changes: 40 additions & 2 deletions src/report/html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,37 @@ type index_element =
| File of index_file
| Directory of (string * index_element list * (int * int))

let output_html_index ~tree title theme filename files =
module Index_element : sig
type t = index_element

val sort_by_stats : t list -> t list

val flatten : t list -> t list
end = struct
type t = index_element

let percentage = function
| File (_, _, stat) -> percentage stat
| Directory (_, _, stat) -> percentage stat

let compare_by_stat e1 e2 =
compare (percentage e1, e1) (percentage e2, e2)

let rec sort_by_stats files =
files
|> List.map (function
| (File _) as f -> f
| Directory (name, files, stats) -> Directory (name, sort_by_stats files, stats))
|> List.sort compare_by_stat

let rec flatten files =
files
|> List.concat_map (function
| (File _) as f -> [ f ]
| Directory (_, files, _) -> flatten files)
end

let output_html_index ~tree ~sort_by_stats title theme filename files =
Util.info "Writing index file...";

let add_stats (visited, total) (visited', total') =
Expand Down Expand Up @@ -138,6 +168,13 @@ let output_html_index ~tree title theme filename files =

let (files, stats) = collate files in

let files =
match sort_by_stats, tree with
| false, (false|true) -> files
| true, false -> files |> Index_element.flatten |> Index_element.sort_by_stats
| true, true -> files |> Index_element.sort_by_stats
in

let overall_coverage =
Printf.sprintf "%.02f%%" (floor ((percentage stats) *. 100.) /. 100.) in
write {|<!DOCTYPE html>
Expand Down Expand Up @@ -501,7 +538,7 @@ let output_string_to_separate_file content filename =

let output
~to_directory ~title ~tab_size ~theme ~coverage_files ~coverage_paths
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree =
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree ~sort_by_stats =

(* Read all the [.coverage] files and get per-source file visit counts. *)
let coverage =
Expand Down Expand Up @@ -535,6 +572,7 @@ let output
(* Write the coverage report landing page. *)
output_html_index
~tree
~sort_by_stats
title
theme
(Filename.concat to_directory "index.html")
Expand Down
1 change: 1 addition & 0 deletions src/report/html.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ val output :
expect:string list ->
do_not_expect:string list ->
tree:bool ->
sort_by_stats:bool ->
unit
12 changes: 9 additions & 3 deletions src/report/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,23 @@ let html =
info ["tree"] ~doc:
("Generate collapsible directory tree with per-directory summaries."))
in
let sort_by_stats =
Arg.(value @@ flag @@
info ["sort-by-stats"] ~doc:
("Sort reported files by increasing values of coverage stats."))
in

let call_with_labels
to_directory title tab_size theme coverage_files coverage_paths
source_paths ignore_missing_files expect do_not_expect tree =
source_paths ignore_missing_files expect do_not_expect tree sort_by_stats =
Html.output
~to_directory ~title ~tab_size ~theme ~coverage_files ~coverage_paths
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree ~sort_by_stats
in
Term.(const set_verbose $ verbose $ const call_with_labels $ to_directory
$ title $ tab_size $ theme $ coverage_files 0 $ coverage_paths
$ source_paths $ ignore_missing_files $ expect $ do_not_expect $ tree),
$ source_paths $ ignore_missing_files $ expect $ do_not_expect $ tree
$ sort_by_stats),
term_info "html" ~doc:"Generate HTML report locally."
~man:[
`S "USAGE EXAMPLE";
Expand Down

0 comments on commit 24765e2

Please sign in to comment.