Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use r instead of script as language #233

Merged
merged 75 commits into from May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
8174510
add renv.lock only
lorenzwalthert Mar 8, 2021
508aa39
Explicitly make this a package
lorenzwalthert Mar 8, 2021
7eee437
bump for hash
lorenzwalthert Mar 8, 2021
9d05c3b
bump for testing
lorenzwalthert Mar 8, 2021
ca3282f
bump for test
lorenzwalthert Mar 8, 2021
3b51d37
bump
lorenzwalthert Mar 9, 2021
1c75f35
add activate script by default
lorenzwalthert Mar 16, 2021
d4f9d09
adapt docs to virtualenv support
lorenzwalthert Mar 16, 2021
f153aad
make clear that this is setting up things for a repo
lorenzwalthert Mar 17, 2021
92be882
intro venv
lorenzwalthert Mar 17, 2021
2c92d9c
rename to test file
lorenzwalthert Mar 17, 2021
9156093
temporarily disable R cmd check
lorenzwalthert Mar 17, 2021
b44f299
add simple hook test
lorenzwalthert Mar 17, 2021
f3a988e
we don't need pandoc
lorenzwalthert Mar 17, 2021
a0b7de4
activate renv
lorenzwalthert Mar 17, 2021
5146ebf
only run on macOS now, cache as suggested in https://github.com/actio…
lorenzwalthert Mar 17, 2021
b41edf5
use a mirror
lorenzwalthert Mar 17, 2021
23dbcf6
install renv globally anyways so we can use install utilities with cache
lorenzwalthert Mar 17, 2021
4b542b2
build package in different location than testthat and other testing d…
lorenzwalthert Mar 17, 2021
63fad34
make a run that caches stuff
lorenzwalthert Mar 17, 2021
8dc8ae9
Revert "make a run that caches stuff"
lorenzwalthert Mar 17, 2021
4cfda2e
to run tests, we must have package loaded
lorenzwalthert Mar 17, 2021
273204f
check if styler is available
lorenzwalthert Mar 17, 2021
9734bc5
use WIP to test
lorenzwalthert Mar 18, 2021
3f53ea1
bump ci
lorenzwalthert Mar 18, 2021
51aa130
bump
lorenzwalthert Mar 18, 2021
8845122
switch to language R
lorenzwalthert Mar 18, 2021
2482329
bump
lorenzwalthert Mar 18, 2021
781e9b2
use env var for testing
lorenzwalthert Mar 18, 2021
56795b2
fail on test error
lorenzwalthert Mar 18, 2021
b90148c
test hooks
lorenzwalthert Mar 18, 2021
8162c67
use newer renv
lorenzwalthert Mar 18, 2021
1f0c088
use cran version of desc, GitHub verion of styler (t make tests pass)
lorenzwalthert Mar 18, 2021
694dc05
exclude renv from build
lorenzwalthert Mar 18, 2021
662589f
use newer renv in activation script too
lorenzwalthert Mar 18, 2021
6f04f87
rename env variable
lorenzwalthert Mar 18, 2021
566b0c4
avoid warning about renv versions by boostraping only for hook env an…
lorenzwalthert Mar 18, 2021
1066cc1
enable cross platform testing
lorenzwalthert Mar 18, 2021
fe4da84
select mirror
lorenzwalthert Mar 18, 2021
529b856
nicer display name
lorenzwalthert Mar 18, 2021
bc9a59c
install remotes for sys req check
lorenzwalthert Mar 18, 2021
648600d
only install remotes on Linux
lorenzwalthert Mar 18, 2021
80c1f75
try cran with repos for windows
lorenzwalthert Mar 18, 2021
1d04d36
ensure cached package library for experimenting
lorenzwalthert Mar 18, 2021
e11eec2
try https
lorenzwalthert Mar 18, 2021
2c3e69c
remove hook env to fail faster
lorenzwalthert Mar 18, 2021
e3e29c4
Revert "remove hook env to fail faster"
lorenzwalthert Mar 18, 2021
dea2fa6
Revert "ensure cached package library for experimenting"
lorenzwalthert Mar 18, 2021
929043f
remove empty test
lorenzwalthert Mar 18, 2021
5be7e1b
test leaking from test into hook env
lorenzwalthert Mar 18, 2021
c642a15
rename
lorenzwalthert Mar 18, 2021
b88827c
better titles
lorenzwalthert Mar 18, 2021
1131dad
reorder
lorenzwalthert Mar 18, 2021
4897aea
don't assert anymore
lorenzwalthert Mar 18, 2021
dcf5983
assert after the fact
lorenzwalthert Mar 18, 2021
583c1b4
try deactivating renv, as it could mistakenly write infra
lorenzwalthert Mar 18, 2021
af6af55
fix title
lorenzwalthert Mar 18, 2021
e5baaee
use load, not activate, to prevent infrastructure writing
lorenzwalthert Mar 18, 2021
5c3982b
no need to deactivate when load was used
lorenzwalthert Mar 18, 2021
5b44616
drop random space
lorenzwalthert Mar 18, 2021
60e93dd
fix news
lorenzwalthert Mar 18, 2021
1881cc1
move details out to testing vignette
lorenzwalthert Mar 18, 2021
1e0197f
Revert "temporarily disable R cmd check"
lorenzwalthert Mar 18, 2021
8bcac08
trigger new run
lorenzwalthert Apr 6, 2021
f2318c8
downgrade to see if warning occurs
lorenzwalthert Apr 6, 2021
829bbef
run test
lorenzwalthert Apr 7, 2021
89e31b5
more doc notes
lorenzwalthert Apr 9, 2021
d3651e6
drop assumption test, hope people use latest version of desc
lorenzwalthert Apr 9, 2021
f3359fb
when using R as language, we need minimal pre-commit version
lorenzwalthert Apr 9, 2021
843a72c
stricter min reqs
lorenzwalthert Apr 10, 2021
5260dff
stricter min reqs
lorenzwalthert Apr 10, 2021
5cdc3e7
the readme hook remains a scripted one
lorenzwalthert Apr 17, 2021
fd9d2b0
newer R, styler
lorenzwalthert Apr 23, 2021
3a3475a
update packages
lorenzwalthert Apr 27, 2021
eae3959
remove Type as we can rely on new definition criterion
lorenzwalthert May 4, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^README\.Rmd$
^.*\.Rproj$
^\.Rproj\.user$
Expand Down
126 changes: 126 additions & 0 deletions .github/workflows/hook-tests.yaml
@@ -0,0 +1,126 @@
on:
push:
branches:
- master
pull_request:
branches:
- master

name: Hook tests

jobs:
hook-test:
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-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
env:
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-r@master
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
- name: Query dependencies
run: |
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
- name: Cache R packages (macOs)
if: startsWith(runner.os, 'macOS')
uses: actions/cache@v2
with:
path: ~/Library/Application Support/renv
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('renv.lock') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
- name: Cache R packages (Linux)
if: startsWith(runner.os, 'Linux')
uses: actions/cache@v2
with:
path: ~/.local/share/renv
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('renv.lock') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
- name: Cache R packages (Windows)
if: startsWith(runner.os, 'Windows')
uses: actions/cache@v2
with:
path: ~\AppData\Local\renv
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('renv.lock') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies (Linux)
if: runner.os == 'Linux'
run: |
Rscript -e "install.packages('remotes', repos = Sys.getenv('RSPM'))"
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'cat(remotes::system_requirements("ubuntu", "18.04"), sep = "\n")')
- name: Prepare hook environment
run: |
source('renv/activate.R')
renv::restore()
renv::install(getwd(), dependencies = FALSE)
# needed to make sure renv is activated in run_test()
activate <- c(
"if (nzchar(Sys.getenv('R_PRECOMMIT_HOOK_ENV')))",
paste0("renv::load('", getwd(), "')")
)
writeLines(activate, '~/.Rprofile')
shell: Rscript {0}
- name: Perpare testing environment
run: |
# testing dependencies (incl. {precommit}) live in global R library,
# hook dependencies in renv, which is only activated when child R
# process is invoked, e.g. as in run_test()
if (!requireNamespace("renv", quietly = TRUE)) {
install.packages("renv", repos = c(CRAN = "https://cloud.r-project.org"))
}
renv::install(c('testthat', 'devtools'))
# some testing infrastructure we need is in R/testing.R
renv::install(getwd(), dependencies = TRUE)
shell: Rscript {0}
- name: Session info (testing environment)
run: |
renv::install('sessioninfo', repos = 'cloud.r-project.org')
options(width = 100)
pkgs <- installed.packages()[, "Package"]
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}
- name: Test
run: |
devtools::load_all()
testthat::test_file(
"tests/testthat/test-hooks.R",
reporter = testthat::MultiReporter$new(list(
testthat::CheckReporter$new(), testthat::FailReporter$new()
))
)
shell: Rscript {0}
- name: Show testthat output
if: always()
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash
- name: Assert no leakage from testing to hook env (1/2)
run: |
Rscript -e "renv::install('usethis'); path_pkg <- file.path(tempdir(), 'someP'); usethis::create_package(path_pkg); renv::install(path_pkg)"
- name: Assert no leakage from testing to hook env (2/2)
env:
R_PRECOMMIT_HOOK_ENV: "1"
run: |
Rscript -e "if ('someP' %in% rownames(installed.packages())) stop('environment isolation not working: You installed a package into the testing environment and it is available in the hook environment')"
- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -3,11 +3,15 @@
default_stages: ["commit"]
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.1.3
rev: acc8657498d5bfb9e9891098ba00b36e82c7ebd6
hooks:
- id: style-files
args: [--style_pkg=styler, --style_fun=tidyverse_style]
exclude: '^tests/testthat/in/.*\.R'
exclude: >
(?x)^(
tests/testthat/in/.*\.R|
renv/.*
)$
- id: roxygenize
# codemeta must be above use-tidy-description when both are used
# - id: codemeta-description-updated
Expand Down
47 changes: 29 additions & 18 deletions .pre-commit-hooks.yaml
@@ -1,51 +1,59 @@
- id: roxygenize
name: roxygenize
description: run roxygen2::roxygenize()
entry: inst/bin/roxygenize
language: script
entry: Rscript inst/bin/roxygenize
language: r
files: '^(R|man)/'
require_serial: true
minimum_pre_commit_version: "2.12"
- id: use-tidy-description
name: use-tidy-description
description: run usethis::use_tidy_description()
entry: inst/bin/use-tidy-description
language: script
entry: Rscript inst/bin/use-tidy-description
language: r
files: '^DESCRIPTION$'
minimum_pre_commit_version: "2.12"
- id: style-files
name: style-files
description: style files with styler
entry: inst/bin/style-files
language: script
entry: Rscript inst/bin/style-files
language: r
files: '(\.R|\.Rmd|\.Rnw|\.r|\.rmd|\.rnw)$'
exclude: 'renv/activate\.R'
minimum_pre_commit_version: "2.12"
- id: no-browser-statement
name: no-browser-statement
description: check if a .R file contains a `browser()` statement
entry: inst/bin/no-browser-statement
language: script
entry: Rscript inst/bin/no-browser-statement
language: r
files: '\.[rR]$'
minimum_pre_commit_version: "2.12"
- id: parsable-R
name: parsable-R
description: check if a .R file is parsable
entry: inst/bin/parsable-R
language: script
entry: Rscript inst/bin/parsable-R
language: r
files: '\.[rR]$'
minimum_pre_commit_version: "2.12"
- id: readme-rmd-rendered
name: readme-rmd-rendered
description: make sure README.Rmd hasn't been edited more recently than README.md
entry: inst/bin/readme-rmd-rendered
language: script
files: 'README\.[Rr]?md$'
minimum_pre_commit_version: "2.12"
- id: codemeta-description-updated
name: codemeta-description-updated
description: make sure codemeta.json is in sync with DESCRIPTION. It should be run after use-tidy-description.
entry: inst/bin/codemeta-description-updated
language: script
entry: Rscript inst/bin/codemeta-description-updated
language: r
files: '^DESCRIPTION$'
minimum_pre_commit_version: "2.12"
- id: spell-check
name: spell-check
description: perform a spell check with spelling::spell_check_files()
entry: inst/bin/spell-check
language: script
entry: Rscript inst/bin/spell-check
language: r
exclude: >
(?x)^(
data/.*|
Expand All @@ -70,17 +78,20 @@
.*\.RData
)$
require_serial: true
minimum_pre_commit_version: "2.12"
- id: deps-in-desc
name: deps-in-desc
description: Check if dependencies that can be parsed from code are in DESCRIPTION.
entry: inst/bin/deps-in-desc
language: script
entry: Rscript inst/bin/deps-in-desc
language: r
files: '(\.[rR]profile|\.R|\.Rmd|\.Rnw|\.r|\.rmd|\.rnw)$'
exclude: 'renv/activate\.R'
minimum_pre_commit_version: "2.12"
- id: lintr
name: lintr
description: check if a .R file is lint free (using lintr)
entry: inst/bin/lintr
language: script
entry: Rscript inst/bin/lintr
language: r
files: '\.[rR]$'
exclude: 'renv/activate\.R'
minimum_pre_commit_version: "2.12"
20 changes: 10 additions & 10 deletions DESCRIPTION
Expand Up @@ -3,11 +3,11 @@ Title: Pre-Commit Hooks
Version: 0.1.3.9000
Author: Lorenz Walthert
Maintainer: Lorenz Walthert <lorenz.walthert@icloud.com>
Description: Useful git hooks for R building on top of the
multi-language framework 'pre-commit' for hook management. This
package provides git hooks for common tasks like formatting files with
'styler' or spell checking as well as wrapper functions to access the
'pre-commit' executable.
Description: Useful git hooks for R building on top of the multi-language
framework 'pre-commit' for hook management. This package provides git
hooks for common tasks like formatting files with 'styler' or spell
checking as well as wrapper functions to access the 'pre-commit'
executable.
License: GPL-3
URL: https://lorenzwalthert.github.io/precommit/,
https://github.com/lorenzwalthert/precommit
Expand Down Expand Up @@ -41,8 +41,8 @@ VignetteBuilder:
Remotes:
r-lib/styler
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace",
"collate", if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet"
else { warning("Please install r-lib/pkgapi to make sure the file API
is kept up to date"); NULL} ) )
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace", "collate",
if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet" else {
warning("Please install r-lib/pkgapi to make sure the file API is kept
up to date"); NULL} ) )
RoxygenNote: 7.1.1.9001
7 changes: 7 additions & 0 deletions NEWS.md
Expand Up @@ -12,6 +12,13 @@
`{R.cache}` cache set up. You can silence the warning with the hook argument
`--no-warn-cache` (#225).

**Major Changes**

- {precommit} now uses [`language: r`](https://pre-commit.com/#r) instead of
`language: script` from the [pre-commit framework](https://pre-commit.com).
This requires `pre-commit >= 2.11.1`. All hooks and dependencies are now
contained in a virtual environment with [`{renv}`](https://rstudio.github.io/renv/)
(#233).

**Minor changes**

Expand Down
4 changes: 2 additions & 2 deletions R/setup.R
@@ -1,6 +1,6 @@
#' Set up pre-commit
#' Get started with pre-commit
#'
#' Get started.
#' This function sets up pre-commit for your git repo.
#' @param install_hooks Whether to install environments for all available hooks.
#' If `FALSE`, environments are installed with first commit.
#' @param legacy_hooks How to treat hooks already in the repo which are not
Expand Down
7 changes: 4 additions & 3 deletions R/testing.R
@@ -1,7 +1,8 @@
#' Run a test
#'
#' Tests for the executables used as pre-commit hooks via `entrypoint` in
#' `.pre-commit-config.yaml`.
#' `.pre-commit-config.yaml`. Set's the env variable `R_PRECOMMIT_HOOK_ENV` to
#' when running.
#' @details
#' Two potential outcomes of a hooks are pass or fail. This is reflected on the
#' level of the executable: Fail means the executable fails or the file is
Expand All @@ -15,7 +16,6 @@
#' executable. To check for failed executable, we set `error_msg` to
#' the message we expect. To check changed file content, we set `error_msg` to
#' `NA`.
#'
#' @param hook_name The name of the hook in `bin/`.
#' @param file_name The file to test in `tests/in` (without extension).
#' @param suffix The suffix of `file_name`.
Expand All @@ -39,6 +39,7 @@ run_test <- function(hook_name,
copy = NULL,
file_transformer = function(files) files,
env = character()) {
withr::local_envvar(list(R_PRECOMMIT_HOOK_ENV = "1"))
path_executable <- system.file(
fs::path("bin", hook_name),
package = "precommit"
Expand Down Expand Up @@ -87,7 +88,7 @@ run_test_impl <- function(path_executable,
tempdir <- fs::dir_create(fs::file_temp())
if (!is.null(copy)) {
if (is.null(names(copy))) {
# no names, take basename
# not namesm take directory name
new_dirs <- fs::path(tempdir, fs::path_dir(copy))
fs::dir_create(new_dirs)
paths_copy <- fs::path(new_dirs, fs::path_file(copy))
Expand Down