Skip to content

Commit

Permalink
tests a shields.io badge showing coverage from coverdata JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
shortishly committed Jul 17, 2023
1 parent 5a33225 commit 03ba670
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
10 changes: 10 additions & 0 deletions _site/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html lang="en">
<head>
<title>coverdata</title>
</head>
<body>
<img alt="Dynamic JSON Badge"
src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fshortishly.github.io%2Fcoverdata%2Fscran.json&query=%24.total&suffix=%25&label=coverage">
</body>
</html>
94 changes: 65 additions & 29 deletions src/coverdata.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,98 @@
-module(coverdata).


-export([import/0]).
-export([main/1]).
-export([report/1]).


main(Args) ->
?FUNCTION_NAME(Args, #{"format" => "json", "level" => "module" }).
?FUNCTION_NAME(
Args,
#{"format" => "json",
"precision" => "5",
"level" => "module" }).

main([[$-, $- | Key], Value | Args], A) ->
?FUNCTION_NAME(Args, A#{Key => Value});

main([],
#{"input" := Input,
"output" := Output,
"level" := Level,
"format" := Format}) ->
main([], Arg) ->
process(
maps:map(
fun
("precision", Precision) ->
list_to_integer(Precision);

("level", Level) ->
list_to_atom(Level);

(_, Value) ->
Value
end,
Arg)).


process(#{"input" := Input, "output" := Output} = Arg) ->
ok = cover:import(Input),
ok = file:write_file(
Output,
report(Format, Level)).
ok = file:write_file(Output, format_report(Arg));

process(#{"output" := Output} = Arg) ->

{ok, CWD} = file:get_cwd(),

true = lists:all(
fun
(Result) ->
Result == ok
end,
[cover:import(CoverData)
|| CoverData
<- lists:map(
fun
(Relative) ->
filename:join(CWD, Relative)
end,
filelib:wildcard("**/*.coverdata"))
-- cover:imported()]),

ok = file:write_file(Output, format_report(Arg)).

report("json", Level) ->
jsx:encode(report(Level)).

report("module" = Level) ->
{result, Analysed, _} = cover:analyse(list_to_atom(Level)),
-spec format_report(#{}) -> iodata().

format_report(#{"format" := "json"} = Arg) ->
jsx:encode(report(Arg)).


report(#{"level" := module = Level, "precision" := Precision}) ->
{result, Analysed, _} = cover:analyse(Level),
{Report, Total} = lists:foldl(
fun
({_, {0, 0}}, A) ->
A;

({Module, {Cov, NotCov} = Counts},
{A, {TotalCov, TotalNotCov}}) ->
{A#{Module => percentage(Counts)},
{A#{Module => percentage(Counts, Precision)},
{Cov + TotalCov, NotCov + TotalNotCov}}
end,
{#{}, {0, 0}},
Analysed),
Report#{total => percentage(Total)}.
Report#{total => percentage(Total, Precision)}.


-spec percentage({non_neg_integer(), non_neg_integer()}, non_neg_integer()) -> float().

percentage({Cov, NotCov}) ->
precision(Cov * 100 / (Cov + NotCov), 5).
percentage({Cov, NotCov}, Precision) ->
precision(Cov * 100 / (Cov + NotCov), Precision).


-spec precision(float(), non_neg_integer()) -> float().

precision(Value, Digits) ->
Decimals = float_to_binary(
Value,
[{decimals, Digits - trunc(math:ceil(math:log10(trunc(abs(Value)) + 1)))}]),
[{decimals, decimals(Value, Digits)}]),

case binary:split(Decimals, <<".">>) of
[_, _] ->
binary_to_float(Decimals);
Expand All @@ -75,13 +117,7 @@ precision(Value, Digits) ->
end.


import() ->
{ok, CWD} = file:get_cwd(),
[cover:import(CoverData)
|| CoverData
<- lists:map(
fun
(Relative) ->
filename:join(CWD, Relative)
end,
filelib:wildcard("**/*.coverdata")) -- cover:imported()].
-spec decimals(float(), non_neg_integer()) -> integer().

decimals(Value, Digits) ->
Digits - trunc(math:ceil(math:log10(trunc(abs(Value)) + 1))).
4 changes: 3 additions & 1 deletion test/coverdata_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ scan_test() ->
scran_number_le => 0.0,
scran_bits => 100.0,
scran_character_complete => 98.667},
coverdata:report("module")).
coverdata:report(
#{"level" => module,
"precision" => 5})).

0 comments on commit 03ba670

Please sign in to comment.