Skip to content

Commit

Permalink
Remove local_tempfile() usage in examples following breakage by withr…
Browse files Browse the repository at this point in the history
… 3.0.0 (#2519)

* debugging

* dont use withr in examples

* kwarg for cat()
  • Loading branch information
MichaelChirico authored Jan 27, 2024
1 parent bad1632 commit 901d9ed
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 4 additions & 2 deletions R/get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions R/ids_with_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions R/is_lint_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
6 changes: 4 additions & 2 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions R/trailing_blank_lines_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 6 additions & 4 deletions man/get_r_string.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/get_source_expressions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/ids_with_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/is_lint_level.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/lint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/linters_with_defaults.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions man/trailing_blank_lines_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 901d9ed

Please sign in to comment.