Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start a fuzzing suite to test for consistency of lints #2818

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3882350
use maybe_write_content for easier 'mocking'
MichaelChirico Mar 5, 2025
c392c53
initial progress
MichaelChirico Mar 6, 2025
5cef281
getting very close i think...
MichaelChirico Mar 6, 2025
a4e4a66
skip Rmd files
MichaelChirico Mar 6, 2025
0b1eaf5
caught a live one!
MichaelChirico Mar 6, 2025
868ad30
need to match original file extension?
MichaelChirico Mar 6, 2025
0ed5cc0
caught another one!
MichaelChirico Mar 6, 2025
99d00a3
simpler approach, avoid rex() due to bug
MichaelChirico Mar 6, 2025
d3cca7a
also ignore warnings
MichaelChirico Mar 6, 2025
59dc1b0
finally getting somewhere...
MichaelChirico Mar 6, 2025
a25065f
progressively more complicated :(
MichaelChirico Mar 6, 2025
491a340
round of fixes & first working nofuzz
MichaelChirico Mar 6, 2025
92f0628
looks like we got another live one... break time
MichaelChirico Mar 6, 2025
d387a71
another true positive
MichaelChirico Mar 6, 2025
e150ffe
more ignores, need '.' in file extension, restore test
MichaelChirico Mar 6, 2025
3d1fc0e
wrapping up
MichaelChirico Mar 6, 2025
b69b7cd
Write up the GHA config
MichaelChirico Mar 6, 2025
b8a06e3
annotation
MichaelChirico Mar 6, 2025
a3dbf27
comment for future work
MichaelChirico Mar 6, 2025
5a22050
vestigial
MichaelChirico Mar 6, 2025
76b869f
skips on old R
MichaelChirico Mar 6, 2025
afec743
expect_no_lint
MichaelChirico Mar 6, 2025
51593e4
new tests
MichaelChirico Mar 6, 2025
f4b9481
NEWS
MichaelChirico Mar 6, 2025
6389d55
bad copy-paste
MichaelChirico Mar 6, 2025
1550ead
need stop_on_failure for batch?
MichaelChirico Mar 6, 2025
bbdac43
delint, fix last skip for R<4.1.0
MichaelChirico Mar 6, 2025
523c218
more extensible structure
MichaelChirico Mar 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions .dev/ast_fuzz_test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Fuzz testing for lint consistency
#
# We have often encountered issues where we handle
# equivalent R constructs inconsistently, e.g.,
# function(...) should almost always match the same
# rules as \(...), and '<-' assignment should almost
# always be equivalent to '='.
#
# Here, we seek to enforce that (under eventual consistency)
# by randomly altering the contents of files encountered
# under expect_lint() to swap known equivalencies.

library(xml2)

expect_lint_file <- "R/expect_lint.R"

original <- readLines(expect_lint_file)
expected_line <- "file <- maybe_write_content(file, content)"
expected_line_idx <- grep(expected_line, original, fixed = TRUE)
if (length(expected_line_idx) != 1L) {
stop(sprintf(
"Please update this workflow -- need exactly one hit for line '%s' in file '%s'.",
expected_line, expect_lint_file
))
}
writeLines(
c(
head(original, expected_line_idx-1L),
# overwrite original exit hook to always delete the fuzzed file
" on.exit({reset_lang(old_lang); unlink(file)})",
" file <- maybe_fuzz_content(file, content)",
tail(original, -expected_line_idx),
readLines(".dev/maybe_fuzz_content.R")
),
expect_lint_file
)
# Not useful in CI but good when running locally.
withr::defer({
writeLines(original, expect_lint_file)
pkgload::load_all()
})

pkgload::load_all()

# beware lazy eval: originally tried adding a withr::defer() in each iteration, but
# this effectively only runs the last 'defer' expression as the names are only
# evaluated at run-time. So instead keep track of all edits in this object.
# this approach to implementing 'nofuzz' feels painfully manual, but I couldn't
# figure out how else to get 'testthat' to give us what we need -- the failures
# object in the reporter is frustratingly inconsistent in whether the trace
# exists, and even if it does, we'd have to text-mangle to get the corresponding
# file names out. Also, the trace 'srcref' happens under keep.source=FALSE,
# so we lose any associated comments anyway. even that would not solve the issue
# of getting top-level exclusions done for 'nofuzz start|end' ranges, except
# maybe if it enabled us to reuse lintr's own exclude() system.
# therefore we take this approach: pass over the test suite first and comment out
# any tests/units that have been marked 'nofuzz'. restore later.
test_restorations <- list()
for (test_file in list.files("tests/testthat", pattern = "^test-", full.names = TRUE)) {
xml <- read_xml(xmlparsedata::xml_parse_data(parse(test_file, keep.source = TRUE)))
# parent::* to catch top-level comments (exprlist). matches one-line nofuzz and start/end ranges.
nofuzz_lines <- xml_find_all(xml, "//COMMENT[contains(text(), 'nofuzz')]/parent::*")
if (length(nofuzz_lines) == 0L) next

test_original <- test_lines <- readLines(test_file)

for (nofuzz_line in nofuzz_lines) {
comments <- xml_find_all(nofuzz_line, "COMMENT[contains(text(), 'nofuzz')]")
comment_text <- xml_text(comments)
# handle start/end ranges first.
start_idx <- grep("nofuzz start", comment_text, fixed = TRUE)
end_idx <- grep("nofuzz end", comment_text, fixed = TRUE)
if (length(start_idx) != length(end_idx) || any(end_idx < start_idx)) {
stop(sprintf(
"Mismatched '# nofuzz start' (%s), '# nofuzz end' (%s) in %s",
toString(start_idx), toString(end_idx), test_file
))
}

comment_ranges <- Map(`:`,
as.integer(xml_attr(comments[start_idx], "line1")),
as.integer(xml_attr(comments[end_idx], "line1"))
)
for (comment_range in comment_ranges) {
test_lines[comment_range] <- paste("#", test_lines[comment_range])
}

if (length(start_idx) > 0L && !any(!start_idx & !end_idx)) next

# NB: one-line tests line expect_lint(...) # nofuzz are not supported,
# since the comment will attach to the parent test_that() & thus comment
# out the whole unit. Easiest solution is just to spread out those few tests for now.
comment_range <- as.integer(xml_attr(nofuzz_line, "line1")):as.integer(xml_attr(nofuzz_line, "line2"))
test_lines[comment_range] <- paste("#", test_lines[comment_range])
}

writeLines(test_lines, test_file)
test_restorations <- c(test_restorations, list(list(file = test_file, lines = test_original)))
}
withr::defer(for (restoration in test_restorations) writeLines(restoration$lines, restoration$file))

# for some reason, 'report <- test_dir(...)' did not work -- the resulting object is ~empty.
# even 'report <- test_local(...)', which does return an object, lacks any information about
# which tests failed (all reports are about successful or skipped tests). probably this is not
# the best approach but documentation was not very helpful.
reporter <- testthat::SummaryReporter$new()
testthat::test_local(reporter = reporter, stop_on_failure = FALSE)

failures <- reporter$failures$as_list()
# ignore any test that failed for expected reasons, e.g. some known lint metadata changes
# about line numbers or the contents of the line. this saves us having to pepper tons of
# 'nofuzz' comments throughout the suite, as well as getting around the difficulty of injecting
# 'expect_lint()' with new code to ignore these attributes (this latter we might explore later).
valid_failure <- vapply(
failures,
function(failure) {
if (grepl('(column_number|ranges|line) .* did not match', failure$message)) {
return(TRUE)
}
FALSE
},
logical(1L)
)
if (!all(valid_failure)) {
failures <- failures[!valid_failure]
names(failures) <- vapply(failures, `[[`, "test", FUN.VALUE = character(1L))
cat("Some fuzzed tests failed unexpectedly!\n")
print(failures)
stop("Use # nofuzz [start|end] to mark false positives or fix any bugs.")
}
62 changes: 62 additions & 0 deletions .dev/maybe_fuzz_content.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
maybe_fuzz_content <- function(file, lines) {
if (is.null(file)) {
new_file <- tempfile()
con <- file(new_file, encoding = "UTF-8")
writeLines(lines, con = con, sep = "\n")
close(con)
} else {
new_file <- tempfile(fileext = paste0(".", tools::file_ext(file)))
file.copy(file, new_file, copy.mode = FALSE)
}

apply_fuzzers(new_file)

new_file
}

function_lambda_fuzzer <- function(pd, lines) {
fun_tokens <- c(`'\\\\'` = "\\", `FUNCTION` = "function")
fun_idx <- which(pd$token %in% names(fun_tokens))
n_fun <- length(fun_idx)

if (n_fun == 0L) {
return(invisible())
}

pd$new_text <- NA_character_
pd$new_text[fun_idx] <- sample(fun_tokens, n_fun, replace = TRUE)

for (ii in rev(fun_idx)) {
if (pd$text[ii] == pd$new_text[ii]) next
# Tried, with all rex(), hit a bug: https://github.com/r-lib/rex/issues/96
ptn = paste0("^(.{", pd$col1[ii] - 1L, "})", rex::rex(pd$text[ii]))
lines[pd$line1[ii]] <- rex::re_substitutes(lines[pd$line1[ii]], ptn, paste0("\\1", rex::rex(pd$new_text[ii])))
}
lines
}

# we could also consider just passing any test where no fuzzing takes place,
# i.e. letting the other GHA handle whether unfuzzed tests pass as expected.
apply_fuzzers <- function(f) {
# skip errors for e.g. Rmd files, and ignore warnings.
# We could use get_source_expressions(), but with little benefit & much slower.
pd <- tryCatch(getParseData(suppressWarnings(parse(f, keep.source = TRUE))), error = identity)
if (inherits(pd, "error")) {
return(invisible())
}

reparse <- FALSE
lines <- readLines(f)
for (fuzzer in list(function_lambda_fuzzer)) {
if (reparse) {
pd <- getParseData(parse(f, keep.source = TRUE))
lines <- readLines(f)
}
updated_lines <- fuzzer(pd, lines)
reparse <- !is.null(updated_lines)
if (!reparse) next # skip some I/O if we can
writeLines(updated_lines, f)
}

invisible()
}
30 changes: 30 additions & 0 deletions .github/workflows/ast-fuzz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Randomly change some code & ensure lint equivalency is maintained
on:
push:
branches: [main]
# TODO before merging: remove this. Only kept to demonstrate during review.
pull_request:
branches: [main]

name: ast-fuzz

jobs:
repo-meta-tests:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
r-version: "release"
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2

- name: Ensure equivalent code generates equivalent lints
run: |
callr::rscript(".dev/ast_fuzz_test.R")
shell: Rscript {0}
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
### Lint accuracy fixes: removing false negatives

* `todo_comment_linter()` finds comments inside {roxygen2} markup comments (#2447, @MichaelChirico).
* Linters with logic around function declarations consistently include the R 4.0.0 shorthand `\()` (#2818, continuation of earlier #2190, @MichaelChirico).
+ `library_call_linter()`
+ `terminal_close_linter()`
+ `unnecessary_lambda_linter()`

## Notes

Expand Down
22 changes: 13 additions & 9 deletions R/expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,8 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
old_lang <- set_lang(language)
on.exit(reset_lang(old_lang))

if (is.null(file)) {
file <- tempfile()
on.exit(unlink(file), add = TRUE)
local({
con <- base::file(file, encoding = "UTF-8")
on.exit(close(con))
writeLines(content, con = con, sep = "\n")
})
}
if (is.null(file)) on.exit(unlink(file), add = TRUE)
file <- maybe_write_content(file, content)

lints <- lint(file, ...)
n_lints <- length(lints)
Expand Down Expand Up @@ -121,6 +114,17 @@ expect_no_lint <- function(content, ..., file = NULL, language = "en") {
expect_lint(content, NULL, ..., file = file, language = language)
}

maybe_write_content <- function(file, lines) {
if (!is.null(file)) {
return(file)
}
tmp <- tempfile()
con <- file(tmp, encoding = "UTF-8")
on.exit(close(con))
writeLines(lines, con = con, sep = "\n")
tmp
}

#' Test that the package is lint free
#'
#' This function is a thin wrapper around lint_package that simply tests there are no
Expand Down
4 changes: 2 additions & 2 deletions R/library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ library_call_linter <- function(allow_preamble = TRUE) {
expr[2][STR_CONST]
or (
SYMBOL_SUB[text() = 'character.only']
and not(ancestor::expr[FUNCTION])
and not(ancestor::expr[FUNCTION or OP-LAMBDA])
)
]
")
Expand All @@ -122,7 +122,7 @@ library_call_linter <- function(allow_preamble = TRUE) {
//SYMBOL_FUNCTION_CALL[{ xp_text_in_table(bad_indirect_funs) }]
/parent::expr
/parent::expr[
not(ancestor::expr[FUNCTION])
not(ancestor::expr[FUNCTION or OP-LAMBDA])
and expr[{ call_symbol_cond }]
]
")
Expand Down
2 changes: 1 addition & 1 deletion R/terminal_close_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' @export
terminal_close_linter <- make_linter_from_xpath(
xpath = "
//FUNCTION
(//FUNCTION | //OP-LAMBDA)
/following-sibling::expr
/expr[last()][
expr/SYMBOL_FUNCTION_CALL[text() = 'close']
Expand Down
2 changes: 1 addition & 1 deletion R/unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ unnecessary_lambda_linter <- function(allow_comparison = FALSE) {
# NB: this includes 0+3 and TRUE+FALSE, which are also fine.
inner_comparison_xpath <- glue("
parent::expr
/expr[FUNCTION]
/expr[FUNCTION or OP-LAMBDA]
/expr[
({ xp_or(infix_metadata$xml_tag[infix_metadata$comparator]) })
and expr[
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-exclusions.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ test_that("#1442: is_excluded_files works if no global exclusions are specified"
)

# 3 lints: assignment_linter(), quotes_linter() and line_length_linter()
expect_lint(
expect_lint( # nofuzz
file = file.path(tmp, "bad.R"),
checks = list(
list(linter = "assignment_linter", line_number = 1L),
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ test_that("Multi-byte character truncated by parser is ignored", {
})

test_that("Can read non UTF-8 file", {
withr::local_options(list(lintr.linter_file = tempfile()))
proj_dir <- test_path("dummy_projects", "project")
withr::local_dir(proj_dir)
expect_no_lint(file = "cp1252.R", linters = list())
expect_no_lint( # nofuzz
file = "cp1252.R",
linters = list()
)
})

test_that("Warns if encoding is misspecified, Pt. 1", {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-indentation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ test_that("indentation linter flags improper closing curly braces", {
)
})

# nofuzz start
test_that("function argument indentation works in tidyverse-style", {
linter <- indentation_linter()
expect_no_lint(
Expand Down Expand Up @@ -355,6 +356,7 @@ test_that("function argument indentation works in always-hanging-style", {
linter
)
})
# nofuzz end

test_that("indentation with operators works", {
linter <- indentation_linter()
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-library_call_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ test_that("skips allowed usages of library()/character.only=TRUE", {
expect_no_lint("library(data.table)", linter)
expect_no_lint("function(pkg) library(pkg, character.only = TRUE)", linter)
expect_no_lint("function(pkgs) sapply(pkgs, require, character.only = TRUE)", linter)

skip_if_not_r_version("4.1.0")
expect_no_lint("\\(pkg) library(pkg, character.only = TRUE)", linter)
expect_no_lint("\\(pkgs) sapply(pkgs, require, character.only = TRUE)", linter)
})

test_that("blocks disallowed usages of strings in library()/require()", {
Expand Down
Loading
Loading