Skip to content

Commit

Permalink
Ship SARIF template as a JSON file in external data folder (#2213)
Browse files Browse the repository at this point in the history
* Include SARIF template as a JSON file

Storing the template file in a `.json` file makes it easier to read, esp. because of proper indentation, and also to format them in case of modification.

Inlining the JSON as a string in the source code makes both of these difficult.

* add missing terminal line

---------

Co-authored-by: AshesITR <[email protected]>
  • Loading branch information
IndrajeetPatil and AshesITR authored Oct 13, 2023
1 parent 8f8f151 commit 4a8b931
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
59 changes: 1 addition & 58 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -575,64 +575,7 @@ sarif_output <- function(lints, filename = "lintr_results.sarif") {

# setup template
sarif <- jsonlite::fromJSON(
'{
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "lintr",
"informationUri": "https://lintr.r-lib.org/",
"version": "2.0.1",
"rules": [
{
"id": "trailing_whitespace_linter",
"fullDescription": {
"text": "Trailing whitespace is superfluous."
},
"defaultConfiguration": {
"level": "note"
}
}
]
}
},
"results": [
{
"ruleId": "trailing_whitespace_linter",
"ruleIndex": 0,
"message": {
"text": "Trailing blank lines are superfluous."
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "TestFileFolder/hello.r",
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 2,
"startColumn": 22,
"snippet": {
"text": "print(Hello World!) "
}
}
}
}
]
}
],
"columnKind": "utf16CodeUnits",
"originalUriBaseIds": {
"ROOTPATH": {
"uri": "file:///C:/repos/repototest/"
}
}
}
]
}',
system.file("extdata", "sarif-template.json", package = "lintr"),
simplifyVector = TRUE,
simplifyDataFrame = FALSE,
simplifyMatrix = FALSE
Expand Down
58 changes: 58 additions & 0 deletions inst/extdata/sarif-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "lintr",
"informationUri": "https://lintr.r-lib.org/",
"version": "2.0.1",
"rules": [
{
"id": "trailing_whitespace_linter",
"fullDescription": {
"text": "Trailing whitespace is superfluous."
},
"defaultConfiguration": {
"level": "note"
}
}
]
}
},
"results": [
{
"ruleId": "trailing_whitespace_linter",
"ruleIndex": 0,
"message": {
"text": "Trailing blank lines are superfluous."
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "TestFileFolder/hello.r",
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 2,
"startColumn": 22,
"snippet": {
"text": "print(Hello World!) "
}
}
}
}
]
}
],
"columnKind": "utf16CodeUnits",
"originalUriBaseIds": {
"ROOTPATH": {
"uri": "file:///C:/repos/repototest/"
}
}
}
]
}
8 changes: 6 additions & 2 deletions tests/testthat/test-sarif_output.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
skip_if_not_installed("jsonlite")

test_that("`sarif_output` produces expected error", {
skip_if_not_installed("jsonlite")

l <- lint(text = "x = 1", linters = assignment_linter())
expect_error(sarif_output(l), "Package path needs to be a relative path", fixed = TRUE)
})

test_that("`sarif_output` writes expected files", {
skip_if_not_installed("jsonlite")

l <- lint_package(
test_path("dummy_packages", "missing_dep"),
linters = object_length_linter(),
Expand All @@ -24,6 +26,8 @@ test_that("`sarif_output` writes expected files", {
})

test_that("`sarif_output` produces valid files", {
skip_if_not_installed("jsonlite")

l <- lint_package(
test_path("dummy_packages", "clean"),
linters = default_linters,
Expand Down

0 comments on commit 4a8b931

Please sign in to comment.