diff --git a/NEWS.md b/NEWS.md index 61a33fadb..b2d9e0247 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,8 @@ * `namespace_linter()` correctly recognizes backticked operators to be exported from respectives namespaces (like `` rlang::`%||%` ``) (#1752, @IndrajeetPatil) +* `lint_package()` correctly finds a package from within a subdir if the `path` points to anywhere within the package (#1759, @AshesITR) + ## Changes to defaults * Set the default for the `except` argument in `duplicate_argument_linter()` to `c("mutate", "transmute")`. diff --git a/R/settings_utils.R b/R/settings_utils.R index 20e43e940..40465927e 100644 --- a/R/settings_utils.R +++ b/R/settings_utils.R @@ -4,6 +4,7 @@ has_description <- function(path) { } find_package <- function(path) { + path <- normalizePath(path) depth <- 2L while (!has_description(path)) { path <- dirname(path) diff --git a/tests/testthat/dummy_packages/assignmentLinter/DESCRIPTION b/tests/testthat/dummy_packages/assignmentLinter/DESCRIPTION index 59ea6ad0c..951356edd 100644 --- a/tests/testthat/dummy_packages/assignmentLinter/DESCRIPTION +++ b/tests/testthat/dummy_packages/assignmentLinter/DESCRIPTION @@ -7,3 +7,6 @@ Author: Who wrote it Maintainer: Who to complain to Description: More about what it does (maybe more than one line) License: What license is it under? +Suggests: + testthat (>= 3.0.0) +Config/testthat/edition: 3 diff --git a/tests/testthat/dummy_packages/assignmentLinter/tests/testthat.R b/tests/testthat/dummy_packages/assignmentLinter/tests/testthat.R new file mode 100644 index 000000000..0d26ab562 --- /dev/null +++ b/tests/testthat/dummy_packages/assignmentLinter/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(assignmentLinter) + +test_check("assignmentLinter") diff --git a/tests/testthat/dummy_packages/assignmentLinter/tests/testthat/test-abc.R b/tests/testthat/dummy_packages/assignmentLinter/tests/testthat/test-abc.R new file mode 100644 index 000000000..38a990c2d --- /dev/null +++ b/tests/testthat/dummy_packages/assignmentLinter/tests/testthat/test-abc.R @@ -0,0 +1,3 @@ +test_that("test abc", { + expect_equal(2 * 2, 4) +}) diff --git a/tests/testthat/test-lint_package.R b/tests/testthat/test-lint_package.R index 197122990..482b04727 100644 --- a/tests/testthat/test-lint_package.R +++ b/tests/testthat/test-lint_package.R @@ -91,7 +91,11 @@ test_that( ) lints_from_a_subdir <- withr::with_dir( file.path(pkg_path, "R"), - lint_package("..", linters = list(assignment_linter())) + lint_package(".", linters = list(assignment_linter())) + ) + lints_from_a_subsubdir <- withr::with_dir( + file.path(pkg_path, "tests", "testthat"), + lint_package(".", linters = list(assignment_linter())) ) expect_identical( @@ -114,6 +118,14 @@ test_that( "(.lintr config present)" ) ) + expect_identical( + as.data.frame(lints_from_outside), + as.data.frame(lints_from_a_subsubdir), + info = paste( + "lint_package() finds the same lints from a sub-subdir as from outside a pkg", + "(.lintr config present)" + ) + ) } ) diff --git a/tests/testthat/test-settings.R b/tests/testthat/test-settings.R index ea3ec43c0..9024f52fd 100644 --- a/tests/testthat/test-settings.R +++ b/tests/testthat/test-settings.R @@ -1,3 +1,5 @@ +# nolint start: undesirable_operator. +# This test file tests multiple internal lintr functions, so we need to allow lintr:::* test_that("it uses default settings if none provided", { lintr:::read_settings(NULL) @@ -98,8 +100,8 @@ test_that("it has a smart default for encodings", { lintr:::read_settings(NULL) expect_identical(settings$encoding, "UTF-8") - proj_file <- test_path("dummy_projects", "project", "metropolis-hastings-rho.R") - pkg_file <- test_path("dummy_packages", "cp1252", "R", "metropolis-hastings-rho.R") + proj_file <- test_path("dummy_projects", "project", "cp1252.R") + pkg_file <- test_path("dummy_packages", "cp1252", "R", "cp1252.R") expect_identical( normalizePath(find_rproj_at(find_rproj_or_package(proj_file)), winslash = "/"), @@ -119,3 +121,4 @@ test_that("it has a smart default for encodings", { lintr:::read_settings(pkg_file) expect_identical(settings$encoding, "ISO8859-1") }) +# nolint end