Skip to content

Commit 608fa90

Browse files
Merge pull request #305 from lorenzwalthert/styler-permanent-cache
- expose cache root arg (#305).
2 parents 5aca6df + 11615df commit 608fa90

File tree

8 files changed

+68
-20
lines changed

8 files changed

+68
-20
lines changed

.github/workflows/hook-dependencies-upate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ jobs:
7676
This PR updates the hook dependencies in `renv.lock`, auto-generated by [create-pull-request][1]. Close and re-open this to trigger `on: pull_request` events to circumvent [limitation of GitHub actions](https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#push-pull-request-branches-to-a-fork).
7777
7878
[1]: https://github.com/peter-evans/create-pull-request
79+
80+

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
rev: v0.1.3.9133
99
hooks:
1010
- id: style-files
11-
args: [--style_pkg=styler, --style_fun=tidyverse_style]
11+
args: [--style_pkg=styler, --style_fun=tidyverse_style, --cache_root=styler-perm]
1212
exclude: >
1313
(?x)^(
1414
tests/testthat/in/.*\.R|

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ used packages, roxygen snippet generation and more. In addition:
99
* rename default branch to *main* (#307).
1010

1111

12+
* `style-files` hook gains an argument `--cache-root` that is passed to
13+
`options(styler.cache_root = ...)` (#305).
14+
1215
# precommit v0.1.3.9012
1316

1417
This is a pre-release for `v0.2.0` and imposes a minimal version requirement

inst/hooks/exported/style-files.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env Rscript
22
'style files.
33
Usage:
4-
style_files [--style_pkg=<style_guide_pkg>] [--style_fun=<style_guide_fun>] [--no-warn-cache] <files>...
4+
style_files [--style_pkg=<style_guide_pkg>] [--style_fun=<style_guide_fun>] [--cache-root=<cache_root_>] [--no-warn-cache] <files>...
55
66
Options:
77
--style_pkg=<style_guide_pkg> Package where the style guide is stored [default: "styler"].
88
--style_fun=<style_guide_fun> The styling function in style_pkg [default: "tidyverse_style"].
99
--no-warn-cache Suppress the warning about a missing permanent cache.
10+
--cache-root=<cache_root_> Passed to `options("styler.cache_root")` [default: "styler-perm"].
1011
' -> doc
1112

1213
if (packageVersion("precommit") < "0.1.3.9010") {
@@ -24,8 +25,8 @@ library(styler)
2425
args <- commandArgs(trailingOnly = TRUE)
2526
non_file_args <- args[!grepl("^[^-][^-]", args)]
2627
keys <- setdiff(
27-
gsub("(^--[0-9A-Za-z_]+).*", "\\1", non_file_args),
28-
c("--style_pkg", "--style_fun", "--no-warn-cache", "cache")
28+
gsub("(^--[0-9A-Za-z_-]+).*", "\\1", non_file_args),
29+
c("--style_pkg", "--style_fun", "--cache-root", "--no-warn-cache")
2930
)
3031
if (length(keys) > 0) {
3132
bare_keys <- gsub("^--", "", keys)
@@ -45,6 +46,8 @@ if (packageVersion("styler") < "1.3.2") {
4546
} else {
4647
precommit::may_require_permanent_cache(arguments$no_warn_cache)
4748
}
49+
options('styler.cache_root' = arguments$cache_root)
50+
print(c('cache root set to ', arguments$cache_root))
4851

4952
style <- eval(parse(text = paste(arguments$style_pkg, "::", arguments$style_fun)))
5053
tryCatch(

renv.lock

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,15 @@
401401
},
402402
"styler": {
403403
"Package": "styler",
404-
"Version": "1.5.1",
405-
"Source": "Repository",
406-
"Repository": "CRAN",
407-
"Hash": "eb62a56987abe8fa9d1de47eef53a790"
404+
"Version": "1.6.2.9000",
405+
"Source": "GitHub",
406+
"RemoteType": "github",
407+
"RemoteHost": "api.github.com",
408+
"RemoteRepo": "styler",
409+
"RemoteUsername": "r-lib",
410+
"RemoteRef": "HEAD",
411+
"RemoteSha": "55d0920fcaafa0217f625b9dec491621556e76f6",
412+
"Hash": "0b44de206a40a24c8065b1ebe72be129"
408413
},
409414
"sys": {
410415
"Package": "sys",

tests/testthat/test-hooks.R

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,34 @@ run_test("use-tidy-description", "DESCRIPTION", suffix = "")
88
### style-files ####
99

1010
# success
11-
run_test("style-files", suffix = "-success.R")
11+
run_test("style-files",
12+
suffix = "-success.R", cmd_args = c('--cache-root=styler')
13+
)
1214
# fail
13-
run_test("style-files", suffix = "-fail-changed.R", error_msg = NA)
15+
run_test("style-files",
16+
suffix = "-fail-changed.R", cmd_args = c('--cache-root=styler'),
17+
error_msg = NA)
1418

15-
run_test("style-files", suffix = "-fail-parse.R", error_msg = "unexpected")
19+
run_test("style-files", suffix = "-fail-parse.R", cmd_args = c('--cache-root=styler'),
20+
error_msg = "unexpected")
1621

1722
# success with cmd args
1823
run_test("style-files",
1924
file_name = "style-files-cmd",
2025
suffix = "-success.R",
21-
cmd_args = c("--style_pkg=styler", "--style_fun=tidyverse_style")
26+
cmd_args = c("--style_pkg=styler", "--style_fun=tidyverse_style", '--cache-root=styler')
2227
)
2328

2429
run_test("style-files",
2530
file_name = "style-files-cmd",
2631
suffix = "-success.R",
27-
cmd_args = c("--scope=spaces")
32+
cmd_args = c("--scope=spaces", '--cache-root=styler')
2833
)
2934

3035
run_test("style-files",
3136
file_name = "style-files-cmd",
3237
suffix = "-success.R",
33-
cmd_args = c('--scope="I(\'spaces\')"')
38+
cmd_args = c('--scope="I(\'spaces\')"', '--cache-root=styler')
3439
)
3540

3641
run_test("style-files",
@@ -39,7 +44,8 @@ run_test("style-files",
3944
cmd_args = c(
4045
'--scope="I(\'spaces\')"',
4146
"--base_indention=0",
42-
"--include_roxygen_examples=TRUE"
47+
"--include_roxygen_examples=TRUE",
48+
'--cache-root=styler'
4349
)
4450
)
4551

@@ -50,35 +56,38 @@ run_test("style-files",
5056
'--scope="I(\'spaces\')"',
5157
"--base_indention=0",
5258
"--include_roxygen_examples=TRUE",
53-
'--reindention="specify_reindention(\'#\')"'
59+
'--reindention="specify_reindention(\'#\')"',
60+
'--cache-root=styler'
5461
)
5562
)
5663

5764
run_test("style-files",
5865
file_name = "style-files",
5966
suffix = "-base-indention-success.R",
60-
cmd_args = c("--base_indention=4")
67+
cmd_args = c("--base_indention=4", '--cache-root=styler')
6168
)
6269

6370
run_test("style-files",
6471
file_name = "style-files",
6572
suffix = "-roxygen-success.R",
66-
cmd_args = c("--include_roxygen_examples=FALSE")
73+
cmd_args = c("--include_roxygen_examples=FALSE", '--cache-root=styler')
6774
)
6875

6976
# fail with cmd args
7077
run_test("style-files",
7178
file_name = "style-files-cmd",
7279
suffix = "-success.R",
7380
error_msg = "scope must be one",
74-
cmd_args = c("--scope=space")
81+
cmd_args = c("--scope=space", '--cache-root=styler')
7582
)
7683

7784
run_test("style-files",
7885
file_name = "style-files-cmd",
7986
suffix = "-fail.R",
8087
error_msg = NA,
81-
cmd_args = c("--style_pkg=styler", "--style_fun=tidyverse_style")
88+
cmd_args = c(
89+
"--style_pkg=styler", "--style_fun=tidyverse_style", '--cache-root=styler'
90+
)
8291
)
8392

8493
### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

tests/testthat/test-zzz.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_that("tests don't write to styler-perm", {
2+
skip_on_cran()
3+
expect_false(fs::file_exists(
4+
fs::path(R.cache::getCacheRootPath(), "styler-perm")
5+
)
6+
)
7+
})

vignettes/available-hooks.Rmd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ In addition, the hook takes the following arguments that are not passed to
8686
* If no permanent `{R.cache}` cache exists, the hook emits a warning which you
8787
can silence with `--no-warn-cache`.
8888

89+
```
90+
id: style-files
91+
args: [--no-warn-cache]
92+
```
93+
94+
95+
* Argument `cache_root` is passed to `options()` to set `styler.cache_root`.
96+
Default value: `styler-perm`. The argument
97+
determines the sub-directory under the {R.cache} cache directory that {styler}
98+
uses. If you want {styler} to auto-clean up caches older than 6 days, set this
99+
to `"styler"`. For more information, see
100+
`help("caching", package = "styler")`.
101+
102+
```
103+
id: style-files
104+
args: [--cache-root=styler]
105+
```
106+
107+
89108
This hook modifies files unless you specify `--dry=fail` (requires
90109
`{styler} > 1.3.2`).
91110

0 commit comments

Comments
 (0)