This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
JUnit report sensor #143
Merged
Merged
JUnit report sensor #143
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
40f6c3b
WIP Unit tests sensor.
mwz 00f8a1e
Tweak debug messages.
mwz 657983a
Save test metrics.
mwz f019abe
Renamed the sensor; improved logging.
mwz a21ffd5
Update log tests.
mwz 6a57a7a
Address some PR comments.
mwz ce6ecf8
Minor refactoring of file resolution.
mwz 06964ad
Improved logging.
mwz 34dfb6b
Improve logging.
mwz bf3686c
Update logic for filtering junit report files.
mwz ca4a468
Refactor utils.
mwz 7c5e8cd
Refactor utils.
mwz 51aa893
Add config syntax tests.
mwz 6644a75
Add some sensor tests.
mwz c8d9884
Add file system syntax tests.
mwz 4903118
Add sensor context syntax tests.
mwz 79634e0
Add a syntax package object.
mwz 76ba759
Refactor syntax into implicit classes.
mwz dd0446f
Refactor option conversions.
mwz 08cf5b6
Add a new file system test.
mwz 1ec5cc5
More sensor tests.
mwz 50e6a5d
Report parser tests.
mwz 8d4f1c8
Remove a trailing comma.
mwz 3a5f129
Change logging level.
mwz bd2e976
Add sonar.tests property to the example set projects.
mwz e58775f
Make tweaks to filtering of junit files.
mwz 5c4d653
Make parsing junit reports safer.
mwz 4607cef
Update example mvn projects.
mwz 036d17e
Update example grade projects.
mwz 2df6b1c
Use getStringArray to get a list of paths from the config.
mwz b1411d7
Drop the ’sonar.junit.disable’ property (the sensor isn’t executed wi…
mwz e6c9f0f
Rename defaultPath to DefaultPaths.
mwz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,15 +28,17 @@ import org.sonar.api.batch.fs.{FileSystem, InputFile} | |
import org.sonar.api.batch.sensor.{Sensor, SensorContext, SensorDescriptor} | ||
import org.sonar.api.config.Configuration | ||
import org.sonar.api.measures.CoreMetrics | ||
import org.sonar.api.scan.filesystem.PathResolver | ||
|
||
import scala.util.Try | ||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Scala JUnit sensor. | ||
* Parses JUnit XML reports and saves test metrics. | ||
*/ | ||
final class JUnitSensor( | ||
untTestsReportParser: JUnitReportParserAPI, | ||
config: Configuration, | ||
fs: FileSystem, | ||
pathResolver: PathResolver | ||
fs: FileSystem // TODO: Is the injected fileSystem different from context.fileSystem? | ||
) extends Sensor { | ||
import JUnitSensor._ // scalastyle:ignore org.scalastyle.scalariform.ImportGroupingChecker | ||
|
||
|
@@ -46,39 +48,52 @@ final class JUnitSensor( | |
descriptor | ||
.name(SensorName) | ||
.onlyOnLanguage(Scala.LanguageKey) | ||
.onlyOnFileType(InputFile.Type.TEST) | ||
// TODO: Add a flag to disable the sensor. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍 |
||
} | ||
|
||
override def execute(context: SensorContext): Unit = { | ||
log.info("Initializing the Scala JUnit sensor.") | ||
|
||
// Get the test paths. | ||
val tests: List[Path] = fromConfig(config, TestsPropertyKey, DefaultTests) | ||
log.debug(s"The source prefixes are: ${tests.mkString("[", ",", "]")}.") | ||
|
||
// Get the junit report paths. | ||
val reports: List[Path] = fromConfig(config, ReportsPropertyKey, DefaultReportPaths) | ||
log.debug(s"The JUnit report paths are: ${reports.mkString("[", ",", "]")}.") | ||
|
||
val inputFiles = context.fileSystem | ||
.inputFiles( | ||
context.fileSystem.predicates.and( | ||
context.fileSystem.predicates.hasLanguage(Scala.LanguageKey), | ||
context.fileSystem.predicates.hasType(InputFile.Type.TEST) | ||
// Get test input files | ||
val inputFiles: Iterable[InputFile] = | ||
context.fileSystem | ||
.inputFiles( | ||
context.fileSystem.predicates.and( | ||
context.fileSystem.predicates.hasLanguage(Scala.LanguageKey), | ||
context.fileSystem.predicates.hasType(InputFile.Type.TEST) | ||
) | ||
) | ||
) | ||
.asScala | ||
|
||
log.debug("Input test files:") | ||
inputFiles.forEach(f => log.debug(f.toString)) | ||
if (inputFiles.nonEmpty) | ||
log.debug(s"Input test files: \n${inputFiles.mkString(", ")}") | ||
else | ||
log.warn(s"No test files found for module ${context.module.key}.") | ||
|
||
val directories: List[File] = | ||
reports.flatMap(path => Try(pathResolver.relativeFile(fs.baseDir, path.toString)).toOption) | ||
// TODO: Is the injected fileSystem different from context.fileSystem? | ||
// Resolve test directories. | ||
val testDirectories: List[File] = resolve(fs, tests) | ||
if (testDirectories.isEmpty) | ||
log.error(s"The following test directories were not found: ${reports.mkString(", ")}.") | ||
|
||
if (directories.isEmpty) | ||
log.warn(s"JUnit test report path(s) not found for ${reports.mkString(", ")}.") | ||
else { | ||
val parsedReports: Map[InputFile, JUnitReport] = untTestsReportParser.parse(tests, directories) | ||
log.debug("Parsed reports:") | ||
log.debug(parsedReports.mkString(", ")) | ||
// Resolve JUnit report directories. | ||
val reportDirectories: List[File] = resolve(fs, reports) | ||
if (reportDirectories.isEmpty) | ||
log.error(s"The following JUnit test report path(s) were not found : ${reports.mkString(", ")}.") | ||
|
||
// Save test metrics for each file. | ||
save(context, parsedReports) | ||
} | ||
// Parse the reports. | ||
val parsedReports: Map[InputFile, JUnitReport] = untTestsReportParser.parse(tests, reportDirectories) | ||
|
||
// Save test metrics for each file. | ||
save(context, parsedReports) | ||
} | ||
|
||
/** | ||
|
@@ -102,6 +117,11 @@ final class JUnitSensor( | |
(report.time * 1000).longValue | ||
) | ||
} | ||
|
||
if (reports.nonEmpty) | ||
log.debug(s"Parsed reports:\n${reports.mkString(", ")}") | ||
else | ||
log.info("No test metrics were saved by this sensor.") | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
package com.mwz.sonar.scala | ||
package util | ||
|
||
import java.io.File | ||
import java.nio.file.{Path, Paths} | ||
import java.util.Optional | ||
|
||
|
@@ -27,7 +28,8 @@ import org.sonar.api.batch.measure.Metric | |
import org.sonar.api.batch.sensor.SensorContext | ||
import org.sonar.api.config.Configuration | ||
|
||
import scala.language.implicitConversions | ||
import scala.language.{higherKinds, implicitConversions} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where/Why exactly do we need |
||
import scala.util.Try | ||
|
||
/** | ||
* Scala.Option <-> Java.Optional conversions. | ||
|
@@ -94,6 +96,14 @@ object PathUtils { | |
val currentWorkdirAbsolutePath = PathUtils.cwd | ||
currentWorkdirAbsolutePath.relativize(moduleAbsolutePath) | ||
} | ||
|
||
/** | ||
* Resolve a list of paths relative to the given file system. | ||
*/ | ||
def resolve(fs: FileSystem, toResolve: List[Path]): List[File] = | ||
toResolve.flatMap { path => | ||
Try(fs.resolvePath(path.toString)).toOption | ||
} | ||
} | ||
|
||
object MetricUtils { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this sensor only works for test files.
Thus, you may add
.onlyOnFileType(InputFile.Type.TEST)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes definitely