Skip to content

Commit 8dbac39

Browse files
committed
Allow lint results to be exported as checkstyle xml
1 parent 104f2d9 commit 8dbac39

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: lintr
22
Title: Static R Code Analysis
3-
Version: 1.0.0.9000
3+
Version: 1.0.0.9001
44
Authors@R: person("Jim", "Hester", email = "[email protected]", role = c("aut", "cre"))
55
URL: https://github.com/jimhester/lintr
66
BugReports: https://github.com/jimhester/lintr/issues

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export(Lint)
1010
export(absolute_paths_linter)
1111
export(assignment_linter)
1212
export(camel_case_linter)
13+
export(checkstyle_output)
1314
export(clear_cache)
1415
export(closed_curly_linter)
1516
export(commas_linter)

R/lint.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,44 @@ rstudio_source_markers <- function(lints) {
252252
autoSelect = "first")
253253
}
254254

255+
#' Checkstyle Report for lint results
256+
#'
257+
#' Generate a report of the linting results using the Checkstyle xml format
258+
#' @param lints the linting results.
259+
#' @param reportName the name of the output report. It will be generated and stored
260+
#' in a reports/ directory.
261+
#' @export
262+
checkstyle_output <- function(lints, reportName = "lint_checkstyle_results.xml") {
263+
264+
# Auto-create a results directory, if one has no existed previously
265+
if (!dir.exists("results")) {
266+
dir.create("results")
267+
}
268+
reportPath = paste("results/", reportName, sep = "")
269+
file.create(reportPath)
270+
271+
# setup file
272+
cat("<?xml version=\"1.0\" encoding=\"utf-8\"?>", file = reportPath, sep = "\n", append = TRUE)
273+
cat("<checkstyle version=\"4.3\">", file = reportPath, sep = "\n", append = TRUE)
274+
275+
# output the style markers to the file
276+
lapply(lints, function(x) {
277+
# Need to replace left carat for xml report
278+
msg <- sub("<", "&gt;", x$message)
279+
280+
filemsg = paste("\t<file name=\"", x$filename, "\">", sep = "")
281+
cat(filemsg, file = reportPath, "\n", append = TRUE)
282+
283+
errmsg = paste("\t\t<error line=\"", x$line_number,"\" column=\"", x$column,
284+
"\" severity=\"error\" message=\"",msg,"\" />", sep = "")
285+
cat(errmsg, file = reportPath, sep = "\n", append = TRUE)
286+
287+
cat("\t</file>", file = reportPath, sep = "\n", append = TRUE)
288+
})
289+
290+
cat("</checkstyle>", file = reportPath, sep = "\n", append = TRUE)
291+
}
292+
255293
highlight_string <- function(message, column_number = NULL, ranges = NULL) {
256294

257295
maximum <- max(column_number, unlist(ranges))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library(XML)
2+
context("checkstyle_output")
3+
test_that("return lint report as checkstyle xml", {
4+
with_mock(`rstudioapi::callFun` = function(...) return(list(...)),
5+
lints <- structure(
6+
list(
7+
Lint(filename = "test_file",
8+
line_number = 1,
9+
column_number = 2,
10+
type = "error",
11+
line = "a line",
12+
message = "hi")
13+
),
14+
class = "lints"
15+
),
16+
checkstyle_output(lints, "test-checkstyle.xml"),
17+
expect_true(file.exists("results/test-checkstyle.xml"))
18+
)
19+
20+
xmlfile <- xmlTreeParse("results/test-checkstyle.xml")
21+
topxml <- xmlRoot(xmlfile)
22+
topxmlStr <- toString.XMLNode(topxml)
23+
expect_equal(topxmlStr,
24+
"<checkstyle version=\"4.3\">\n <file name=\"test_file\">\n <error line=\"1\" column=\"2\" severity=\"error\" message=\"hi\"/>\n </file>\n</checkstyle>"
25+
)
26+
27+
#clean up
28+
file.remove("results/test-checkstyle.xml")
29+
file.remove("results")
30+
})

0 commit comments

Comments
 (0)