Skip to content

Commit

Permalink
Merge branch 'main' into more_nolint_directives
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Oct 26, 2022
2 parents 9e5934d + d0023d4 commit 9993108
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 118 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/R-CMD-check-hard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
#
# NOTE: This workflow only directly installs "hard" dependencies, i.e. Depends,
# Imports, and LinkingTo dependencies. Notably, Suggests dependencies are never
# installed, with the exception of testthat, knitr, and rmarkdown. The cache is
# never used to avoid accidentally restoring a cache containing a suggested
# dependency.
#
# Customization for `{lintr}`
#
# The following soft dependencies are not optional for testing:
# `{patrick}`, `{rex}`, `{withr}`
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check-hard

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, r: "release" }

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
pak-version: devel
dependencies: '"hard"'
cache: false
extra-packages: |
any::rcmdcheck
any::testthat
any::knitr
any::rmarkdown
any::patrick
any::withr
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Suggests:
jsonlite,
mockery,
patrick,
pkgdown,
rlang,
rmarkdown,
rstudioapi (>= 0.2),
Expand Down Expand Up @@ -146,6 +145,7 @@ Collate:
'semicolon_linter.R'
'seq_linter.R'
'settings.R'
'settings_utils.R'
'single_quotes_linter.R'
'spaces_inside_linter.R'
'spaces_left_parentheses_linter.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* `object_usage_linter()` no longer silently ignores usage warnings that don't contain a quoted name (#1714, @AshesITR)

* `namespace_linter()` correctly recognizes backticked operators to be exported from respectives namespaces (like `` rlang::`%||%` ``) (#1752, @IndrajeetPatil)

## Changes to defaults

* Set the default for the `except` argument in `duplicate_argument_linter()` to `c("mutate", "transmute")`.
Expand Down
67 changes: 0 additions & 67 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,73 +379,6 @@ reorder_lints <- function(lints) {
}


has_description <- function(path) {
desc_info <- file.info(file.path(path, "DESCRIPTION"))
!is.na(desc_info$size) && desc_info$size > 0.0 && !desc_info$isdir
}

find_package <- function(path) {
depth <- 2L
while (!has_description(path)) {
path <- dirname(path)
if (is_root(path) || depth <= 0L) {
return(NULL)
}
depth <- depth - 1L
}
path
}

find_rproj_or_package <- function(path) {
path <- normalizePath(path, mustWork = FALSE)

depth <- 2L
while (!(has_description(path) || has_rproj(path))) {
path <- dirname(path)
if (is_root(path) || depth <= 0L) {
return(NULL)
}
depth <- depth - 1L
}
path
}

has_rproj <- function(path) {
length(head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)) == 1L
}

find_rproj_at <- function(path) {
head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)
}
is_root <- function(path) {
identical(path, dirname(path))
}

has_config <- function(path, config) {
file.exists(file.path(path, config))
}

find_config2 <- function(path) {
config <- basename(getOption("lintr.linter_file"))
path <- normalizePath(path, mustWork = FALSE)

while (!has_config(path, config)) {
path <- dirname(path)
if (is_root(path)) {
return(character())
}
}
return(file.path(path, config))
}

pkg_name <- function(path = find_package()) {
if (is.null(path)) {
return(NULL)
} else {
read.dcf(file.path(path, "DESCRIPTION"), fields = "Package")[1L]
}
}

#' Create a `lint` object
#' @param filename path to the source file that was linted.
#' @param line_number line number where the lint occurred.
Expand Down
3 changes: 3 additions & 0 deletions R/namespace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ build_ns_get_int_lints <- function(packages, symbols, symbol_nodes, namespaces,
build_ns_get_lints <- function(packages, symbols, symbol_nodes, namespaces, source_expression) {
lints <- list()

# strip backticked symbols; `%>%` is the same as %>% (#1752).
symbols <- gsub("^`(.*)`$", "\\1", symbols)

## Case 4: foo is not an export in pkg::foo

unexported <- !vapply(
Expand Down
46 changes: 0 additions & 46 deletions R/settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,6 @@ clear_settings <- function() {
rm(list = ls(settings), envir = settings)
}

find_config <- function(filename) {
if (is.null(filename)) {
return(NULL)
}
linter_file <- getOption("lintr.linter_file")

## if users changed lintr.linter_file, return immediately.
if (is_absolute_path(linter_file) && file.exists(linter_file)) {
return(linter_file)
}

path <- if (is_directory(filename)) {
filename
} else {
dirname(filename)
}

## check for a file in the current directory
linter_config <- file.path(path, linter_file)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

## next check for a file higher directories
linter_config <- find_config2(path)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

## next check for a file in the user directory
# cf: rstudio@bc9b6a5 SessionRSConnect.R#L32
home_dir <- Sys.getenv("HOME", unset = "~")
linter_config <- file.path(home_dir, linter_file)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

NULL
}

find_default_encoding <- function(filename) {
if (is.null(filename)) {
return(NULL)
Expand Down Expand Up @@ -138,9 +98,3 @@ get_encoding_from_dcf <- function(file) {

NULL
}

is_directory <- function(filename) {
is_dir <- file.info(filename)$isdir

!is.na(is_dir) && is_dir
}
113 changes: 113 additions & 0 deletions R/settings_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
has_description <- function(path) {
desc_info <- file.info(file.path(path, "DESCRIPTION"))
!is.na(desc_info$size) && desc_info$size > 0.0 && !desc_info$isdir
}

find_package <- function(path) {
depth <- 2L
while (!has_description(path)) {
path <- dirname(path)
if (is_root(path) || depth <= 0L) {
return(NULL)
}
depth <- depth - 1L
}
path
}

find_rproj_or_package <- function(path) {
path <- normalizePath(path, mustWork = FALSE)

depth <- 2L
while (!(has_description(path) || has_rproj(path))) {
path <- dirname(path)
if (is_root(path) || depth <= 0L) {
return(NULL)
}
depth <- depth - 1L
}
path
}

has_rproj <- function(path) {
length(head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)) == 1L
}

find_rproj_at <- function(path) {
head(Sys.glob(file.path(path, "*.Rproj")), n = 1L)
}

is_root <- function(path) {
identical(path, dirname(path))
}

is_directory <- function(filename) {
is_dir <- file.info(filename)$isdir

!is.na(is_dir) && is_dir
}

has_config <- function(path, config) {
file.exists(file.path(path, config))
}

find_config <- function(filename) {
if (is.null(filename)) {
return(NULL)
}
linter_file <- getOption("lintr.linter_file")

## if users changed lintr.linter_file, return immediately.
if (is_absolute_path(linter_file) && file.exists(linter_file)) {
return(linter_file)
}

path <- if (is_directory(filename)) {
filename
} else {
dirname(filename)
}

## check for a file in the current directory
linter_config <- file.path(path, linter_file)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

## next check for a file higher directories
linter_config <- find_config2(path)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

## next check for a file in the user directory
# cf: rstudio@bc9b6a5 SessionRSConnect.R#L32
home_dir <- Sys.getenv("HOME", unset = "~")
linter_config <- file.path(home_dir, linter_file)
if (isTRUE(file.exists(linter_config))) {
return(linter_config)
}

NULL
}

find_config2 <- function(path) {
config <- basename(getOption("lintr.linter_file"))
path <- normalizePath(path, mustWork = FALSE)

while (!has_config(path, config)) {
path <- dirname(path)
if (is_root(path)) {
return(character())
}
}
return(file.path(path, config))
}

pkg_name <- function(path = find_package()) {
if (is.null(path)) {
return(NULL)
} else {
read.dcf(file.path(path, "DESCRIPTION"), fields = "Package")[1L]
}
}
5 changes: 5 additions & 0 deletions tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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(...))

Expand All @@ -48,6 +50,8 @@ 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(...))

Expand Down Expand Up @@ -408,6 +412,7 @@ 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)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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)
Expand All @@ -39,6 +41,8 @@ test_that("GitHub Actions - linting on error works", {
})

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)")
Expand All @@ -50,6 +54,8 @@ test_that("Printing works for Travis", {
})

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)")
Expand Down
Loading

0 comments on commit 9993108

Please sign in to comment.