Skip to content

Commit 95e5824

Browse files
committed
Added reporters from scales-reporters module which will be removed
1 parent 4d66356 commit 95e5824

File tree

7 files changed

+531
-5
lines changed

7 files changed

+531
-5
lines changed

build.sbt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ pomIncludeRepository := {
2020

2121
javacOptions ++= Seq("-source", "1.6", "-target", "1.6")
2222

23-
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.10.3" % "provided"
23+
libraryDependencies ++= Seq(
24+
"org.scala-lang" % "scala-compiler" % "2.10.3" % "provided",
25+
"commons-io" % "commons-io" % "2.4"
26+
)
2427

2528
publishTo <<= version {
2629
(v: String) =>
2730
val nexus = "https://oss.sonatype.org/"
28-
if (v.trim.endsWith("SNAPSHOT"))
29-
Some("snapshots" at nexus + "content/repositories/snapshots")
30-
else
31-
Some("releases" at nexus + "service/local/staging/deploy/maven2")
31+
if (v.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "content/repositories/snapshots")
32+
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
3233
}
3334

3435
pomExtra := {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package scales.reporters
2+
3+
import scales.{MeasuredMethod, MeasuredClass, MeasuredPackage, Coverage}
4+
import scala.xml.Node
5+
import java.io.File
6+
import org.apache.commons.io.FileUtils
7+
8+
/** @author Stephen Samuel */
9+
object CoberturaXmlWriter extends CoverageWriter {
10+
11+
def write(coverage: Coverage, dir: File): Unit = {
12+
FileUtils.write(new File(dir.getAbsolutePath + "/cobertura.xml"), xml(coverage).toString())
13+
}
14+
15+
def method(method: MeasuredMethod): Node = {
16+
<method name={method.name}
17+
signature="()V"
18+
line-rate={method.statementCoverage.toString}
19+
branch-rate={method.branchCoverageFormatted}>
20+
<lines>
21+
{method.statements.map(stmt =>
22+
<line
23+
number={stmt.line.toString}
24+
hits={stmt.count.toString}
25+
branch="false"/>
26+
)}
27+
</lines>
28+
</method>
29+
}
30+
31+
def klass(klass: MeasuredClass): Node = {
32+
<class name={klass.name}
33+
filename={klass.source}
34+
line-rate={klass.statementCoverage.toString}
35+
branch-rate={klass.branchCoverageFormatted}
36+
complexity="0">
37+
<methods>
38+
{klass.methods.map(method)}
39+
</methods>
40+
</class>
41+
}
42+
43+
def pack(pack: MeasuredPackage): Node = {
44+
<package name={pack.name}
45+
line-rate={pack.statementCoverage.toString}
46+
branch-rate="0"
47+
complexity="0">
48+
<classes>
49+
{pack.classes.map(klass)}
50+
</classes>
51+
</package>
52+
}
53+
54+
def xml(coverage: Coverage): Node = {
55+
<coverage line-rate={coverage.statementCoverage.toString}
56+
branch-rate={coverage.branchCoverageFormatted}
57+
version="1.0"
58+
timestamp={System.currentTimeMillis.toString}>
59+
<sources>
60+
<source>/src/main/scala</source>
61+
</sources>
62+
<packages>
63+
{coverage.packages.map(pack)}
64+
</packages>
65+
</coverage>
66+
}
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scales.reporters
2+
3+
import scales.Coverage
4+
import java.io.File
5+
6+
/** @author Stephen Samuel */
7+
trait CoverageWriter {
8+
def write(coverage: Coverage, dir: File)
9+
}
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
package scales.reporters
2+
3+
import scales._
4+
import scala.xml.Node
5+
import scales.MeasuredFile
6+
import java.util.Date
7+
import java.io.File
8+
import org.apache.commons.io.{FilenameUtils, FileUtils}
9+
10+
/** @author Stephen Samuel */
11+
object ScalesHtmlWriter extends CoverageWriter {
12+
13+
def write(coverage: Coverage, dir: File): Unit = {
14+
val indexFile = new File(dir.getAbsolutePath + "/index.html")
15+
val packageFile = new File(dir.getAbsolutePath + "/packages.html")
16+
val overviewFile = new File(dir.getAbsolutePath + "/overview.html")
17+
18+
FileUtils.copyInputStreamToFile(getClass.getResourceAsStream("/org/scalescc/reporters/index.html"), indexFile)
19+
FileUtils.write(packageFile, packages(coverage).toString())
20+
FileUtils.write(overviewFile, overview(coverage).toString())
21+
22+
coverage.packages.foreach(write(_, dir))
23+
}
24+
25+
def write(pack: MeasuredPackage, dir: File) {
26+
val file = new File(dir.getAbsolutePath + "/" + pack.name.replace('.', '/') + "/package.html")
27+
file.getParentFile.mkdirs()
28+
FileUtils.write(file, _package(pack).toString())
29+
pack.files.foreach(write(_, file.getParentFile))
30+
}
31+
32+
def write(mfile: MeasuredFile, dir: File) {
33+
val file = new File(dir.getAbsolutePath + "/" + FilenameUtils.getBaseName(mfile.source) + ".html")
34+
file.getParentFile.mkdirs()
35+
FileUtils.write(file, _file(mfile).toString())
36+
}
37+
38+
def _file(mfile: MeasuredFile): Node = {
39+
new SourceHighlighter().print(mfile)
40+
}
41+
42+
def _package(pack: MeasuredPackage): Node = {
43+
<html>
44+
<head>
45+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
46+
<title id='title'>Scales Code Coverage</title>
47+
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/>
48+
</head>
49+
<body>
50+
<table class="pure-table pure-table-bordered pure-table-striped" style="font-size:12px">
51+
<thead>
52+
<tr>
53+
<th>Class</th>
54+
<th>Source</th>
55+
<th>Lines</th>
56+
<th>Methods</th>
57+
<th>Statements</th>
58+
<th>Statements Invoked</th>
59+
<th>Statement Coverage</th>
60+
<th>Branches</th>
61+
<th>Branches Invoked</th>
62+
<th>Branch Coverage</th>
63+
</tr>
64+
</thead>
65+
<tbody>
66+
{pack.classes.map(_class)}
67+
</tbody>
68+
</table>
69+
</body>
70+
</html>
71+
}
72+
73+
def _class(klass: MeasuredClass): Node = {
74+
val filename = FilenameUtils.getBaseName(klass.source) + ".html"
75+
val simpleClassName = klass.name.split('.').last
76+
<tr>
77+
<td>
78+
<a href={filename}>
79+
{simpleClassName}
80+
</a>
81+
</td>
82+
<td>
83+
{klass.statements.headOption.map(_.source.split('/').last).getOrElse("")}
84+
</td>
85+
<td>
86+
{klass.loc.toString}
87+
</td>
88+
<td>
89+
{klass.methodCount.toString}
90+
</td>
91+
<td>
92+
{klass.statementCount.toString}
93+
</td>
94+
<td>
95+
{klass.invokedStatementCount.toString}
96+
</td>
97+
<td>
98+
{klass.statementCoverageFormatted}
99+
%
100+
</td>
101+
<td>
102+
{klass.branchCount.toString}
103+
</td>
104+
<td>
105+
{klass.invokedBranchesCount.toString}
106+
</td>
107+
<td>
108+
{klass.branchCoverageFormatted}
109+
%
110+
</td>
111+
</tr>
112+
}
113+
114+
def packages(coverage: Coverage): Node = {
115+
<html>
116+
<head>
117+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
118+
<title id='title'>Scales Code Coverage</title>
119+
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/>
120+
</head>
121+
<body>
122+
<table class="pure-table pure-table-bordered pure-table-striped" style="font-size: 12px">
123+
<tbody>
124+
<tr>
125+
<td>
126+
<a href="overview.html" target="mainFrame">
127+
All packages
128+
</a>{coverage.statementCoverageFormatted}
129+
%
130+
</td>
131+
</tr>{coverage.packages.map(arg =>
132+
<tr>
133+
<td>
134+
<a href={arg.name.replace('.', '/') + "/package.html"} target="mainFrame">
135+
{arg.name}
136+
</a>{arg.statementCoverageFormatted}
137+
%
138+
</td>
139+
</tr>
140+
)}
141+
</tbody>
142+
</table>
143+
</body>
144+
</html>
145+
}
146+
147+
def risks(coverage: Coverage, limit: Int) = {
148+
<table class="pure-table pure-table-bordered pure-table-striped" style="font-size: 12px">
149+
<caption>Top 20 Class Risks</caption>
150+
<thead>
151+
<tr>
152+
<th>Class</th>
153+
<th>Lines</th>
154+
<th>Methods</th>
155+
<th>Statements</th>
156+
<th>Statement Rate</th>
157+
<th>Branches</th>
158+
<th>Branch Rate</th>
159+
</tr>
160+
</thead>
161+
<tbody>
162+
{coverage.risks(limit).map(klass =>
163+
<tr>
164+
<td>
165+
{klass.simpleName}
166+
</td>
167+
<td>
168+
{klass.loc.toString}
169+
</td>
170+
<td>
171+
{klass.methodCount.toString}
172+
</td>
173+
<td>
174+
{klass.statementCount.toString}
175+
</td>
176+
<td>
177+
{klass.statementCoverageFormatted}
178+
%
179+
</td>
180+
<td>
181+
{klass.branchCount.toString}
182+
</td>
183+
<td>
184+
{klass.branchCoverageFormatted}
185+
%
186+
</td>
187+
</tr>)}
188+
</tbody>
189+
</table>
190+
}
191+
192+
def packages2(coverage: Coverage) = {
193+
val rows = coverage.packages.map(arg => {
194+
<tr>
195+
<td>
196+
{arg.name}
197+
</td>
198+
<td>
199+
{arg.invokedClasses.toString}
200+
/
201+
{arg.classCount}
202+
(
203+
{arg.classCoverage.toString}
204+
%)
205+
</td>
206+
<td>
207+
{arg.invokedStatements.toString()}
208+
/
209+
{arg.statementCount}
210+
(
211+
{arg.statementCoverageFormatted}
212+
%)
213+
</td>
214+
</tr>
215+
})
216+
<table>
217+
{rows}
218+
</table>
219+
}
220+
221+
def overview(coverage: Coverage): Node = {
222+
<html>
223+
<head>
224+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
225+
<title id='title'>Scales Code Coverage</title>
226+
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/>
227+
</head>
228+
<body>
229+
<div class="overview">
230+
{stats(coverage)}{risks(coverage, 20)}
231+
</div>
232+
</body>
233+
</html>
234+
}
235+
236+
def stats(coverage: Coverage): Node = {
237+
<table>
238+
<caption>Statistics generated at
239+
{new Date().toString}
240+
</caption>
241+
<tr>
242+
<td>Lines of code:</td>
243+
<td>
244+
{coverage.loc.toString}
245+
</td>
246+
<td>Statements:</td>
247+
<td>
248+
{coverage.statementCount}
249+
</td>
250+
<td>Clases per package:</td>
251+
<td>
252+
{coverage.avgClassesPerPackageFormatted}
253+
</td>
254+
<td>Methods per class:</td>
255+
<td>
256+
{coverage.avgMethodsPerClassFormatted}
257+
</td>
258+
</tr>
259+
<tr>
260+
<td>to be completed</td>
261+
<td>
262+
</td>
263+
<td>Packages:</td>
264+
<td>
265+
{coverage.classCount.toString}
266+
</td>
267+
<td>Classes:</td>
268+
<td>
269+
{coverage.packageCount.toString}
270+
</td>
271+
<td>Methods:</td>
272+
<td>
273+
{coverage.methodCount.toString}
274+
</td>
275+
</tr>
276+
</table>
277+
}
278+
279+
}
280+

0 commit comments

Comments
 (0)