Skip to content
Snippets Groups Projects
Commit d2d53a04 authored by Arsenij Solovjev's avatar Arsenij Solovjev
Browse files

[INTERNAL] add specification/unit tests

Adds a specification that acts as a unit tests
parent abebc600
Branches
No related tags found
No related merge requests found
......@@ -14,7 +14,9 @@ import com.typesafe.scalalogging.slf4j._
class GitRepo(repoLocation: String = ".git/") {
private val repo = (new FileRepositoryBuilder).setGitDir(new File(repoLocation)).build
lazy val lastCommit = new Commit("HEAD")
private lazy val _lastCommit = new Commit("HEAD")
def lastCommit:Commit = _lastCommit
class Commit(newCommit: String, oldCommit: String) {
......@@ -30,15 +32,15 @@ class GitRepo(repoLocation: String = ".git/") {
setShowNameAndStatusOnly(true).
call
lazy val affectedFiles = (nameAndStatusOnly.filter(contributionTypes contains _.getChangeType) map
def affectedFiles = (nameAndStatusOnly.filter(contributionTypes contains _.getChangeType) map
(_.getPath(Side.NEW))).
toSet
private val commit = getCommit(repo, resolve(newCommit))
lazy val isMerge = commit.getParentCount > 1
def commit = getCommit(repo, resolve(newCommit))
def isMerge = commit.getParentCount > 1
private val pathRoot = (x: String) => if(x.contains("/")) x.substring(0, x.indexOf("/")) else ""
lazy val affectedProjects = affectedFiles map pathRoot
private lazy val getProjectFolder = (x: String) => if(x.contains("/")) x.substring(0, x.indexOf("/")) else ""
def affectedProjects = affectedFiles map getProjectFolder
}
private def resolve = (ref: String) => repo.resolve(ref).name
......
......@@ -27,3 +27,12 @@ libraryDependencies += "org.eclipse.jgit" % "org.eclipse.jgit" % "3.3.2.20140417
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"
libraryDependencies += "org.agse" % "gitclient_2.10" % "0.0.1"
libraryDependencies += "com.typesafe" % "config" % "1.2.1"
libraryDependencies += "org.specs2" % "specs2_2.10" % "2.4.16"
resolvers ++= Seq(
"Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
"Typesafe" at "http://repo.typesafe.com/typesafe/releases/"
)
......@@ -11,19 +11,17 @@ import scala.xml._
* https://github.com/SonarCommunity/sonar-issues-report/blob/1b38791/src/main/resources/org/sonar/issuesreport/printer/html/issuesreport.ftl
*/
object IssuesParser extends LazyLogging {
class IssuesParser(repo: GitRepo) extends LazyLogging {
// this particular parser can parse "tag-soup" document, which old html is
val xml = XML.withSAXParser(new org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl().newSAXParser())
val repo = new GitRepo((XML.load("conf.xml") \ "gitRepoLocation").text)
val projects = repo.lastCommit.affectedProjects
def projects = repo.lastCommit.affectedProjects
logger.debug(s"Projects affected by last commit:\n${projects.show}")
val changedFiles = repo.lastCommit.affectedFiles
def changedFiles = repo.lastCommit.affectedFiles
logger.debug(s"Files affected by last commit:\n${changedFiles.show}")
val isMerge = repo.lastCommit.isMerge
def isMerge = repo.lastCommit.isMerge
logger.debug(s"Last commit is a merge commit? $isMerge")
def parse(pathToReportsDir: String, changeId: String): String =
......@@ -57,12 +55,14 @@ object IssuesParser extends LazyLogging {
val comments = merged(resourceReports.get map commentsPerResource)
if (comments == JNothing)
("message" -> "Congratulations, QA found no issues whatsoever!")
("message" -> s"Congratulations, QA found no issues whatsoever!")
else
("message" -> "Please review your QA analysis results") ~
("message" -> s"Please review your QA analysis results") ~
("comments" -> comments)
}
val forProject = (project: String) => "for project ${project}"
private def commentsPerResource(resourceReport: Node): JValue = {
val sources = (resourceReport \\ "table") find
{ x => (x \ "@class").text == "sources" }
......
package org.agse.sonarreview
import org.rogach.scallop._
import scala.xml._
import org.agse.gitclient.{ GitRepo }
import com.typesafe.config._
object sonarReview {
......@@ -49,11 +51,14 @@ object sonarReview {
val conf = XML.load("conf.xml")
val pathToIssuesReport = (conf \ "issuesReportDir").text
val issuesParser = new IssuesParser(new GitRepo((XML.load("conf.xml") \ "gitRepoLocation").text))
val comments = P.project.get match {
case Some(project) =>
IssuesParser.parse(pathToIssuesReport, P.changeNumber.apply, project)
issuesParser.parse(pathToIssuesReport, P.changeNumber.apply, project)
case None =>
IssuesParser.parse(pathToIssuesReport, P.changeNumber.apply)
issuesParser.parse(pathToIssuesReport, P.changeNumber.apply)
}
GerritRestClient.setReview(P.changeNumber.apply, P.patchsetNumber.apply, comments)
......
package org.agse.sonarreview
import org.agse.gitclient.GitRepo
import net.liftweb.json._
import org.specs2.mutable._
import org.specs2.mock.Mockito
object ParserTest extends Specification with Mockito {
sequential
val affectedFile = "de.fu_berlin.inf.dpp.core/src/de/fu_berlin/inf/dpp/ClassThatBreaksACoupleOfSonarRules.java"
val affectedProject = "de.fu_berlin.inf.dpp.core"
val mockCommit = mock[mockRepo.Commit]
mockCommit.affectedProjects returns
Set(affectedProject)
mockCommit.affectedFiles returns
Set() thenReturns
Set() thenReturns
Set(affectedFile)
mockCommit.isMerge returns
true thenReturns
true thenReturns false
val mockRepo = mock[GitRepo]
mockRepo.lastCommit returns (mockCommit)
val pathToReports = "src/test/resources/"
val changeId = "sampleReport"
val project = "de.fu_berlin.inf.dpp.core"
val issuesParser = new IssuesParser(mockRepo)
"IssuesParser" should {
"not analyze merge commits" in {
val review = issuesParser.parse(pathToReports, changeId, project)
compact(render(parse(review) \ "message")) mustEqual "\"Nothing to analyze, since this is a merge commit.\""
}
"produce a confirmatory review message when no issues are found" in {
val review = issuesParser.parse(pathToReports, changeId, project)
compact(render(parse(review) \ "message")) mustEqual "\"Congratulations, QA found no issues whatsoever!\""
}
"produce a review message that prompts to review the issues found" in {
val review = issuesParser.parse(pathToReports, changeId, project)
compact(render(parse(review) \ "message")) mustEqual "\"Please review your QA analysis results\""
}
"produce a comment for each line with an issue" in {
val numberOfIssues = 12
val review = issuesParser.parse(pathToReports, changeId, project)
(parse(review) \\ "line").children.length mustEqual numberOfIssues
((parse(review) \\ "message").children.length - 1) mustEqual numberOfIssues
}
"produce comments when issues are found" in {
val lengthOfReview = 1942
val review = issuesParser.parse(pathToReports, changeId, project)
compact(render(parse(review))).length mustEqual lengthOfReview
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment