Skip to content

Commit e302656

Browse files
authored
Add --sort-by-stats flag to HTML reporter (#433)
1 parent 004a913 commit e302656

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/report/html.ml

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,36 @@ type index_element =
3131
| File of index_file
3232
| Directory of (string * index_element list * (int * int))
3333

34-
let output_html_index ~tree title theme filename files =
34+
module Index_element :
35+
sig
36+
val sort_by_stats : index_element list -> index_element list
37+
val flatten : index_element list -> index_element list
38+
end =
39+
struct
40+
let percentage = function
41+
| File (_, _, stat) -> percentage stat
42+
| Directory (_, _, stat) -> percentage stat
43+
44+
let compare_by_stat e1 e2 =
45+
compare (percentage e1, e1) (percentage e2, e2)
46+
47+
let rec sort_by_stats files =
48+
files
49+
|> List.map (function
50+
| (File _) as f -> f
51+
| Directory (name, files, stats) ->
52+
Directory (name, sort_by_stats files, stats))
53+
|> List.sort compare_by_stat
54+
55+
let rec flatten files =
56+
files
57+
|> List.map (function
58+
| (File _) as f -> [f]
59+
| Directory (_, files, _) -> flatten files)
60+
|> List.concat
61+
end
62+
63+
let output_html_index ~tree ~sort_by_stats title theme filename files =
3564
Util.info "Writing index file...";
3665

3766
let add_stats (visited, total) (visited', total') =
@@ -138,6 +167,14 @@ let output_html_index ~tree title theme filename files =
138167

139168
let (files, stats) = collate files in
140169

170+
let files =
171+
match sort_by_stats, tree with
172+
| false, _ -> files
173+
| true, false ->
174+
files |> Index_element.flatten |> Index_element.sort_by_stats
175+
| true, true -> files |> Index_element.sort_by_stats
176+
in
177+
141178
let overall_coverage =
142179
Printf.sprintf "%.02f%%" (floor ((percentage stats) *. 100.) /. 100.) in
143180
write {|<!DOCTYPE html>
@@ -501,7 +538,8 @@ let output_string_to_separate_file content filename =
501538

502539
let output
503540
~to_directory ~title ~tab_size ~theme ~coverage_files ~coverage_paths
504-
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree =
541+
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree
542+
~sort_by_stats =
505543

506544
(* Read all the [.coverage] files and get per-source file visit counts. *)
507545
let coverage =
@@ -535,6 +573,7 @@ let output
535573
(* Write the coverage report landing page. *)
536574
output_html_index
537575
~tree
576+
~sort_by_stats
538577
title
539578
theme
540579
(Filename.concat to_directory "index.html")

src/report/html.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ val output :
1616
expect:string list ->
1717
do_not_expect:string list ->
1818
tree:bool ->
19+
sort_by_stats:bool ->
1920
unit

src/report/main.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,25 @@ let html =
179179
info ["tree"] ~doc:
180180
("Generate collapsible directory tree with per-directory summaries."))
181181
in
182+
let sort_by_stats =
183+
Arg.(value @@ flag @@
184+
info ["sort-by-stats"] ~doc:
185+
("Sort files in order of increasing coverage stats."))
186+
in
182187

183188
let call_with_labels
184189
to_directory title tab_size theme coverage_files coverage_paths
185-
source_paths ignore_missing_files expect do_not_expect tree =
190+
source_paths ignore_missing_files expect do_not_expect tree
191+
sort_by_stats =
186192
Html.output
187193
~to_directory ~title ~tab_size ~theme ~coverage_files ~coverage_paths
188194
~source_paths ~ignore_missing_files ~expect ~do_not_expect ~tree
195+
~sort_by_stats
189196
in
190197
Term.(const set_verbose $ verbose $ const call_with_labels $ to_directory
191198
$ title $ tab_size $ theme $ coverage_files 0 $ coverage_paths
192-
$ source_paths $ ignore_missing_files $ expect $ do_not_expect $ tree),
199+
$ source_paths $ ignore_missing_files $ expect $ do_not_expect $ tree
200+
$ sort_by_stats),
193201
term_info "html" ~doc:"Generate HTML report locally."
194202
~man:[
195203
`S "USAGE EXAMPLE";

0 commit comments

Comments
 (0)