-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Import coverage reports from utPLSQL (#156)
- Loading branch information
Showing
10 changed files
with
263 additions
and
5 deletions.
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
57 changes: 57 additions & 0 deletions
57
sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageReport.kt
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Z PL/SQL Analyzer | ||
* Copyright (C) 2015-2024 Felipe Zorzo | ||
* mailto:felipe AT felipezorzo DOT com DOT br | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plsqlopen.utplsql | ||
|
||
import org.simpleframework.xml.Attribute | ||
import org.simpleframework.xml.ElementList | ||
import org.simpleframework.xml.Root | ||
|
||
@Root(name = "coverage") | ||
data class Coverage @JvmOverloads constructor( | ||
@field:Attribute(name = "version") | ||
var version: Int = 0, | ||
|
||
@field:ElementList(name = "file", inline = true, required = false) | ||
var files: List<CoveredFile>? = null | ||
) | ||
|
||
@Root(name = "file") | ||
data class CoveredFile @JvmOverloads constructor( | ||
@field:Attribute(name = "path") | ||
var path: String = "", | ||
|
||
@field:ElementList(name = "lineToCover", inline = true, required = false) | ||
var linesToCover: List<LineToCover>? = null | ||
) | ||
|
||
@Root(name = "lineToCover") | ||
data class LineToCover @JvmOverloads constructor( | ||
@field:Attribute(name = "lineNumber") | ||
var lineNumber: Int = 0, | ||
|
||
@field:Attribute(name = "covered") | ||
var covered: Boolean = false, | ||
|
||
@field:Attribute(name = "branchesToCover", required = false) | ||
var branchesToCover: Int? = null, | ||
|
||
@field:Attribute(name = "coveredBranches", required = false) | ||
var coveredBranches: Int? = null | ||
) |
85 changes: 85 additions & 0 deletions
85
sonar-zpa-plugin/src/main/kotlin/org/sonar/plsqlopen/utplsql/CoverageResultImporter.kt
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Z PL/SQL Analyzer | ||
* Copyright (C) 2015-2024 Felipe Zorzo | ||
* mailto:felipe AT felipezorzo DOT com DOT br | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plsqlopen.utplsql | ||
|
||
import org.simpleframework.xml.core.Persister | ||
import org.sonar.api.batch.sensor.SensorContext | ||
import org.sonar.api.notifications.AnalysisWarnings | ||
import org.sonar.plsqlopen.symbols.ObjectLocator | ||
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar | ||
import java.io.File | ||
|
||
|
||
class CoverageResultImporter(private val objectLocator: ObjectLocator, | ||
analysisWarnings: AnalysisWarnings) : AbstractReportImporter(analysisWarnings) { | ||
|
||
override val reportType = "coverage" | ||
override val reportKey = UtPlSqlSensor.COVERAGE_REPORT_PATH_KEY | ||
|
||
override fun processReport(context: SensorContext, report: File) { | ||
val serializer = Persister() | ||
val coverage = serializer.read(Coverage::class.java, report) | ||
|
||
coverage.files?.forEach { file -> | ||
val filePath = file.path | ||
var inputFile = context.fileSystem() | ||
.inputFile(context.fileSystem().predicates().hasPath(filePath)) | ||
|
||
if (inputFile == null) { | ||
val objectType = when (filePath.substringBeforeLast(' ')) { | ||
"package body" -> PlSqlGrammar.CREATE_PACKAGE_BODY | ||
"procedure" -> PlSqlGrammar.CREATE_PROCEDURE | ||
"function" -> PlSqlGrammar.CREATE_FUNCTION | ||
"trigger" -> PlSqlGrammar.CREATE_TRIGGER | ||
"type body" -> PlSqlGrammar.CREATE_TYPE_BODY | ||
else -> error("Unknown object type for file \"$filePath\"") | ||
} | ||
val objectName = filePath.substringAfterLast('.') | ||
|
||
val mappedFile = objectLocator.findMainObject(objectName, objectType) | ||
|
||
inputFile = mappedFile?.inputFile | ||
} | ||
|
||
if (inputFile != null) { | ||
val newCoverage = context.newCoverage().onFile(inputFile) | ||
|
||
file.linesToCover?.forEach { line -> | ||
newCoverage.lineHits(line.lineNumber, if (line.covered) 1 else 0) | ||
|
||
val branchesToCover = line.branchesToCover | ||
val coveredBranches = line.coveredBranches ?: 0 | ||
if (branchesToCover != null) { | ||
check(coveredBranches <= branchesToCover) { | ||
"\"coveredBranches\" should not be greater than \"branchesToCover\" on line " + | ||
"${line.lineNumber} for file \"$filePath\"" | ||
} | ||
|
||
newCoverage.conditions(line.lineNumber, branchesToCover, coveredBranches) | ||
} | ||
} | ||
|
||
newCoverage.save() | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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
18 changes: 18 additions & 0 deletions
18
sonar-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/award_bonus.sql
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
create or replace procedure award_bonus(emp_id number, sales_amt number) as | ||
commission real; | ||
comm_missing exception; | ||
begin | ||
select commission_pct | ||
into commission | ||
from employees_test | ||
where employee_id = emp_id; | ||
|
||
if commission is null then | ||
raise comm_missing; | ||
else | ||
update employees_test | ||
set salary = nvl(salary, 0) + sales_amt * commission | ||
where employee_id = emp_id; | ||
end if; | ||
end; | ||
/ |
9 changes: 9 additions & 0 deletions
9
sonar-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/betwnstr.sql
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
create or replace function betwnstr(a_string varchar2, a_start_pos integer, a_end_pos integer) return varchar2 is | ||
l_start_pos pls_integer := a_start_pos; | ||
begin | ||
if l_start_pos = 0 then | ||
l_start_pos := 1; | ||
end if; | ||
return substr(a_string, l_start_pos, a_end_pos - l_start_pos + 1); | ||
end; | ||
/ |
9 changes: 9 additions & 0 deletions
9
...-zpa-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/coverage-report-with-paths.xml
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<coverage version="1"> | ||
<file path="award_bonus.sql"> | ||
<lineToCover lineNumber="5" covered="true"/> | ||
<lineToCover lineNumber="10" covered="true" branchesToCover="2" coveredBranches="2"/> | ||
<lineToCover lineNumber="11" covered="true"/> | ||
<lineToCover lineNumber="13" covered="true"/> | ||
</file> | ||
</coverage> |
9 changes: 9 additions & 0 deletions
9
...a-plugin/src/test/resources/org/sonar/plsqlopen/utplsql/coverage-report-without-paths.xml
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<coverage version="1"> | ||
<file path="function ut3_demo.betwnstr"> | ||
<lineToCover lineNumber="2" covered="true"/> | ||
<lineToCover lineNumber="4" covered="true"/> | ||
<lineToCover lineNumber="5" covered="true"/> | ||
<lineToCover lineNumber="7" covered="true"/> | ||
</file> | ||
</coverage> |