1#!/usr/bin/env bash
2
3coverdata_dir=@abs_builddir@/logs
4coverdata_file=${coverdata_dir}/@PACKAGE_NAME@.coverdata
5coverdata_app=@PACKAGE_NAME@
6analyze_coverdata=@abs_builddir@/analyze_coverdata.escript
7
8color_0="ff2200"
9color_5="ff3800"
10color_10="ff4e00"
11color_15="ff6400"
12color_20="ff7a00"
13color_25="ff9000"
14color_30="ffa500"
15color_35="ffbb00"
16color_40="ffd100"
17color_45="ffe700"
18color_50="fdfc00"
19color_55="ecff00"
20color_60="d6ff00"
21color_65="c0ff00"
22color_70="aaff00"
23color_75="94ff00"
24color_80="7eff00"
25color_85="69ff00"
26color_90="53ff00"
27color_95="3dff00"
28color_100="27ff00"
29
30output_analyze_coverdata () {
31    pretty_name=$(echo $coverdata_app | tr "_" " ")
32
33    # Generate covered modules data. We ignore stdout because cover
34    # writes some useless logs.
35    $analyze_coverdata $coverdata_file > /dev/null
36    covered_mods=${coverdata_app}_covered_mods
37
38    # Prepare index block.
39    index=${coverdata_dir}/cover.html
40    timestamp=$(date +'%Y-%m-%d at %H:%M:%S')
41    cat << EOF > "$index"
42<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
43  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
44<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
45<head>
46<title>Testsuite coverage - ${pretty_name}</title>
47<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
48<style type="text/css">
49body {
50  color: #000;
51  background-color: #fff;
52  font-family: sans-serif;
53}
54h1 {
55  margin: 0px;
56}
57#timestamp {
58  margin-top: 0px;
59  margin-bottom: 3em;
60  color: #777;
61  font-style: italic;
62  border-bottom: solid 1px #000;
63}
64#tests_col {
65  padding-left: 30px;
66}
67.testsuite {
68  float: left;
69  margin-right: 30px;
70  margin-bottom: 30px;
71  padding-left: 25px;
72}
73.testsuite table {
74  border: 1px solid #999;
75  border-collapse: collapse;
76  background-color: #f9f9f9;
77}
78.testsuite tr:hover {
79  background-color: #ebebeb;
80}
81.testsuite th, .testsuite td {
82  padding: 4px;
83}
84.testsuite th {
85  border-bottom: 1px solid #999;
86  border-top: 1px solid #999;
87  background-color: #ebebeb;
88}
89</style>
90</head>
91<body>
92<h1>Testsuite coverage - <span style="text-transform: capitalize;">${pretty_name}</span></h1>
93<p id="timestamp">Date: ${timestamp}</p>
94
95<div class="testsuite" id="${coverdata_app}">
96<h2><span style="text-transform: capitalize;">${pretty_name}</span> modules:</h2>
97<table>
98<tr>
99<th style="width: 200px; text-align: left;">Module</th>
100<th style="width: 100px; text-align: right;">Coverage (%)</th>
101<th style="width: 100px; text-align: right;">Coveraged (Lines)</th>
102<th style="width: 100px; text-align: right;" colspan="2">Not coveraged (Lines)</th>
103</tr>
104EOF
105
106    # For each module of this test:
107    #     o  Write its source code with cover results.
108    #     o  Update index block.
109    total_covered=0
110    total_not_covered=0
111    for module in $(cat ${coverdata_dir}/${covered_mods} | sort); do
112        in=${coverdata_dir}/${coverdata_app}_${module}.out
113        cvd=${coverdata_dir}/${coverdata_app}_${module}.covered
114        not_cvd=${coverdata_dir}/${coverdata_app}_${module}.not_covered
115        pct=${coverdata_dir}/${coverdata_app}_${module}.percent
116        out=${coverdata_dir}/${module}.COVER.html
117        link=${module}.COVER.html
118
119        covered=$(cat $cvd)
120        not_covered=$(cat $not_cvd)
121        percent=$(cat $pct)
122
123        color=$(echo $percent | sed -e 's/\..*$//')
124        if [ $((color % 5)) -gt 0 ]; then
125            color=$((color + (5 - color % 5)))
126        fi
127        color=$(eval echo \${color_$color})
128
129        headline=$(head -n 1 "$in")
130        timestamp=${headline#*COVER }
131
132        # Update source file to emphasize cover results.
133        cat << EOF > "$out"
134<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
135  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
136<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
137<head>
138<title>Testsuite coverage / ${pretty_name} / ${module}</title>
139<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
140<style type="text/css">
141body {
142  color: #000;
143  background-color: #fff;
144  font-family: sans-serif;
145}
146h1 {
147  margin: 0px;
148}
149#percent {
150  position: absolute;
151  top: 10px;
152  right: 10px;
153  margin: 0px;
154  font-size: 28px;
155  font-weight: bold;
156  padding-right: 8px;
157  border-right: solid 5px #${color};
158}
159#timestamp {
160  margin-top: 0px;
161  madding-bottom: 0px;
162  color: #777;
163  font-style: italic;
164  border-bottom: solid 1px #000;
165}
166#back {
167  margin-top: 0px;
168  margin-bottom: 3em;
169  font-size: 10px;
170  text-align: right;
171}
172.cvsid, .func {
173  font-weight: bold;
174}
175.cvsid {
176  color: #461b7e;
177}
178.cov {
179  color: #007a00;
180}
181.notcov {
182  color: #f00;
183}
184.comment {
185  color: #777;
186}
187</style>
188</head>
189<body>
190<h1>Testsuite coverage: <span style="text-transform: capitalize;">${pretty_name}</span> / <span style="color: #9fc550;">${module}</span></h1>
191<p id="percent">${percent} %</p>
192<p id="timestamp">Date: ${timestamp}</p>
193<p id="back">[<a href="cover.html">index</a>]</p>
194<pre>
195EOF
196
197        line_nb=$(wc -l "$in")
198        tail -n $((${line_nb% *} - 4)) "$in" | sed \
199         -e 's,<,\&lt;,g' -e 's,>,\&gt;,g' \
200         -e 's,^\([[:space:]]*0\.\.|.*\),<span class="notcov">\1</span>,' \
201         -e 's,^\([[:space:]]*[1-9][0-9]*\.\.|.*\),<span class="cov">\1</span>,' \
202         -e 's,^\([[:space:]]*|[[:space:]][[:space:]]*%+ \$Id.*\),<span class="cvsid">\1</span>,' \
203         -e 's,^\([[:space:]]*|[[:space:]][[:space:]]*%.*\),<span class="comment">\1</span>,' \
204         -e 's,^\([[:space:]]*|[[:space:]]\{2\}[[:alpha:]][a-zA-Z0-9_]*\)\(.*\),<span class="func">\1</span>\2,' \
205         >> "$out"
206
207        cat << EOF >> "$out"
208</pre>
209</body>
210EOF
211
212        # Update index block.
213        cat << EOF >> "$index"
214<tr>
215<td style="text-align: left;"><a href="${link}">${module}</a></td>
216<td style="text-align: right;">${percent} %</td>
217<td style="text-align: right;">${covered}</td>
218<td style="text-align: right;">${not_covered}</td>
219<td style="width: 5px; padding: 0px; background-color: #$color;"></td>
220</tr>
221EOF
222        # Update total results
223        total_covered=$((total_covered + covered))
224        total_not_covered=$((total_not_covered + not_covered))
225
226        # Remove file generated by $analyze_coverdata.
227        rm -f $in $cvd $not_cvd $pct
228    done
229
230    total_percent=$(echo "scale=1; ($total_covered * 100 / ( $total_covered + $total_not_covered))" | bc)
231
232    color=$(echo $total_percent | sed -e 's/\..*$//')
233    if [ $((color % 5)) -gt 0 ]; then
234        color=$((color + (5 - color % 5)))
235    fi
236    color=$(eval echo \${color_$color})
237    # End index block.
238    cat << EOF >> "$index"
239<tr>
240<th style="text-align: left;">Total</th>
241<th style="text-align: right;">${total_percent} %</th>
242<th style="text-align: right;">${total_covered}</th>
243<th style="text-align: right;">${total_not_covered}</th>
244<th style="width: 5px; padding: 0px; background-color: #$color;"></th>
245</tr>
246</table>
247</div>
248
249</body>
250</html>
251EOF
252
253    rm -f $covered_mods
254}
255
256output_analyze_coverdata
257