-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathsonarqube.R
38 lines (29 loc) · 1.17 KB
/
sonarqube.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#' Create a SonarQube Generic XML file for test coverage according to
#' https://docs.sonarqube.org/latest/analysis/generic-test/
#' Based on cobertura.R
#'
#' This functionality requires the xml2 package be installed.
#' @param cov the coverage object returned from [package_coverage()]
#' @param filename the name of the SonarQube Generic XML file
#' @author Talkdesk Inc.
#' @export
to_sonarqube <- function(cov, filename = "sonarqube.xml"){
loadNamespace("xml2")
df <- tally_coverage(cov, by = "line")
d <- xml2::xml_new_document()
top <- xml2::xml_add_child(d, "coverage", version = "1")
files <- unique(df$filename)
for (f in files){
file <- xml2::xml_add_child(top, "file", path = paste(attr(cov, "package")$package, "/", as.character(f), sep=""))
for (fun_name in unique(na.omit(df[df$filename == f, "functions"]))) {
fun_lines <- which(df$functions == fun_name & df$filename == f)
for (i in fun_lines){
line <- df[i, ]
xml2::xml_add_child(file, "lineToCover", lineNumber = as.character(line$line),
covered = tolower(as.character(line$value>0)))
}
}
}
xml2::write_xml(d, file = filename)
invisible(d)
}