From 0c82dbfa26f20d4e4e4fc9fcce5b7edf8c06676c Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 20 Aug 2024 11:16:14 -0400 Subject: [PATCH] add test for codetools --- .Rbuildignore | 1 + .github/.gitignore | 1 + .github/workflows/R-CMD-check.yaml | 52 ++++++++++++++++++++++++++++++ README.Rmd | 4 +++ README.md | 26 +++++++-------- tests/testthat/test-dot.R | 20 ++++++++++++ 6 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 .github/.gitignore create mode 100644 .github/workflows/R-CMD-check.yaml diff --git a/.Rbuildignore b/.Rbuildignore index 931662b..9e030a0 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,4 @@ ^.*\.Rproj$ ^\.Rproj\.user$ ^README\.Rmd$ +^\.github$ diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 0000000..2d19fc7 --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +*.html diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml new file mode 100644 index 0000000..d46a617 --- /dev/null +++ b/.github/workflows/R-CMD-check.yaml @@ -0,0 +1,52 @@ +# 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 +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: R-CMD-check.yaml + +permissions: read-all + +jobs: + R-CMD-check: + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + + steps: + - uses: actions/checkout@v4 + + - 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: + extra-packages: any::rcmdcheck + needs: check + + - uses: r-lib/actions/check-r-package@v2 + with: + upload-snapshots: true + build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' diff --git a/README.Rmd b/README.Rmd index d507eef..1636b4a 100644 --- a/README.Rmd +++ b/README.Rmd @@ -3,6 +3,10 @@ title: dotty output: github_document --- + +[![R-CMD-check](https://github.com/kevinushey/dotty/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/kevinushey/dotty/actions/workflows/R-CMD-check.yaml) + + Destructuring assignments in R with the `.` object. diff --git a/README.md b/README.md index f878fa5..ff00a00 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ dotty ================ + + +[![R-CMD-check](https://github.com/kevinushey/dotty/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/kevinushey/dotty/actions/workflows/R-CMD-check.yaml) + + Destructuring assignments in R with the `.` object. ### Usage @@ -45,15 +50,7 @@ c(x, y, z) c(major, minor, patch) ``` - ## [1] 4 2 2 - -``` r -# compute on values in object -- this is probably too magical -.[total = sum(a)] <- list(a = 1:5) -total -``` - - ## [1] 15 + ## [1] 4 4 1 ### R CMD check @@ -61,8 +58,11 @@ If you’d like to use `dotty` in your CRAN package, you can avoid `R CMD check` warnings by including a file called `R/zzz.R` with the contents: - dotty::dotify() + .onLoad <- function(libname, pkgname) { + dotty::dotify() + } -This function will search for usages of `dotty`, and call -`utils::globalVariables()` to assert to `R CMD check` that such -variables are valid for use. +This function patches +[codetools](https://cran.r-project.org/package=codetools) so that +variables usages in `.` expressions can be linted as though those were +regular bindings / assignments. diff --git a/tests/testthat/test-dot.R b/tests/testthat/test-dot.R index ab36260..92d0c9e 100644 --- a/tests/testthat/test-dot.R +++ b/tests/testthat/test-dot.R @@ -81,3 +81,23 @@ test_that("we can destructure R versions", { expect_equal(minor, unclass(version)[[1L]][[2L]]) expect_equal(patch, unclass(version)[[1L]][[3L]]) }) + +test_that("dotify helps codetools understand dotty usages", { + + skip_if_not_installed("codetools") + + example <- function() { + .[apple, banana] <- c(1, 2) + c(apple, banana, cherry) + } + + messages <- "" + result <- codetools::checkUsage(example, report = function(x) { + messages <<- paste(messages, x, sep = "") + }) + + expect_false(grepl("apple", messages)) + expect_false(grepl("banana", messages)) + expect_true(grepl("cherry", messages)) + +})