Hook dependencies update #746
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
pull_request: | |
branches: | |
- '*' | |
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-latest, r: 'release', use-public-rspm: true} | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
steps: | |
- uses: r-lib/actions/setup-pandoc@v2 | |
- uses: actions/checkout@v4 | |
- uses: r-lib/actions/setup-r@v2 | |
with: | |
r-version: ${{ matrix.config.r }} | |
- 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@v4 | |
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@v4 | |
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@v4 | |
with: | |
path: ~\AppData\Local\renv | |
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('renv.lock') }} | |
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-2- | |
- uses: r-lib/actions/setup-r-dependencies@v2 | |
- name: Prepare hook environment | |
run: | | |
source('renv/activate.R') | |
options(install.packages.compile.from.source = "never", pkgType = "binary") | |
renv::restore() | |
# install hook-specific additional_dependencies from .pre-commit-config.yaml | |
renv::install(c('pkgdown')) | |
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(), "\")"), | |
# " print(renv::status())", | |
"}", | |
"print('R profile activated')" | |
) | |
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")) | |
} | |
# avoid build-time deps | |
options(install.packages.compile.from.source = "never") | |
renv::install(c('testthat', 'pkgload', 'git2r')) | |
# some testing infrastructure we need is in R/testing.R | |
renv::install(getwd()) | |
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: | | |
pkgload::load_all() | |
hook_test_files <- as.character(fs::dir_ls('tests/testthat/', regexp = "test-hook-.*\\.R", type = "file")) | |
tester <- function(file) { | |
testthat::test_file(file, | |
reporter = testthat::MultiReporter$new(list( | |
testthat::CheckReporter$new(), testthat::FailReporter$new() | |
)) | |
) | |
} | |
purrr::walk(hook_test_files, tester) | |
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 |