From 901d9ed3096933973c412d77262647b455da45dc Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 27 Jan 2024 09:12:31 -0800 Subject: [PATCH] Remove local_tempfile() usage in examples following breakage by withr 3.0.0 (#2519) * debugging * dont use withr in examples * kwarg for cat() --- DESCRIPTION | 2 +- R/get_source_expressions.R | 6 ++++-- R/ids_with_token.R | 6 ++++-- R/is_lint_level.R | 6 ++++-- R/lint.R | 6 ++++-- R/trailing_blank_lines_linter.R | 13 ++++++++----- R/utils.R | 10 +++++++--- R/with.R | 6 ++++-- man/get_r_string.Rd | 10 ++++++---- man/get_source_expressions.Rd | 6 +++--- man/ids_with_token.Rd | 7 ++++--- man/is_lint_level.Rd | 7 ++++--- man/lint.Rd | 7 ++++--- man/linters_with_defaults.Rd | 7 ++++--- man/trailing_blank_lines_linter.Rd | 14 ++++++++------ 15 files changed, 69 insertions(+), 44 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c3857e4b3..3bf5b9a15 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -56,7 +56,7 @@ Config/Needs/development: pkgload, cli, testthat, patrick Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Collate: 'make_linter_from_xpath.R' 'xp_utils.R' diff --git a/R/get_source_expressions.R b/R/get_source_expressions.R index 3bdba9030..bc83712d5 100644 --- a/R/get_source_expressions.R +++ b/R/get_source_expressions.R @@ -54,9 +54,11 @@ #' \item{lines}{The [readLines()] output for this file.} #' } #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) -#' tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +#' @examples +#' tmp <- tempfile() +#' writeLines(c("x <- 1", "y <- x + 1"), tmp) #' get_source_expressions(tmp) +#' unlink(tmp) #' @export get_source_expressions <- function(filename, lines = NULL) { source_expression <- srcfile(filename, encoding = settings$encoding) diff --git a/R/ids_with_token.R b/R/ids_with_token.R index 9518b4272..69b04a578 100644 --- a/R/ids_with_token.R +++ b/R/ids_with_token.R @@ -17,11 +17,13 @@ #' the `token` column of `parsed_content`. Typically `==` or `%in%`. #' @param source_file (DEPRECATED) Same as `source_expression`. Will be removed. #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) -#' tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +#' @examples +#' tmp <- tempfile() +#' writeLines(c("x <- 1", "y <- x + 1"), tmp) #' source_exprs <- get_source_expressions(tmp) #' ids_with_token(source_exprs$expressions[[1L]], value = "SYMBOL") #' with_id(source_exprs$expressions[[1L]], 2L) +#' unlink(tmp) #' #' @return `ids_with_token`: The indices of the `parsed_content` data frame #' entry of the list of source expressions. Indices correspond to the diff --git a/R/is_lint_level.R b/R/is_lint_level.R index d850c51cf..38d96d9ba 100644 --- a/R/is_lint_level.R +++ b/R/is_lint_level.R @@ -9,13 +9,15 @@ #' means an individual expression, while `"file"` means all expressions #' in the current file are available. #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) -#' tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +#' @examples +#' tmp <- tempfile() +#' writeLines(c("x <- 1", "y <- x + 1"), tmp) #' source_exprs <- get_source_expressions(tmp) #' is_lint_level(source_exprs$expressions[[1L]], level = "expression") #' is_lint_level(source_exprs$expressions[[1L]], level = "file") #' is_lint_level(source_exprs$expressions[[3L]], level = "expression") #' is_lint_level(source_exprs$expressions[[3L]], level = "file") +#' unlink(tmp) #' #' @export is_lint_level <- function(source_expression, level = c("expression", "file")) { diff --git a/R/lint.R b/R/lint.R index b4fec08cd..4435cb081 100644 --- a/R/lint.R +++ b/R/lint.R @@ -26,11 +26,13 @@ #' #' @return An object of class `c("lints", "list")`, each element of which is a `"list"` object. #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) -#' f <- withr::local_tempfile(lines = "a=1", fileext = "R") +#' @examples +#' f <- tempfile() +#' writeLines("a=1", f) #' lint(f) # linting a file #' lint("a = 123\n") # linting inline-code #' lint(text = "a = 123") # linting inline-code +#' unlink(f) #' #' @export lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings = TRUE, text = NULL) { diff --git a/R/trailing_blank_lines_linter.R b/R/trailing_blank_lines_linter.R index 101d778aa..023bfda1a 100644 --- a/R/trailing_blank_lines_linter.R +++ b/R/trailing_blank_lines_linter.R @@ -2,22 +2,25 @@ #' #' Check that there are no trailing blank lines in source code. #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) +#' @examples #' # will produce lints -#' f <- withr::local_tempfile(lines = "x <- 1\n") -#' readLines(f) +#' f <- tempfile() +#' cat("x <- 1\n\n", file = f) +#' writeLines(readChar(f, file.size(f))) #' lint( #' filename = f, #' linters = trailing_blank_lines_linter() #' ) +#' unlink(f) #' #' # okay -#' f <- withr::local_tempfile(lines = "x <- 1") -#' readLines(f) +#' cat("x <- 1\n", file = f) +#' writeLines(readChar(f, file.size(f))) #' lint( #' filename = f, #' linters = trailing_blank_lines_linter() #' ) +#' unlink(f) #' #' @evalRd rd_tags("trailing_blank_lines_linter") #' @seealso [linters] for a complete list of linters available in lintr. diff --git a/R/utils.R b/R/utils.R index 6331c3440..8d2f92378 100644 --- a/R/utils.R +++ b/R/utils.R @@ -221,20 +221,24 @@ platform_independent_sort <- function(x) x[platform_independent_order(x)] #' and `xpath` is specified, it is extracted with [xml2::xml_find_chr()]. #' @param xpath An XPath, passed on to [xml2::xml_find_chr()] after wrapping with `string()`. #' -#' @examplesIf requireNamespace("withr", quietly = TRUE) -#' tmp <- withr::local_tempfile(lines = "c('a', 'b')") +#' @examples +#' tmp <- tempfile() +#' writeLines("c('a', 'b')", tmp) #' expr_as_xml <- get_source_expressions(tmp)$expressions[[1L]]$xml_parsed_content #' writeLines(as.character(expr_as_xml)) #' get_r_string(expr_as_xml, "expr[2]") # "a" #' get_r_string(expr_as_xml, "expr[3]") # "b" +#' unlink(tmp) #' #' # more importantly, extract strings under R>=4 raw strings #' @examplesIf getRversion() >= "4.0.0" -#' tmp4.0 <- withr::local_tempfile(lines = "c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')") +#' tmp4.0 <- tempfile() +#' writeLines("c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')", tmp4.0) #' expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content #' writeLines(as.character(expr_as_xml4.0)) #' get_r_string(expr_as_xml4.0, "expr[2]") # "a\\b" #' get_r_string(expr_as_xml4.0, "expr[3]") # "a\\\"'\"\\b" +#' unlink(tmp4.0) #' #' @export get_r_string <- function(s, xpath = NULL) { diff --git a/R/with.R b/R/with.R index 399d5769e..8a036c1b4 100644 --- a/R/with.R +++ b/R/with.R @@ -149,10 +149,12 @@ all_linters <- function(..., packages = "lintr") { #' #' @param defaults Default list of linters to modify. Must be named. #' @inheritParams linters_with_tags -#' @examplesIf requireNamespace("withr", quietly = TRUE) +#' @examples #' # When using interactively you will usually pass the result onto `lint` or `lint_package()` -#' f <- withr::local_tempfile(lines = "my_slightly_long_variable_name <- 2.3", fileext = "R") +#' f <- tempfile() +#' writeLines("my_slightly_long_variable_name <- 2.3", f) #' lint(f, linters = linters_with_defaults(line_length_linter = line_length_linter(120L))) +#' unlink(f) #' #' # the default linter list with a different line length cutoff #' my_linters <- linters_with_defaults(line_length_linter = line_length_linter(120L)) diff --git a/man/get_r_string.Rd b/man/get_r_string.Rd index 96a3db2bc..05b8c6062 100644 --- a/man/get_r_string.Rd +++ b/man/get_r_string.Rd @@ -23,20 +23,22 @@ NB: this is also properly vectorized on \code{s}, and accepts a variety of input will become \code{NA} outputs, which helps ensure that \code{length(get_r_string(s)) == length(s)}. } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -tmp <- withr::local_tempfile(lines = "c('a', 'b')") +tmp <- tempfile() +writeLines("c('a', 'b')", tmp) expr_as_xml <- get_source_expressions(tmp)$expressions[[1L]]$xml_parsed_content writeLines(as.character(expr_as_xml)) get_r_string(expr_as_xml, "expr[2]") # "a" get_r_string(expr_as_xml, "expr[3]") # "b" +unlink(tmp) # more importantly, extract strings under R>=4 raw strings -\dontshow{\}) # examplesIf} \dontshow{if (getRversion() >= "4.0.0") (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -tmp4.0 <- withr::local_tempfile(lines = "c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')") +tmp4.0 <- tempfile() +writeLines("c(R'(a\\\\b)', R'--[a\\\\\"\'\"\\\\b]--')", tmp4.0) expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content writeLines(as.character(expr_as_xml4.0)) get_r_string(expr_as_xml4.0, "expr[2]") # "a\\b" get_r_string(expr_as_xml4.0, "expr[3]") # "a\\\"'\"\\b" +unlink(tmp4.0) \dontshow{\}) # examplesIf} } diff --git a/man/get_source_expressions.Rd b/man/get_source_expressions.Rd index 3293f0e31..f48939d0e 100644 --- a/man/get_source_expressions.Rd +++ b/man/get_source_expressions.Rd @@ -67,8 +67,8 @@ This setting is found by taking the first valid result from the following locati } } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +tmp <- tempfile() +writeLines(c("x <- 1", "y <- x + 1"), tmp) get_source_expressions(tmp) -\dontshow{\}) # examplesIf} +unlink(tmp) } diff --git a/man/ids_with_token.Rd b/man/ids_with_token.Rd index af0cc1b11..35d7c5161 100644 --- a/man/ids_with_token.Rd +++ b/man/ids_with_token.Rd @@ -48,10 +48,11 @@ conjunction with \code{ids_with_token} to iterate over rows containing desired t }} \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +tmp <- tempfile() +writeLines(c("x <- 1", "y <- x + 1"), tmp) source_exprs <- get_source_expressions(tmp) ids_with_token(source_exprs$expressions[[1L]], value = "SYMBOL") with_id(source_exprs$expressions[[1L]], 2L) -\dontshow{\}) # examplesIf} +unlink(tmp) + } diff --git a/man/is_lint_level.Rd b/man/is_lint_level.Rd index b0e3e0152..101fcf100 100644 --- a/man/is_lint_level.Rd +++ b/man/is_lint_level.Rd @@ -19,12 +19,13 @@ Helper for determining whether the current \code{source_expression} contains all expressions in the current file, or just a single expression. } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -tmp <- withr::local_tempfile(lines = c("x <- 1", "y <- x + 1")) +tmp <- tempfile() +writeLines(c("x <- 1", "y <- x + 1"), tmp) source_exprs <- get_source_expressions(tmp) is_lint_level(source_exprs$expressions[[1L]], level = "expression") is_lint_level(source_exprs$expressions[[1L]], level = "file") is_lint_level(source_exprs$expressions[[3L]], level = "expression") is_lint_level(source_exprs$expressions[[3L]], level = "file") -\dontshow{\}) # examplesIf} +unlink(tmp) + } diff --git a/man/lint.Rd b/man/lint.Rd index efc4d2925..a165a9343 100644 --- a/man/lint.Rd +++ b/man/lint.Rd @@ -89,12 +89,13 @@ Note that if files contain unparseable encoding problems, only the encoding prob unintelligible error messages from other linters. } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -f <- withr::local_tempfile(lines = "a=1", fileext = "R") +f <- tempfile() +writeLines("a=1", f) lint(f) # linting a file lint("a = 123\n") # linting inline-code lint(text = "a = 123") # linting inline-code -\dontshow{\}) # examplesIf} +unlink(f) + if (FALSE) { lint_dir() diff --git a/man/linters_with_defaults.Rd b/man/linters_with_defaults.Rd index 183986a67..9cabd02b4 100644 --- a/man/linters_with_defaults.Rd +++ b/man/linters_with_defaults.Rd @@ -19,10 +19,11 @@ The result of this function is meant to be passed to the \code{linters} argument or to be put in your configuration file. } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # When using interactively you will usually pass the result onto `lint` or `lint_package()` -f <- withr::local_tempfile(lines = "my_slightly_long_variable_name <- 2.3", fileext = "R") +f <- tempfile() +writeLines("my_slightly_long_variable_name <- 2.3", f) lint(f, linters = linters_with_defaults(line_length_linter = line_length_linter(120L))) +unlink(f) # the default linter list with a different line length cutoff my_linters <- linters_with_defaults(line_length_linter = line_length_linter(120L)) @@ -39,7 +40,7 @@ my_linters <- linters_with_defaults( # checking the included linters names(my_linters) -\dontshow{\}) # examplesIf} + } \seealso{ \itemize{ diff --git a/man/trailing_blank_lines_linter.Rd b/man/trailing_blank_lines_linter.Rd index 5cad661d2..29d7cc486 100644 --- a/man/trailing_blank_lines_linter.Rd +++ b/man/trailing_blank_lines_linter.Rd @@ -10,23 +10,25 @@ trailing_blank_lines_linter() Check that there are no trailing blank lines in source code. } \examples{ -\dontshow{if (requireNamespace("withr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # will produce lints -f <- withr::local_tempfile(lines = "x <- 1\n") -readLines(f) +f <- tempfile() +cat("x <- 1\n\n", file = f) +writeLines(readChar(f, file.size(f))) lint( filename = f, linters = trailing_blank_lines_linter() ) +unlink(f) # okay -f <- withr::local_tempfile(lines = "x <- 1") -readLines(f) +cat("x <- 1\n", file = f) +writeLines(readChar(f, file.size(f))) lint( filename = f, linters = trailing_blank_lines_linter() ) -\dontshow{\}) # examplesIf} +unlink(f) + } \seealso{ \link{linters} for a complete list of linters available in lintr.