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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
regexes <- list(
assign = rex::rex("Use one of <-, <<- for assignment, not =."),
local_var = rex::rex("local variable"),
quotes = rex::rex("Only use double-quotes."),
trailing = rex::rex("Remove trailing blank lines."),
trailws = rex::rex("Remove trailing whitespace."),
indent = rex::rex("Indentation should be")
)
test_that("it handles dir", {
file_pattern <- rex::rex(".R", one_of("html", "md", "nw", "rst", "tex", "txt"))
lints <- lint_dir(path = "knitr_formats", pattern = file_pattern, parse_settings = FALSE)
# For every file there should be at least 1 lint
expect_identical(
sort(unique(names(lints))),
sort(list.files(test_path("knitr_formats"), pattern = file_pattern))
)
})
test_that("it handles markdown", {
expect_lint(
file = test_path("knitr_formats", "test.Rmd"),
checks = list(
list(regexes[["assign"]], line_number = 9L),
list(regexes[["local_var"]], line_number = 22L),
list(regexes[["assign"]], line_number = 22L),
list(regexes[["trailing"]], line_number = 24L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles quarto", {
expect_lint(
file = test_path("knitr_formats", "test.qmd"),
checks = list(
list(regexes[["assign"]], line_number = 9L),
list(regexes[["local_var"]], line_number = 22L),
list(regexes[["assign"]], line_number = 22L),
list(regexes[["trailing"]], line_number = 24L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles Sweave", {
expect_lint(
file = test_path("knitr_formats", "test.Rnw"),
checks = list(
list(regexes[["assign"]], line_number = 12L),
list(regexes[["local_var"]], line_number = 24L),
list(regexes[["assign"]], line_number = 24L),
list(regexes[["trailing"]], line_number = 26L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles reStructuredText", {
expect_lint(
file = test_path("knitr_formats", "test.Rrst"),
checks = list(
list(regexes[["assign"]], line_number = 10L),
list(regexes[["local_var"]], line_number = 23L),
list(regexes[["assign"]], line_number = 23L),
list(regexes[["trailing"]], line_number = 25L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles HTML", {
expect_lint(
file = test_path("knitr_formats", "test.Rhtml"),
checks = list(
list(regexes[["assign"]], line_number = 15L),
list(regexes[["local_var"]], line_number = 27L),
list(regexes[["assign"]], line_number = 27L),
list(regexes[["trailing"]], line_number = 29L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles tex", {
expect_lint(
file = test_path("knitr_formats", "test.Rtex"),
checks = list(
list(regexes[["indent"]], line_number = 11L),
list(regexes[["assign"]], line_number = 11L),
list(regexes[["indent"]], line_number = 22L),
list(regexes[["local_var"]], line_number = 23L),
list(regexes[["assign"]], line_number = 23L),
list(regexes[["trailing"]], line_number = 25L),
list(regexes[["trailws"]], line_number = 25L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it handles asciidoc", {
expect_lint(
file = test_path("knitr_formats", "test.Rtxt"),
checks = list(
list(regexes[["assign"]], line_number = 9L),
list(regexes[["local_var"]], line_number = 22L),
list(regexes[["assign"]], line_number = 22L),
list(regexes[["trailing"]], line_number = 24L)
),
linters = default_linters,
parse_settings = FALSE
)
})
test_that("it does _not_ handle brew", {
expect_lint("'<% a %>'\n",
checks = list(
regexes[["quotes"]],
regexes[["trailing"]]
),
default_linters
)
})
test_that("it does _not_ error with inline \\Sexpr", {
expect_lint(
"#' text \\Sexpr{1 + 1} more text",
NULL,
default_linters
)
})
test_that("it does lint .Rmd or .qmd file with malformed input", {
expect_lint(
file = test_path("knitr_malformed", "incomplete_r_block.Rmd"),
checks = "Missing chunk end",
linters = default_linters,
parse_settings = FALSE
)
expect_lint(
file = test_path("knitr_malformed", "incomplete_r_block.qmd"),
checks = "Missing chunk end",
linters = default_linters,
parse_settings = FALSE
)
contents <- c(
trim_some("
```{r chunk}
lm(x ~ y)
# some text
```
bash some_script.sh
```
"),
trim_some("
```{r chunk-1}
code <- 42
# A heading
Some text
```{r chunk-2}
some_more_code <- 42
```
"),
trim_some("
```{r chunk-1}
code <- 42
```
# A heading
Some text
```{r chunk-2}
some_more_code <- 42
")
)
expected <- list(
NULL, # This test case would require parsing all chunk fences, not just r chunks.
"maybe starting at line 1",
"maybe starting at line 8"
)
for (i in seq_along(contents)) {
expect_lint(contents[[i]], expected[[i]], linters = list())
}
})
|