diff --git a/DESCRIPTION b/DESCRIPTION index eec1beb54..e6c86e948 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,12 +34,13 @@ Suggests: mockery, rmarkdown, rstudioapi (>= 0.2), - testthat (>= 2.2.1), + testthat (>= 3.0.0), withr License: MIT + file LICENSE Encoding: UTF-8 VignetteBuilder: knitr RoxygenNote: 7.1.2 +Config/testthat/edition: 3 Collate: 'T_and_F_symbol_linter.R' 'utils.R' diff --git a/NEWS.md b/NEWS.md index d5a5e9ac9..7662f3745 100644 --- a/NEWS.md +++ b/NEWS.md @@ -85,6 +85,7 @@ function calls. (#850, #851, @renkun-ken) * Each linter can have multiple tags * New function `available_linters()` to list available linters and their tags This feature is extensible by package authors providing add-on linters for {lintr}. +* `lintr` now uses the 3rd edition of `testthat` (@MichaelChirico, #910) # lintr 2.0.1 diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 9d4693a01..ef1620d34 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -53,116 +53,96 @@ test_that("clear_cache deletes the directory if no file is given", { # `load_cache` test_that("load_cache loads the saved file in a new empty environment", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - assign("x", "foobar", envir = e1), - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", - save_cache(cache = e1, file = f1, path = d1), - e2 <- load_cache(file = f1, path = d1), - - expect_equal(ls(e2), "x"), - expect_equal(e2[["x"]], "foobar") - ) + e1 <- new.env(parent = emptyenv()) + e1[["x"]] <- "foobar" + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" + save_cache(cache = e1, file = f1, path = d1) + e2 <- load_cache(file = f1, path = d1) + + # TODO(michaelchirico): use plain expect_equal after waldo#133 makes it into a CRAN release + expect_equal(as.list(e2, all.names = TRUE), as.list(e1, all.names = TRUE)) }) test_that("load_cache returns an empty environment if no cache file exists", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", - f2 <- "test.R", + e1 <- new.env(parent = emptyenv()) + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" + f2 <- "test.R" - save_cache(cache = e1, file = f1, path = d1), - e2 <- load_cache(file = f2, path = d1), + save_cache(cache = e1, file = f1, path = d1) + e2 <- load_cache(file = f2, path = d1) - expect_equal(ls(e2), character(0)) - ) + # TODO(michaelchirico): use plain expect_equal after waldo#133 makes it into a CRAN release + expect_equal(as.list(e2, all.names = TRUE), as.list(e1, all.names = TRUE)) }) test_that("load_cache returns an empty environment if reading cache file fails", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - assign("x", "foobar", envir = e1), - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", - save_cache(cache = e1, file = f1, path = d1), - cache_f1 <- file.path(d1, fhash(f1)), - writeLines(character(), cache_f1), - expect_warning(e2 <- load_cache(file = f1, path = d1)), - saveRDS(e1, cache_f1), - expect_warning(e3 <- load_cache(file = f1, path = d1)), - expect_equal(ls(e2), character(0)), - expect_equal(ls(e3), character(0)) - ) + e1 <- new.env(parent = emptyenv()) + e1[["x"]] <- "foobar" + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" + save_cache(cache = e1, file = f1, path = d1) + cache_f1 <- file.path(d1, fhash(f1)) + writeLines(character(), cache_f1) + + expect_warning(e2 <- load_cache(file = f1, path = d1)) + saveRDS(e1, cache_f1) + expect_warning(e3 <- load_cache(file = f1, path = d1)) + + expect_equal(ls(e2), character(0)) + expect_equal(ls(e3), character(0)) }) # `save_cache` test_that("save_cache creates a directory if needed", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", + e1 <- new.env(parent = emptyenv()) + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" - expect_false(file.exists(d1)), - expect_false(file.exists(file.path(d1, fhash(f1)))), + expect_false(file.exists(d1)) + expect_false(file.exists(file.path(d1, fhash(f1)))) - save_cache(cache = e1, file = f1, path = d1), + save_cache(cache = e1, file = f1, path = d1) - expect_true(file.exists(d1)), - expect_true(file.info(d1)$isdir), - expect_true(file.exists(file.path(d1, fhash(f1)))) - ) + expect_true(file.exists(d1)) + expect_true(file.info(d1)$isdir) + expect_true(file.exists(file.path(d1, fhash(f1)))) }) test_that("save_cache uses unambiguous cache file names", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", - f2 <- "test.R", + e1 <- new.env(parent = emptyenv()) + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" + f2 <- "test.R" - expect_false(file.exists(file.path(d1, fhash(f1)))), - expect_false(file.exists(file.path(d1, fhash(f2)))), + expect_false(file.exists(file.path(d1, fhash(f1)))) + expect_false(file.exists(file.path(d1, fhash(f2)))) - save_cache(cache = e1, file = f1, path = d1), - save_cache(cache = e1, file = f2, path = d1), + save_cache(cache = e1, file = f1, path = d1) + save_cache(cache = e1, file = f2, path = d1) - expect_true(fhash(f1) != fhash(f2)), - expect_true(file.exists(file.path(d1, fhash(f1)))), - expect_true(file.exists(file.path(d1, fhash(f2)))) - ) + expect_true(fhash(f1) != fhash(f2)) + expect_true(file.exists(file.path(d1, fhash(f1)))) + expect_true(file.exists(file.path(d1, fhash(f2)))) }) test_that("save_cache saves all non-hidden objects from the environment", { - with_mock( - `lintr::read_settings` = function(...) invisible(...), - - e1 <- new.env(parent = emptyenv()), - e1$t1 <- 1, - e1$t2 <- 2, + e1 <- new.env(parent = emptyenv()) + e1$t1 <- 1 + e1$t2 <- 2 - d1 <- tempfile(pattern = "lintr_cache_"), - f1 <- "R/test.R", + d1 <- tempfile(pattern = "lintr_cache_") + f1 <- "R/test.R" - save_cache(cache = e1, file = f1, path = d1), + save_cache(cache = e1, file = f1, path = d1) - e2 <- new.env(parent = emptyenv()), - load(file = file.path(d1, fhash(f1)), envir = e2), + e2 <- new.env(parent = emptyenv()) + load(file = file.path(d1, fhash(f1)), envir = e2) - expect_equal(e1, e2) - ) + # TODO(michaelchirico): use plain expect_equal after waldo#133 makes it into a CRAN release + expect_equal(as.list(e1, all.names = TRUE), as.list(e2, all.names = TRUE)) }) # `cache_file` @@ -177,7 +157,7 @@ test_that("cache_file generates the same cache with different lints", { cache_file(e1, f1, list(), list()) cache_file(e1, f1, list(), list(1)) - expect_equal(length(ls(e1)), 1) + expect_length(ls(e1), 1L) }) test_that("cache_file generates different caches for different linters", { @@ -190,7 +170,7 @@ test_that("cache_file generates different caches for different linters", { cache_file(e1, f1, list(), list()) cache_file(e1, f1, list(1), list()) - expect_equal(length(ls(e1)), 2) + expect_length(ls(e1), 2L) }) # `retrieve_file` @@ -225,7 +205,7 @@ test_that("cache_lint generates the same cache with different lints", { cache_lint(e1, t1, list(), list()) cache_lint(e1, t1, list(), list(1)) - expect_equal(length(ls(e1)), 1) + expect_length(ls(e1), 1L) }) test_that("cache_lint generates different caches for different linters", { @@ -236,7 +216,7 @@ test_that("cache_lint generates different caches for different linters", { cache_lint(e1, t1, list(), list()) cache_lint(e1, t1, list(1), list()) - expect_equal(length(ls(e1)), 2) + expect_length(ls(e1), 2L) }) # `retrieve_lint` @@ -435,18 +415,14 @@ test_that("lint with cache uses the provided relative cache directory", { test_that("it works outside of a package", { linter <- assignment_linter() - with_mock( - `lintr::find_package` = function(...) NULL, - `lintr::pkg_name` = function(...) NULL, - - path <- tempfile(pattern = "my_cache_dir_"), - expect_false(dir.exists(path)), - expect_lint("a <- 1", NULL, linter, cache = path), - expect_true(dir.exists(path)), - expect_length(list.files(path), 1), - expect_lint("a <- 1", NULL, linter, cache = path), - expect_true(dir.exists(path)) - ) + mockery::stub(lintr:::find_default_encoding, "find_package", function(...) NULL) + path <- tempfile(pattern = "my_cache_dir_") + expect_false(dir.exists(path)) + expect_lint("a <- 1", NULL, linter, cache = path) + expect_true(dir.exists(path)) + expect_length(list.files(path), 1L) + expect_lint("a <- 1", NULL, linter, cache = path) + expect_true(dir.exists(path)) }) test_that("cache = TRUE workflow works", { diff --git a/tests/testthat/test-ci.R b/tests/testthat/test-ci.R index 9eb1117a4..de13b25c6 100644 --- a/tests/testthat/test-ci.R +++ b/tests/testthat/test-ci.R @@ -1,67 +1,55 @@ test_that("GitHub Actions functionality works", { # imitate being on GHA whether or not we are - withr::with_envvar(c(GITHUB_ACTIONS = "true"), { - old <- options(lintr.rstudio_source_markers = FALSE) - on.exit(options(old), add = TRUE) + withr::local_envvar(list(GITHUB_ACTIONS = "true")) + withr::local_options(lintr.rstudio_source_markers = FALSE) + tmp <- withr::local_tempfile() - writeLines("x <- 1:nrow(y)", tmp <- tempfile()) - on.exit(unlink(tmp)) + writeLines("x <- 1:nrow(y)", tmp) - l <- lint(tmp) - expect_output(print(l), "::warning file", fixed = TRUE) - }) + l <- lint(tmp) + expect_output(print(l), "::warning file", fixed = TRUE) }) test_that("GitHub Actions functionality works in a subdirectory", { # imitate being on GHA whether or not we are pkg_path <- file.path("dummy_packages", "assignmentLinter") - withr::with_envvar(c(GITHUB_ACTIONS = "true"), { - old <- options( - lintr.rstudio_source_markers = FALSE, - lintr.github_annotation_project_dir = pkg_path - ) - on.exit(options(old), add = TRUE) - - read_settings(NULL) - l <- lint_package( - pkg_path, linters = list(assignment_linter()), - parse_settings = FALSE - ) - expect_output( - print(l), - paste0("::warning file=", file.path(pkg_path, "R(/|\\\\)abc\\.R")) - ) - }) + withr::local_envvar(list(GITHUB_ACTIONS = "true")) + withr::local_options(lintr.rstudio_source_markers = FALSE, lintr.github_annotation_project_dir = pkg_path) + + read_settings(NULL) + l <- lint_package( + pkg_path, + linters = list(assignment_linter()), + parse_settings = FALSE + ) + expect_output( + print(l), + paste0("::warning file=", file.path(pkg_path, "R(/|\\\\)abc\\.R")) + ) }) test_that("Printing works for Travis", { - withr::with_envvar(c(GITHUB_ACTIONS = "false", TRAVIS_REPO_SLUG = "test/repo", LINTR_COMMENT_BOT = "true"), { - old <- options(lintr.rstudio_source_markers = FALSE) - on.exit(options(old), add = TRUE) + withr::local_envvar(list(GITHUB_ACTIONS = "false", TRAVIS_REPO_SLUG = "test/repo", LINTR_COMMENT_BOT = "true")) + withr::local_options(lintr.rstudio_source_markers = FALSE) + tmp <- withr::local_tempfile() + writeLines("x <- 1:nrow(y)", tmp) - writeLines("x <- 1:nrow(y)", tmp <- tempfile()) - on.exit(unlink(tmp)) + l <- lint(tmp) - l <- lint(tmp) - - with_mock(github_comment = function(x, ...) cat(x, "\n"), .env = asNamespace("lintr"), { - expect_output(print(l), "*warning:*", fixed = TRUE) - }) + with_mock(github_comment = function(x, ...) cat(x, "\n"), .env = asNamespace("lintr"), { + expect_output(print(l), "*warning:*", fixed = TRUE) }) }) test_that("Printing works for Wercker", { - withr::with_envvar(c(GITHUB_ACTIONS = "false", WERCKER_GIT_BRANCH = "test/repo", LINTR_COMMENT_BOT = "true"), { - old <- options(lintr.rstudio_source_markers = FALSE) - on.exit(options(old), add = TRUE) - - writeLines("x <- 1:nrow(y)", tmp <- tempfile()) - on.exit(unlink(tmp)) + withr::local_envvar(list(GITHUB_ACTIONS = "false", WERCKER_GIT_BRANCH = "test/repo", LINTR_COMMENT_BOT = "true")) + withr::local_options(lintr.rstudio_source_markers = FALSE) + tmp <- withr::local_tempfile() + writeLines("x <- 1:nrow(y)", tmp) - l <- lint(tmp) + l <- lint(tmp) - with_mock(github_comment = function(x, ...) cat(x, "\n"), .env = asNamespace("lintr"), { - expect_output(print(l), "*warning:*", fixed = TRUE) - }) + with_mock(github_comment = function(x, ...) cat(x, "\n"), .env = asNamespace("lintr"), { + expect_output(print(l), "*warning:*", fixed = TRUE) }) }) diff --git a/tests/testthat/test-commented_code_linter.R b/tests/testthat/test-commented_code_linter.R index 9eb40b307..ab13ed83b 100644 --- a/tests/testthat/test-commented_code_linter.R +++ b/tests/testthat/test-commented_code_linter.R @@ -1,7 +1,7 @@ test_that("returns the correct linting", { msg <- rex("Commented code should be removed.") linter <- commented_code_linter() - expect_is(linter, "linter") + expect_s3_class(linter, "linter") expect_lint("blah", NULL, linter) diff --git a/tests/testthat/test-defaults.R b/tests/testthat/test-defaults.R index eaf9257d4..422508d16 100644 --- a/tests/testthat/test-defaults.R +++ b/tests/testthat/test-defaults.R @@ -1,7 +1,7 @@ test_that("linters", { # non-empty named list of functions x <- default_linters - expect_is(x, "list") + expect_type(x, "list") expect_gt(length(x), 0L) expect_true(all(names(x) != "")) expect_true(all(vapply(x, inherits, logical(1L), "linter"))) @@ -14,7 +14,7 @@ test_that("undesirable functions and operators", { all_undesirable_operators, default_undesirable_operators) for (x in vars) { - expect_is(x, "list") + expect_type(x, "list") expect_gt(length(x), 0L) expect_true(all(names(x) != "")) expect_true(all(vapply(x, function(x) is.na(x) || is.character(x), logical(1L)))) @@ -25,7 +25,7 @@ test_that("undesirable functions and operators", { test_that("settings", { # non-empty named list x <- default_settings - expect_is(x, "list") + expect_type(x, "list") expect_gt(length(x), 0L) expect_true(all(names(x) != "")) }) diff --git a/tests/testthat/test-get_source_expressions.R b/tests/testthat/test-get_source_expressions.R index 53e390d90..bb8749ce2 100644 --- a/tests/testthat/test-get_source_expressions.R +++ b/tests/testthat/test-get_source_expressions.R @@ -18,45 +18,48 @@ test_that("tab positions have been corrected", { ) with_content_to_parse("TRUE", - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], c(1L, 4L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], use.names = FALSE), c(1L, 4L)) ) with_content_to_parse("\tTRUE", - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], c(2L, 5L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], use.names = FALSE), c(2L, 5L)) ) with_content_to_parse("\t\tTRUE", - expect_equivalent(pc[[1]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], c(3L, 6L)) + expect_identical(unlist(pc[[1]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], use.names = FALSE), c(3L, 6L)) ) with_content_to_parse("x\t<-\tTRUE", { - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "x", c("col1", "col2")], c(1L, 1L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "<-", c("col1", "col2")], c(3L, 4L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], c(6L, 9L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "x", c("col1", "col2")], use.names = FALSE), c(1L, 1L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "<-", c("col1", "col2")], use.names = FALSE), c(3L, 4L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "TRUE", c("col1", "col2")], use.names = FALSE), c(6L, 9L)) }) with_content_to_parse("\tfunction\t(x)\t{\tprint(pc[\t,1])\t;\t}", { - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "function", c("col1", "col2")], c(2L, 9L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "x", c("col1", "col2")], c(12L, 12L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "print", c("col1", "col2")], c(17L, 21L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == ";", c("col1", "col2")], c(32L, 32L)) - expect_equivalent(pc[[1L]][pc[[1L]][["text"]] == "}", c("col1", "col2")], c(34L, 34L)) + expect_identical( + unlist(pc[[1L]][pc[[1L]][["text"]] == "function", c("col1", "col2")], use.names = FALSE), + c(2L, 9L) + ) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "x", c("col1", "col2")], use.names = FALSE), c(12L, 12L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "print", c("col1", "col2")], use.names = FALSE), c(17L, 21L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == ";", c("col1", "col2")], use.names = FALSE), c(32L, 32L)) + expect_identical(unlist(pc[[1L]][pc[[1L]][["text"]] == "}", c("col1", "col2")], use.names = FALSE), c(34L, 34L)) }) with_content_to_parse("# test tab\n\ns <- 'I have \\t a dog'\nrep(\ts, \t3)", { - expect_equivalent( - pc[[2L]][pc[[2L]][["text"]] == "'I have \\t a dog'", c("line1", "col1", "col2")], + expect_identical( + unlist(pc[[2L]][pc[[2L]][["text"]] == "'I have \\t a dog'", c("line1", "col1", "col2")], use.names = FALSE), c(3L, 6L, 22L) ) - expect_equivalent( - pc[[3L]][pc[[3L]][["text"]] == "3", c("line1", "col1", "col2")], + expect_identical( + unlist(pc[[3L]][pc[[3L]][["text"]] == "3", c("line1", "col1", "col2")], use.names = FALSE), c(4L, 10L, 10L) ) }) with_content_to_parse("function(){\nTRUE\n\t}", { - expect_equivalent( - pc[[1L]][1L, c("line1", "col1", "line2", "col2")], + expect_identical( + unlist(pc[[1L]][1L, c("line1", "col1", "line2", "col2")], use.names = FALSE), c(1L, 1L, 3L, 2L), info = "expression that spans several lines" ) diff --git a/tests/testthat/test-implicit_integer_linter.R b/tests/testthat/test-implicit_integer_linter.R index ce586281a..bab09465b 100644 --- a/tests/testthat/test-implicit_integer_linter.R +++ b/tests/testthat/test-implicit_integer_linter.R @@ -36,7 +36,7 @@ test_that("single numerical constants are properly identified ", { test_that("linter returns the correct linting", { msg <- "Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles." linter <- implicit_integer_linter() - expect_is(linter, "linter") + expect_s3_class(linter, "linter") expect_lint("x <<- 1L", NULL, linter) expect_lint("1.0/-Inf -> y", NULL, linter) diff --git a/tests/testthat/test-knitr_formats.R b/tests/testthat/test-knitr_formats.R index 7505e5f73..4cbc51a41 100644 --- a/tests/testthat/test-knitr_formats.R +++ b/tests/testthat/test-knitr_formats.R @@ -12,9 +12,9 @@ test_that("it handles dir", { parse_settings = FALSE ) has_lints <- length(lints) > 0 - testthat::expect(has_lints, "There should be lints") + expect_true(has_lints, info = "There should be lints") - testthat::expect_equivalent(length(unique(names(lints))), 6, info="For every file there should be at least 1 lint") + expect_identical(length(unique(names(lints))), 6L, info = "For every file there should be at least 1 lint") }) test_that("it handles markdown", { diff --git a/tests/testthat/test-lint_file.R b/tests/testthat/test-lint_file.R index fc902b7cc..3b45e8620 100644 --- a/tests/testthat/test-lint_file.R +++ b/tests/testthat/test-lint_file.R @@ -186,7 +186,8 @@ test_that("compatibility warnings work", { "Use is.na", linters = list(unclass(equals_na_linter())) ), - fixed = "The use of linters of class 'function'" + "The use of linters of class 'function'", + fixed = TRUE ) expect_error( diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index d1132b7c8..fc929bfcf 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -21,7 +21,7 @@ test_that("it returns the input trimmed to the last full lint if one exists with test_that("as.data.frame.lints", { # A minimum lint - expect_is( + expect_s3_class( l1 <- Lint( "dummy.R", line_number = 1L, @@ -33,7 +33,7 @@ test_that("as.data.frame.lints", { ) # A larger lint - expect_is( + expect_s3_class( l2 <- Lint( "dummy.R", line_number = 2L, @@ -53,7 +53,7 @@ test_that("as.data.frame.lints", { # Convert lints to data.frame lints <- structure(list(l1, l2), class = "lints") - expect_is( + expect_s3_class( df <- as.data.frame.lints(lints), "data.frame" ) diff --git a/tests/testthat/test-object_name_linter.R b/tests/testthat/test-object_name_linter.R index ed41f4861..fdf05de94 100644 --- a/tests/testthat/test-object_name_linter.R +++ b/tests/testthat/test-object_name_linter.R @@ -1,39 +1,39 @@ test_that("styles are correctly identified", { styles <- names(style_regexes) do_style_check <- function(nms) lapply(styles, check_style, nms = nms) - # symbl UpC lowC snake SNAKE dot alllow ALLUP - expect_equivalent(do_style_check("x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) - expect_equivalent(do_style_check(".x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) - expect_equivalent(do_style_check("X"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) - expect_equivalent(do_style_check("x."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("X."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("x_"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("X_"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("xy"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) - expect_equivalent(do_style_check("xY"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("Xy"), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("XY"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) - expect_equivalent(do_style_check("x1"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) - expect_equivalent(do_style_check("X1"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) - expect_equivalent(do_style_check("x_y"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("X_Y"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("X.Y"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("x_2"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("X_2"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("x.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)) - expect_equivalent(do_style_check("X.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - - # symbl UpC lowC snake SNAKE dot alllow ALLUP - expect_equivalent(do_style_check("IHave1Cat"), c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("iHave1Cat"), c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("i_have_1_cat"), c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("I_HAVE_1_CAT"), c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("i.have.1.cat"), c(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)) - expect_equivalent(do_style_check("ihave1cat"), c(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) - expect_equivalent(do_style_check("IHAVE1CAT"), c(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) - expect_equivalent(do_style_check("I.HAVE_ONECAT"), c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("."), c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) - expect_equivalent(do_style_check("%^%"), c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + # symbl UpC lowC snake SNAKE dot allow ALLUP + expect_identical(do_style_check("x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) + expect_identical(do_style_check(".x"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) + expect_identical(do_style_check("X"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) + expect_identical(do_style_check("x."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("X."), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("x_"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("X_"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("xy"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) + expect_identical(do_style_check("xY"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("Xy"), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("XY"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) + expect_identical(do_style_check("x1"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) + expect_identical(do_style_check("X1"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) + expect_identical(do_style_check("x_y"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("X_Y"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("X.Y"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("x_2"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("X_2"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("x.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)) + expect_identical(do_style_check("X.2"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + + # symbl UpC lowC snake SNAKE dot alllow ALLUP + expect_identical(do_style_check("IHave1Cat"), list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("iHave1Cat"), list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("i_have_1_cat"), list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("I_HAVE_1_CAT"), list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("i.have.1.cat"), list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)) + expect_identical(do_style_check("ihave1cat"), list(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)) + expect_identical(do_style_check("IHAVE1CAT"), list(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)) + expect_identical(do_style_check("I.HAVE_ONECAT"), list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("."), list(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) + expect_identical(do_style_check("%^%"), list(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) }) test_that("linter ignores some objects", { diff --git a/tests/testthat/test-rstudio_markers.R b/tests/testthat/test-rstudio_markers.R index 2270f419d..c75889d3f 100644 --- a/tests/testthat/test-rstudio_markers.R +++ b/tests/testthat/test-rstudio_markers.R @@ -1,4 +1,7 @@ test_that("it returns markers which match lints", { + mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) + mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + lint1 <- structure( list( Lint(filename = "test_file", @@ -10,11 +13,8 @@ test_that("it returns markers which match lints", { ), class = "lints" ) - with_mock( - `rstudioapi::callFun` = function(...) list(...), - `rstudioapi::executeCommand` = function(...) NULL, - marker1 <- rstudio_source_markers(lint1) - ) + + marker1 <- rstudio_source_markers(lint1) expect_equal(marker1$name, "lintr") expect_equal(marker1$markers[[1]]$type, lint1[[1]]$type) expect_equal(marker1$markers[[1]]$file, lint1[[1]]$filename) @@ -38,11 +38,7 @@ test_that("it returns markers which match lints", { ), class = "lints" ) - with_mock( - `rstudioapi::callFun` = function(...) list(...), - `rstudioapi::executeCommand` = function(...) NULL, - marker2 <- rstudio_source_markers(lint2) - ) + marker2 <- rstudio_source_markers(lint2) expect_equal(marker2$name, "lintr") expect_equal(marker2$markers[[1]]$type, lint2[[1]]$type) expect_equal(marker2$markers[[1]]$file, lint2[[1]]$filename) @@ -52,6 +48,9 @@ test_that("it returns markers which match lints", { }) test_that("it prepends the package path if it exists", { + mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) + mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + lint3 <- structure( list( Lint(filename = "test_file", @@ -64,11 +63,7 @@ test_that("it prepends the package path if it exists", { class = "lints", path = "test" ) - with_mock( - `rstudioapi::callFun` = function(...) list(...), - `rstudioapi::executeCommand` = function(...) NULL, - marker3 <- rstudio_source_markers(lint3) - ) + marker3 <- rstudio_source_markers(lint3) expect_equal(marker3$name, "lintr") expect_equal(marker3$basePath, "test") expect_equal(marker3$markers[[1]]$type, lint3[[1]]$type) @@ -79,40 +74,32 @@ test_that("it prepends the package path if it exists", { }) test_that("it returns an empty list of markers if there are no lints", { + mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) + mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + lint4 <- structure( list(), class = "lints" ) - with_mock( - `rstudioapi::callFun` = function(...) list(...), - `rstudioapi::executeCommand` = function(...) NULL, - marker4 <- rstudio_source_markers(lint4) - ) + marker4 <- rstudio_source_markers(lint4) expect_equal(marker4$name, "lintr") expect_equal(marker4$markers, list()) }) test_that("rstudio_source_markers apply to print within rstudio", { + withr::local_options(lintr.rstudio_source_markers = TRUE) + tmp <- withr::local_tempfile(lines = "1:ncol(x)") + empty <- withr::local_tempfile() + file.create(empty) + with_mock( `rstudioapi::hasFun` = function(x, ...) TRUE, `rstudioapi::callFun` = function(...) cat("matched\n"), { - writeLines("1:ncol(x)", tmp <- tempfile()) - on.exit(unlink(tmp)) - - old <- options(lintr.rstudio_source_markers = TRUE) - on.exit(options(old), add = TRUE) + l <- lint(tmp, seq_linter()) + expect_output(print(l), "matched", fixed = TRUE) - l <- lint(tmp, seq_linter()) + l <- lint(empty, seq_linter()) - expect_output(print(l), "matched", fixed = TRUE) - - empty <- tempfile() - file.create(empty) - on.exit(unlink(empty), add = TRUE) - - l <- lint(empty, seq_linter()) - - expect_output(print(l), "matched", fixed = TRUE) - } - ) + expect_output(print(l), "matched", fixed = TRUE) + }) })