Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SonarQube Support in ReportGenerator #194

Closed
diegoibellini opened this issue Jan 14, 2019 · 10 comments
Closed

SonarQube Support in ReportGenerator #194

diegoibellini opened this issue Jan 14, 2019 · 10 comments

Comments

@diegoibellini
Copy link

Hi Daniel,

Your projects seems to be really interesting, I'm using Pester to generate some Code Coverage reports in Jacoco format but to import it to Sonar we need to reconvert it in the Generic Sonar Format.
Are you planning to add more output formats in the tools soon ?

Best!

Regards
DIB

@danielpalme
Copy link
Owner

Doesn't look very difficult to implement (see https://docs.sonarqube.org/latest/analysis/generic-test/)
Will give it a try within the next days.

@diegoibellini
Copy link
Author

Thanks so much for your reply, I'll waiting for news!. I was started a parser to convert my current Jacoco output to the generic format of Sonar, but to be completly honest, I'm not sure that we're parsing the output of the file fine.

This is the code that we develop

param (
    [string]$in = $( Read-Host "Input pester output file." ),
    [string]$out = $( Read-Host "Input output sonarqube generic test path." ),
    [switch]$force = $false
 )

$xml = [xml](Get-Content $in)
$covered = ($Xml.report.counter | Where-Object Type -eq "INSTRUCTION" | Select-Object -Property covered).covered
$missed = ($Xml.report.counter | Where-Object Type -eq "INSTRUCTION" | Select-Object -Property missed).missed
$outputCoverage = "C:\Users\diego.bellini\Downloads\Code\PowerShell\CodigoSonarqube\src\Get-Pester.ps1"

# Set the File Name
$filePath = $out+"/TestCoverage.GF.xml"
 
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath, $Null)
 
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
 
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
 
# Write Root Element
$xmlWriter.WriteStartElement("coverage")
$xmlWriter.WriteAttributeString("version", "1")
 
# Write the Document
$xmlWriter.WriteStartElement("file")
$XmlWriter.WriteAttributeString("path", "$outputCoverage")
$count=0
while ($count -lt $covered) {
    $xmlWriter.WriteStartElement("lineToCover")
    $XmlWriter.WriteAttributeString("lineNumber", $count+1) 
    $XmlWriter.WriteAttributeString("covered", "true") 
    $XmlWriter.WriteEndElement()
    $count++
    

}
$count
# Count total based on last count.
$totalCount=$count+$missed

while ($count -lt $totalCount){
    $xmlWriter.WriteStartElement("lineToCover")
    $XmlWriter.WriteAttributeString("lineNumber", $count+1) 
    $XmlWriter.WriteAttributeString("covered", "false") 
    $XmlWriter.WriteEndElement()
    $count++
}
$count

$XmlWriter.WriteEndElement()


# End the XML Document
$xmlWriter.WriteEndDocument;
 
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()

But if you look at it, we're just parsing the "INSTRUCTION" of the output, and we really don't know if we need to do the same with the other parameters that the Jacoco are giving us like (LINE, METHOD & CLASS) or if we're parsing the file correctly.

So if you can help me with this we will be completely grateful with you.

Thanks so much!

Best
DIB

@danielpalme
Copy link
Owner

danielpalme commented Jan 14, 2019

Please give this new (pre-) release a try:
https://www.nuget.org/packages/ReportGenerator/4.0.6

Use the following arguments:
-reports:mycoveragefile.xml -targetdir:mytargetdir -reporttypes:SonarQube

Please let me know if this works for you.

@diegoibellini
Copy link
Author

Before at all, thanks so much for your fast reply again! I'm trying to use the tool but I think that the tool is not working with my report.

This is the Jacoco file that I'm generating with my pester test

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd">
    <report name="Pester (01/14/2019 10:01:59)">
        <sessioninfo id="this" start="1547460118853" dump="1547460119566" />
            <counter type="INSTRUCTION" missed="17" covered="12" />
            <counter type="LINE" missed="17" covered="10" />
            <counter type="METHOD" missed="3" covered="1" />
            <counter type="CLASS" missed="3" covered="1" />
    </report>

This is the line that I'm executing with your tool

C:\Users\diego.bellini\.nuget\packages\reportgenerator\4.0.6-rc2\tools\net47\ReportGenerator.exe "-reports:C:\Users\diego.bellini\Downloads\Code\PowerShell\CodigoSonarqube\SonarProject\artefacts\TestsCoverage.Jacoco.xml" "-targetdir:C:\Users\diego.bellini\Downloads\Code\PowerShell\CodigoSonarqube\SonarProject\artefacts\" -reporttypes:SonarQube

And this is the file that the tool is generating, that apparently is empty

<?xml version="1.0" encoding="utf-8"?>
<coverage version="1" />

Please let me know if you think that I'm doing something wrong or if you thing that I can give you some more input to understand better what is happening with the tool.

Many thanks!

Regards
DIB

@danielpalme
Copy link
Owner

Your command line arguments look fine.
But your JaCoCo file does not contain any coverage information.
The <report> element should contain at least one <package> element.

Here's an example file:
https://github.com/danielpalme/ReportGenerator/blob/master/src/Testprojects/Java/Reports/JaCoCo0.8.3.xml

@danielpalme
Copy link
Owner

danielpalme commented Jan 15, 2019

Perhaps you need to specify the -detailedcodecoverage parameter when executing Pester.
See: pester/Pester#920

Edit:
-detailedcodecoverage seems not to be required any more; see: pester/Pester#1120

So perhaps you are not using the latest version of Pester?!
The version containing pester/Pester#1120 was only released five days ago:
https://github.com/pester/Pester/releases/tag/4.5.0

@diegoibellini
Copy link
Author

Thanks so much for your update. I'm trying to parse my Jacoco outputs with the new pester version an apparently your parser are working fine. Let me do some more tests and I will send you a new message with the update soon!

@drdamour
Copy link

drdamour commented Sep 7, 2022

@danielpalme was excited to see this support, BUT the https://docs.sonarqube.org/latest/analysis/generic-test/ format needs 2 files one for coverage and 1 for test execution. Is there a way to gen the test execution file too with reportgenerator?

@danielpalme
Copy link
Owner

@drdamour
I'm sorry but the relevant information for the test execution file is not available in the coverage files.
So ReportGenerator is not able to generate this file.

@drdamour
Copy link

drdamour commented Sep 7, 2022

@danielpalme hm ok, i'm clearly outta my depth, somehow it works for cobertura files. but my prior any any future question would be uninformed. thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants