From 12285684e7089ae87b2271505f2d81d18b22c3af Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 08:41:07 +0100 Subject: [PATCH 1/8] start with linters_with_tags test --- tests/testthat/test-with.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-with.R b/tests/testthat/test-with.R index 6b4fc00a8..0e9a7f7e6 100644 --- a/tests/testthat/test-with.R +++ b/tests/testthat/test-with.R @@ -22,11 +22,15 @@ test_that("linters_with_defaults warns on unused NULLs", { }) test_that("linters_with_tags() verifies the output of available_linters()", { - skip_if_not_installed("mockery") - mockery::stub( - linters_with_tags, - "available_linters", - data.frame(linter = c("fake_linter", "very_fake_linter"), package = "lintr", tags = "", stringsAsFactors = FALSE) + local_mocked_bindings( + available_linters = function(...) { + data.frame( + linter = c("fake_linter", "very_fake_linter"), + package = "lintr", + tags = "", + stringsAsFactors = FALSE + ) + } ) expect_error( linters_with_tags(NULL), From d4eac88757a6e558958be7c1ab89d896e3dfe45b Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 08:49:02 +0100 Subject: [PATCH 2/8] bump testthat --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 48c2b13b3..ca0aa2799 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,7 +44,7 @@ Suggests: rlang, rmarkdown, rstudioapi (>= 0.2), - testthat (>= 3.1.5), + testthat (>= 3.2.1), tibble, tufte, withr (>= 2.5.0) From 4d4962e21a731f4286203ffa32041aac66a9511a Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 19:11:34 +0100 Subject: [PATCH 3/8] use testthat mock tests for cache --- R/lintr-package.R | 4 ++++ tests/testthat/test-cache.R | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/R/lintr-package.R b/R/lintr-package.R index 3bc35f703..3305e5901 100644 --- a/R/lintr-package.R +++ b/R/lintr-package.R @@ -24,3 +24,7 @@ #' } ## lintr namespace: end NULL + +# make binding available for mock testing +# ref: https://testthat.r-lib.org/dev/reference/local_mocked_bindings.html#base-functions +unlink <- NULL diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 71bf9e6b6..792786495 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -34,10 +34,10 @@ fhash <- function(filename) { # `clear_cache` test_that("clear_cache deletes the file if a file is given", { - skip_if_not_installed("mockery") - - mockery::stub(clear_cache, "read_settings", function(...) invisible(...)) - mockery::stub(clear_cache, "unlink", function(...) list(...)) + local_mocked_bindings( + read_settings = function(...) invisible(...), + unlink = function(...) list(...) + ) e1 <- new.env(parent = emptyenv()) d1 <- withr::local_tempfile(pattern = "lintr_cache_") @@ -50,10 +50,10 @@ test_that("clear_cache deletes the file if a file is given", { }) test_that("clear_cache deletes the directory if no file is given", { - skip_if_not_installed("mockery") - - mockery::stub(clear_cache, "read_settings", function(...) invisible(...)) - mockery::stub(clear_cache, "unlink", function(...) list(...)) + local_mocked_bindings( + read_settings = function(...) invisible(...), + unlink = function(...) list(...) + ) expect_identical(clear_cache(file = NULL, path = "."), list(".", recursive = TRUE)) }) From cdaf688c8b3b8fc28b4fcac33bda91de8ecb78b4 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 19:19:03 +0100 Subject: [PATCH 4/8] all but namespaced functions covered --- R/lintr-package.R | 1 + tests/testthat/test-cache.R | 3 +-- tests/testthat/test-ci.R | 12 +++--------- tests/testthat/test-rstudio_markers.R | 2 +- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/R/lintr-package.R b/R/lintr-package.R index 3305e5901..898db1445 100644 --- a/R/lintr-package.R +++ b/R/lintr-package.R @@ -28,3 +28,4 @@ NULL # make binding available for mock testing # ref: https://testthat.r-lib.org/dev/reference/local_mocked_bindings.html#base-functions unlink <- NULL +quit <- NULL diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 792786495..6b0c47aa1 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -421,10 +421,9 @@ test_that("lint with cache uses the provided relative cache directory", { }) test_that("it works outside of a package", { - skip_if_not_installed("mockery") linter <- assignment_linter() - mockery::stub(lintr:::find_default_encoding, "find_package", function(...) NULL) + local_mocked_bindings(find_package = function(...) NULL) path <- withr::local_tempfile(pattern = "my_cache_dir_") expect_false(dir.exists(path)) expect_lint("a <- 1", NULL, linter, cache = path) diff --git a/tests/testthat/test-ci.R b/tests/testthat/test-ci.R index 6da47a84f..8bab65609 100644 --- a/tests/testthat/test-ci.R +++ b/tests/testthat/test-ci.R @@ -27,8 +27,6 @@ test_that("GitHub Actions functionality works in a subdirectory", { }) test_that("GitHub Actions - linting on error works", { - skip_if_not_installed("mockery") - # imitate being on GHA whether or not we are withr::local_envvar(list(GITHUB_ACTIONS = "true", LINTR_ERROR_ON_LINT = "true")) withr::local_options(lintr.rstudio_source_markers = FALSE) @@ -36,32 +34,28 @@ test_that("GitHub Actions - linting on error works", { l <- lint(tmp) - mockery::stub(print.lints, "base::quit", function(...) cat("Tried to quit.\n")) + local_mocked_bindings(quit = function(...) cat("Tried to quit.\n")) expect_output(print(l), "::warning file", fixed = TRUE) }) test_that("Printing works for Travis", { - skip_if_not_installed("mockery") - 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(lines = "x <- 1:nrow(y)") l <- lint(tmp) - mockery::stub(print.lints, "github_comment", function(x, ...) cat(x, "\n")) + local_mocked_bindings(github_comment = function(x, ...) cat(x, "\n")) expect_output(print(l), "*warning:*", fixed = TRUE) }) test_that("Printing works for Wercker", { - skip_if_not_installed("mockery") - 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(lines = "x <- 1:nrow(y)") l <- lint(tmp) - mockery::stub(print.lints, "github_comment", function(x, ...) cat(x, "\n")) + local_mocked_bindings(github_comment = function(x, ...) cat(x, "\n")) expect_output(print(l), "*warning:*", fixed = TRUE) }) diff --git a/tests/testthat/test-rstudio_markers.R b/tests/testthat/test-rstudio_markers.R index 203e87a41..3a9fb2aef 100644 --- a/tests/testthat/test-rstudio_markers.R +++ b/tests/testthat/test-rstudio_markers.R @@ -100,7 +100,7 @@ test_that("rstudio_source_markers apply to print within rstudio", { empty <- withr::local_tempfile(lines = character(0L)) mockery::stub(print.lints, "rstudioapi::hasFun", function(x, ...) TRUE) - mockery::stub(print.lints, "rstudio_source_markers", function(x) cat("matched\n")) + local_mocked_bindings(rstudio_source_markers = function(x) cat("matched\n")) l <- lint(tmp, seq_linter()) expect_output(print(l), "matched", fixed = TRUE) From c7abb002a324e7c79184b33d7715518bfc2deeb6 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 19:27:54 +0100 Subject: [PATCH 5/8] mock namespaced functions --- DESCRIPTION | 1 - tests/testthat/test-methods.R | 7 +++--- tests/testthat/test-rstudio_markers.R | 34 +++++++++++++++------------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ca0aa2799..792453586 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,7 +39,6 @@ Suggests: cli, httr (>= 1.2.1), jsonlite, - mockery, patrick, rlang, rmarkdown, diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 0d003c597..2b4c6163a 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -98,8 +98,6 @@ test_that("print.lint works", { }) test_that("print.lint works for inline data, even in RStudio", { - skip_if_not_installed("mockery") - l <- lint("x = 1\n") # Make sure lints print to console. @@ -114,7 +112,10 @@ test_that("print.lint works for inline data, even in RStudio", { expect_output(print(l), "not =") ) - mockery::stub(print.lints, "rstudioapi::hasFun", function(...) FALSE) + local_mocked_bindings( + hasFun = function(x, ...) FALSE, + .package = "rstudioapi" + ) withr::with_options( list(lintr.rstudio_source_markers = TRUE), expect_output(print(l), "not =") diff --git a/tests/testthat/test-rstudio_markers.R b/tests/testthat/test-rstudio_markers.R index 3a9fb2aef..bc946143e 100644 --- a/tests/testthat/test-rstudio_markers.R +++ b/tests/testthat/test-rstudio_markers.R @@ -1,8 +1,9 @@ test_that("it returns markers which match lints", { - skip_if_not_installed("mockery") - - mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) - mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + local_mocked_bindings( + callFun = function(...) list(...), + executeCommand = function(...) NULL, + .package = "rstudioapi" + ) lint1 <- list(Lint( filename = "test_file", @@ -53,10 +54,11 @@ test_that("it returns markers which match lints", { }) test_that("it prepends the package path if it exists", { - skip_if_not_installed("mockery") - - mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) - mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + local_mocked_bindings( + callFun = function(...) list(...), + executeCommand = function(...) NULL, + .package = "rstudioapi" + ) lint3 <- list(Lint( filename = "test_file", @@ -80,10 +82,11 @@ test_that("it prepends the package path if it exists", { }) test_that("it returns an empty list of markers if there are no lints", { - skip_if_not_installed("mockery") - - mockery::stub(rstudio_source_markers, "rstudioapi::callFun", function(...) list(...)) - mockery::stub(rstudio_source_markers, "rstudioapi::executeCommand", function(...) NULL) + local_mocked_bindings( + callFun = function(...) list(...), + executeCommand = function(...) NULL, + .package = "rstudioapi" + ) lint4 <- `class<-`(list(), "lints") marker4 <- rstudio_source_markers(lint4) @@ -92,14 +95,15 @@ test_that("it returns an empty list of markers if there are no lints", { }) test_that("rstudio_source_markers apply to print within rstudio", { - skip_if_not_installed("mockery") - withr::local_options(lintr.rstudio_source_markers = TRUE) tmp <- withr::local_tempfile(lines = "1:ncol(x)") empty <- withr::local_tempfile(lines = character(0L)) - mockery::stub(print.lints, "rstudioapi::hasFun", function(x, ...) TRUE) + local_mocked_bindings( + hasFun = function(x, ...) TRUE, + .package = "rstudioapi" + ) local_mocked_bindings(rstudio_source_markers = function(x) cat("matched\n")) l <- lint(tmp, seq_linter()) From b19949bda5e78ce62245117084959f4a47f1f983 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 5 Dec 2023 19:38:31 +0100 Subject: [PATCH 6/8] fix hard dep check --- tests/testthat/test-methods.R | 1 + tests/testthat/test-rstudio_markers.R | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 2b4c6163a..8a8b87dd3 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -112,6 +112,7 @@ test_that("print.lint works for inline data, even in RStudio", { expect_output(print(l), "not =") ) + skip_if_not_installed("rstudioapi") local_mocked_bindings( hasFun = function(x, ...) FALSE, .package = "rstudioapi" diff --git a/tests/testthat/test-rstudio_markers.R b/tests/testthat/test-rstudio_markers.R index bc946143e..3db103f02 100644 --- a/tests/testthat/test-rstudio_markers.R +++ b/tests/testthat/test-rstudio_markers.R @@ -1,4 +1,5 @@ test_that("it returns markers which match lints", { + skip_if_not_installed("rstudioapi") local_mocked_bindings( callFun = function(...) list(...), executeCommand = function(...) NULL, @@ -54,6 +55,7 @@ test_that("it returns markers which match lints", { }) test_that("it prepends the package path if it exists", { + skip_if_not_installed("rstudioapi") local_mocked_bindings( callFun = function(...) list(...), executeCommand = function(...) NULL, @@ -82,6 +84,7 @@ test_that("it prepends the package path if it exists", { }) test_that("it returns an empty list of markers if there are no lints", { + skip_if_not_installed("rstudioapi") local_mocked_bindings( callFun = function(...) list(...), executeCommand = function(...) NULL, @@ -100,6 +103,7 @@ test_that("rstudio_source_markers apply to print within rstudio", { tmp <- withr::local_tempfile(lines = "1:ncol(x)") empty <- withr::local_tempfile(lines = character(0L)) + skip_if_not_installed("rstudioapi") local_mocked_bindings( hasFun = function(x, ...) TRUE, .package = "rstudioapi" From 6898a90d3a05ccd646c75b9306ea9338fd9a5a58 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Wed, 6 Dec 2023 02:21:40 +0100 Subject: [PATCH 7/8] correct signautre --- tests/testthat/test-methods.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 8a8b87dd3..9f454c1f4 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -114,7 +114,7 @@ test_that("print.lint works for inline data, even in RStudio", { skip_if_not_installed("rstudioapi") local_mocked_bindings( - hasFun = function(x, ...) FALSE, + hasFun = function(...) FALSE, .package = "rstudioapi" ) withr::with_options( From 9c6f9c86ef473276183ed6694f7dbdebf004f326 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 5 Dec 2023 17:37:28 -0800 Subject: [PATCH 8/8] second site --- tests/testthat/test-rstudio_markers.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-rstudio_markers.R b/tests/testthat/test-rstudio_markers.R index 3db103f02..8aad2cee8 100644 --- a/tests/testthat/test-rstudio_markers.R +++ b/tests/testthat/test-rstudio_markers.R @@ -105,7 +105,7 @@ test_that("rstudio_source_markers apply to print within rstudio", { skip_if_not_installed("rstudioapi") local_mocked_bindings( - hasFun = function(x, ...) TRUE, + hasFun = function(...) TRUE, .package = "rstudioapi" ) local_mocked_bindings(rstudio_source_markers = function(x) cat("matched\n"))