Skip to content

More exclusion for spelling hook #276

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

Merged
merged 2 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,32 @@ repos:
- id: spell-check
exclude: >
(?x)^(
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
.*\.RData|
(.*/|)\.gitignore|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
data/.*|
inst/bin/.*|
inst/pre-commit-.*
inst/pre-commit-.*|
)$
- id: readme-rmd-rendered
- id: parsable-R
Expand Down Expand Up @@ -104,3 +107,9 @@ repos:
language: fail
files: '\.Rhistory|\.csv|\.RData|\.Rds|\.rds$'
# `exclude: <regex>` to allow committing specific files.
- id: spell-check-ordered-exclude
name: Ordered regex pattern for spell-check exclusion
description: Ensure alphabetical order in `exclude:` key of spell check.
entry: inst/spell-check-ordered-exclude.R
language: script
files: '^(.*/|)\.?pre-commit-config.*\.yaml$'
34 changes: 19 additions & 15 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,30 @@
language: r
exclude: >
(?x)^(
data/.*|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
.*\.RData
(.*/|)\.gitignore|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
)$
require_serial: true
minimum_pre_commit_version: "2.12"
Expand Down
8 changes: 6 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ installation.
- Warnings are no longer promoted to errors in the styler hook, which is
particularly relevant for the apparently random error
`Unknown or uninitialised column: text`(#268).
- `deps-in-desc` now checks `.Rprofile`, `.Rmd` and `.Rnw` files in addition to
`.R` files (#216).

**Minor changes**

Expand All @@ -52,8 +54,10 @@ installation.
if there are no changes in roxygen comments (#214).
- `{renv}` infra files are not checked anymore by default in the template config
files (#237).
- `deps-in-desc` now checks `.Rprofile`, `.Rmd` and `.Rnw` files in addition to
`.R` files (#216).
- `.png`, `.jpeg`, `.pdf` and files in `.github/workflows` are no longer
spell-checked in the template config file (#276).
- All sub-patterns in the `exclude:` pattern of the spell check hook are now
ordered alphabetically (#276).
- The location of the pre-commit executable is now also recognized on Apple
Silicon when installed with Homebrew (#240).
- The `deps-in-desc` hook now points to the hook argument
Expand Down
34 changes: 19 additions & 15 deletions inst/pre-commit-config-pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ repos:
- id: spell-check
exclude: >
(?x)^(
data/.*|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
.*\.RData
(.*/|)\.gitignore|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
)$
- id: lintr
- id: readme-rmd-rendered
Expand Down
34 changes: 19 additions & 15 deletions inst/pre-commit-config-proj.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ repos:
- id: spell-check
exclude: >
(?x)^(
data/.*|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
.*\.RData
(.*/|)\.gitignore|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
)$
- id: lintr
- id: readme-rmd-rendered
Expand Down
34 changes: 19 additions & 15 deletions inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,30 @@
language: r
exclude: >
(?x)^(
data/.*|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
.*\.RData
(.*/|)\.gitignore|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
)$
require_serial: true
minimum_pre_commit_version: "2.12"
Expand Down
29 changes: 29 additions & 0 deletions inst/spell-check-ordered-exclude.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env Rscript
# This hook checks that the `exclude:` key in the pre-commit config files
# are alphabetically ordered. This is helpful for manual searching plus also
# groups the patterns, i.e. extensions, files everywhere, directories
library(magrittr)
args <- commandArgs(trailingOnly = TRUE)
run_one_file <- function(file) {
config <- yaml::read_yaml(file) # pre-commit filter
print(config)
ours <- which(purrr::map_chr(config$repos, "repo") == "https://github.com/lorenzwalthert/precommit")
nme <- purrr::map_chr(config$repos[[ours]]$hooks, "id")

regex <- config$repos[[1]]$hooks[[which(nme == "spell-check")]]$exclude %>%
strsplit(" +") %>%
unlist()

without_mask <- regex[c(-1, -length(regex))]
if (any(without_mask != sort(without_mask))) {
cat(paste0(
"regular expressions not sorted for spell check hook, paste this ",
"into the pre-commit config file: under the `exclude:` key:"
))

cat(c("", regex[1], sort(without_mask), regex[length(regex)]), sep = "\n ")
rlang::abort("Execution halted.")
}
}

purrr::walk(args, run_one_file)
27 changes: 20 additions & 7 deletions tests/testthat/test-hooks-regex-exclude.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
test_that("exclude regex for spell check hook matches expected files", {
skip_if(not_conda())
skip_if(on_cran())

# declare
re <- reticulate::import("re")
pattern <- readLines(system.file("pre-commit-hooks.yaml", package = "precommit")) %>%
gsub("^ *exclude *: *>", " exclude: |", .) %>%
yaml::yaml.load() %>%
purrr::keep(~ .x$id == "spell-check") %>%
magrittr::extract2(1) %>%
magrittr::extract2("exclude")

pattern_read <- function(file) {
readLines(file) %>%
gsub("^ *exclude *: *>", " exclude: |", .) %>%
yaml::yaml.load() %>%
purrr::keep(~ .x$id == "spell-check") %>%
magrittr::extract2(1) %>%
magrittr::extract2("exclude")
}

is_match <- function(pattern, x) {
matches <- purrr::map_lgl(
Expand Down Expand Up @@ -42,8 +47,16 @@ test_that("exclude regex for spell check hook matches expected files", {
"more/data.rds",
"things/xx.Rds",
".pre-commit-",
".Rproj"
".Rproj",
"some/X.pdf",
"figure.png",
"pciture.jpeg",
".github/workflows/r-cmd-check.yaml"
)

# run
pattern <- pattern_read(system.file("pre-commit-hooks.yaml", package = "precommit"))

individual_patterns <- setdiff(
unlist(strsplit(pattern, "\n", fixed = TRUE)),
c("(?x)^(", ")$")
Expand Down