-
-
Notifications
You must be signed in to change notification settings - Fork 48
127 lines (120 loc) · 5.05 KB
/
hook-tests.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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