1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
%% Copyright (c) 2008-2009 Nick Gerakines <nick@gerakines.net>
%%
%% Permission is hereby granted, free of charge, to any person
%% obtaining a copy of this software and associated documentation
%% files (the "Software"), to deal in the Software without
%% restriction, including without limitation the rights to use,
%% copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following
%% conditions:
%%
%% The above copyright notice and this permission notice shall be
%% included in all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
%%
%% @doc A module for creating nice looking code coverage reports.
-module(etap_report).
-export([create/0]).
%% @spec create() -> ok
%% @doc Create html code coverage reports for each module that code coverage
%% data exists for.
create() ->
[cover:import(File) || File <- filelib:wildcard("cover/*.coverdata")],
Modules = lists:foldl(
fun(Module, Acc) ->
[{Module, file_report(Module)} | Acc]
end,
[],
cover:imported_modules()
),
index(Modules).
%% @private
index(Modules) ->
{ok, IndexFD} = file:open("cover/index.html", [write]),
io:format(IndexFD, "<html><head><style>
table.percent_graph { height: 12px; border:1px solid #E2E6EF; empty-cells: show; }
table.percent_graph td.covered { height: 10px; background: #00f000; }
table.percent_graph td.uncovered { height: 10px; background: #e00000; }
.odd { background-color: #ddd; }
.even { background-color: #fff; }
</style></head>", []),
io:format(IndexFD, "<body>", []),
lists:foldl(
fun({Module, {Good, Bad, Source}}, LastRow) ->
case {Good + Bad, Source} of
{0, _} -> LastRow;
{_, none} -> LastRow;
_ ->
CovPer = round((Good / (Good + Bad)) * 100),
UnCovPer = round((Bad / (Good + Bad)) * 100),
RowClass = case LastRow of 1 -> "odd"; _ -> "even" end,
io:format(IndexFD, "<div class=\"~s\">", [RowClass]),
io:format(IndexFD, "<a href=\"~s\">~s</a>", [atom_to_list(Module) ++ "_report.html", atom_to_list(Module)]),
io:format(IndexFD, "
<table cellspacing='0' cellpadding='0' align='right'>
<tr>
<td><tt>~p%</tt> </td><td>
<table cellspacing='0' class='percent_graph' cellpadding='0' width='100'>
<tr><td class='covered' width='~p' /><td class='uncovered' width='~p' /></tr>
</table>
</td>
</tr>
</table>
", [CovPer, CovPer, UnCovPer]),
io:format(IndexFD, "</div>", []),
case LastRow of
1 -> 0;
0 -> 1
end
end
end,
0,
lists:sort(Modules)
),
{TotalGood, TotalBad} = lists:foldl(
fun({_, {Good, Bad, Source}}, {TGood, TBad}) ->
case Source of none -> {TGood, TBad}; _ -> {TGood + Good, TBad + Bad} end
end,
{0, 0},
Modules
),
io:format(IndexFD, "<p>Generated on ~s.</p>~n", [etap:datetime({date(), time()})]),
case TotalGood + TotalBad of
0 -> ok;
_ ->
TotalCovPer = round((TotalGood / (TotalGood + TotalBad)) * 100),
TotalUnCovPer = round((TotalBad / (TotalGood + TotalBad)) * 100),
io:format(IndexFD, "<div>", []),
io:format(IndexFD, "Total
<table cellspacing='0' cellpadding='0' align='right'>
<tr>
<td><tt>~p%</tt> </td><td>
<table cellspacing='0' class='percent_graph' cellpadding='0' width='100'>
<tr><td class='covered' width='~p' /><td class='uncovered' width='~p' /></tr>
</table>
</td>
</tr>
</table>
", [TotalCovPer, TotalCovPer, TotalUnCovPer]),
io:format(IndexFD, "</div>", [])
end,
io:format(IndexFD, "</body></html>", []),
file:close(IndexFD),
ok.
%% @private
file_report(Module) ->
{ok, Data} = cover:analyse(Module, calls, line),
Source = find_source(Module),
{Good, Bad} = collect_coverage(Data, {0, 0}),
case {Source, Good + Bad} of
{none, _} -> ok;
{_, 0} -> ok;
_ ->
{ok, SourceFD} = file:open(Source, [read]),
{ok, WriteFD} = file:open("cover/" ++ atom_to_list(Module) ++ "_report.html", [write]),
io:format(WriteFD, "~s", [header(Module, Good, Bad)]),
output_lines(Data, WriteFD, SourceFD, 1),
io:format(WriteFD, "~s", [footer()]),
file:close(WriteFD),
file:close(SourceFD),
ok
end,
{Good, Bad, Source}.
%% @private
collect_coverage([], Acc) -> Acc;
collect_coverage([{{_, _}, 0} | Data], {Good, Bad}) ->
collect_coverage(Data, {Good, Bad + 1});
collect_coverage([_ | Data], {Good, Bad}) ->
collect_coverage(Data, {Good + 1, Bad}).
%% @private
output_lines(Data, WriteFD, SourceFD, LineNumber) ->
{Match, NextData} = datas_match(Data, LineNumber),
case io:get_line(SourceFD, '') of
eof -> ok;
Line = "%% @todo" ++ _ ->
io:format(WriteFD, "~s", [out_line(LineNumber, highlight, Line)]),
output_lines(NextData, WriteFD, SourceFD, LineNumber + 1);
Line = "% " ++ _ ->
io:format(WriteFD, "~s", [out_line(LineNumber, none, Line)]),
output_lines(NextData, WriteFD, SourceFD, LineNumber + 1);
Line ->
case Match of
{true, CC} ->
io:format(WriteFD, "~s", [out_line(LineNumber, CC, Line)]),
output_lines(NextData, WriteFD, SourceFD, LineNumber + 1);
false ->
io:format(WriteFD, "~s", [out_line(LineNumber, none, Line)]),
output_lines(NextData, WriteFD, SourceFD, LineNumber + 1)
end
end.
%% @private
out_line(Number, none, Line) ->
PadNu = string:right(integer_to_list(Number), 5, $.),
io_lib:format("<span class=\"marked\"><a name=\"line~p\"></a>~s ~s</span>", [Number, PadNu, Line]);
out_line(Number, highlight, Line) ->
PadNu = string:right(integer_to_list(Number), 5, $.),
io_lib:format("<span class=\"highlight\"><a name=\"line~p\"></a>~s ~s</span>", [Number, PadNu, Line]);
out_line(Number, 0, Line) ->
PadNu = string:right(integer_to_list(Number), 5, $.),
io_lib:format("<span class=\"uncovered\"><a name=\"line~p\"></a>~s ~s</span>", [Number, PadNu, Line]);
out_line(Number, _, Line) ->
PadNu = string:right(integer_to_list(Number), 5, $.),
io_lib:format("<span class=\"covered\"><a name=\"line~p\"></a>~s ~s</span>", [Number, PadNu, Line]).
%% @private
datas_match([], _) -> {false, []};
datas_match([{{_, Line}, CC} | Datas], LineNumber) when Line == LineNumber -> {{true, CC}, Datas};
datas_match(Data, _) -> {false, Data}.
%% @private
find_source(Module) when is_atom(Module) ->
Root = filename:rootname(Module),
Dir = filename:dirname(Root),
XDir = case os:getenv("SRC") of false -> "src"; X -> X end,
find_source([
filename:join([Dir, Root ++ ".erl"]),
filename:join([Dir, "..", "src", Root ++ ".erl"]),
filename:join([Dir, "src", Root ++ ".erl"]),
filename:join([Dir, "elibs", Root ++ ".erl"]),
filename:join([Dir, "..", "elibs", Root ++ ".erl"]),
filename:join([Dir, XDir, Root ++ ".erl"])
]);
find_source([]) -> none;
find_source([Test | Tests]) ->
case filelib:is_file(Test) of
true -> Test;
false -> find_source(Tests)
end.
%% @private
header(Module, Good, Bad) ->
io:format("Good ~p~n", [Good]),
io:format("Bad ~p~n", [Bad]),
CovPer = round((Good / (Good + Bad)) * 100),
UnCovPer = round((Bad / (Good + Bad)) * 100),
io:format("CovPer ~p~n", [CovPer]),
io_lib:format("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>~s - C0 code coverage information</title>
<style type='text/css'>body { background-color: rgb(240, 240, 245); }</style>
<style type='text/css'>span.marked0 {
background-color: rgb(185, 210, 200);
display: block;
}
span.marked { display: block; background-color: #ffffff; }
span.highlight { display: block; background-color: #fff9d7; }
span.covered { display: block; background-color: #f7f7f7 ; }
span.uncovered { display: block; background-color: #ffebe8 ; }
span.overview {
border-bottom: 1px solid #E2E6EF;
}
div.overview {
border-bottom: 1px solid #E2E6EF;
}
body {
font-family: verdana, arial, helvetica;
}
div.footer {
font-size: 68%;
margin-top: 1.5em;
}
h1, h2, h3, h4, h5, h6 {
margin-bottom: 0.5em;
}
h5 {
margin-top: 0.5em;
}
.hidden {
display: none;
}
div.separator {
height: 10px;
}
table.percent_graph {
height: 12px;
border: 1px solid #E2E6EF;
empty-cells: show;
}
table.percent_graph td.covered {
height: 10px;
background: #00f000;
}
table.percent_graph td.uncovered {
height: 10px;
background: #e00000;
}
table.percent_graph td.NA {
height: 10px;
background: #eaeaea;
}
table.report {
border-collapse: collapse;
width: 100%;
}
table.report td.heading {
background: #dcecff;
border: 1px solid #E2E6EF;
font-weight: bold;
text-align: center;
}
table.report td.heading:hover {
background: #c0ffc0;
}
table.report td.text {
border: 1px solid #E2E6EF;
}
table.report td.value {
text-align: right;
border: 1px solid #E2E6EF;
}
table.report tr.light {
background-color: rgb(240, 240, 245);
}
table.report tr.dark {
background-color: rgb(230, 230, 235);
}
</style>
</head>
<body>
<h3>C0 code coverage information</h3>
<p>Generated on ~s with <a href='http://github.com/ngerakines/etap'>etap 0.3.4</a>.
</p>
<table class='report'>
<thead>
<tr>
<td class='heading'>Name</td>
<td class='heading'>Total lines</td>
<td class='heading'>Lines of code</td>
<td class='heading'>Total coverage</td>
<td class='heading'>Code coverage</td>
</tr>
</thead>
<tbody>
<tr class='light'>
<td>
<a href='~s'>~s</a>
</td>
<td class='value'>
<tt>??</tt>
</td>
<td class='value'>
<tt>??</tt>
</td>
<td class='value'>
<tt>??</tt>
</td>
<td>
<table cellspacing='0' cellpadding='0' align='right'>
<tr>
<td><tt>~p%</tt> </td><td>
<table cellspacing='0' class='percent_graph' cellpadding='0' width='100'>
<tr><td class='covered' width='~p' /><td class='uncovered' width='~p' /></tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table><pre>", [Module, etap:datetime({date(), time()}), atom_to_list(Module) ++ "_report.html", Module, CovPer, CovPer, UnCovPer]).
%% @private
footer() ->
"</pre><hr /><p>Generated using <a href='http://github.com/ngerakines/etap'>etap 0.3.4</a>.</p>
</body>
</html>
".
|