diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dae870181..ddb93760b 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -104,3 +107,9 @@ repos: language: fail files: '\.Rhistory|\.csv|\.RData|\.Rds|\.rds$' # `exclude: ` 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$' diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 6e69987bb..8ce87b73a 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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" diff --git a/NEWS.md b/NEWS.md index c4c38b04e..3fe5f208f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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** @@ -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 diff --git a/inst/pre-commit-config-pkg.yaml b/inst/pre-commit-config-pkg.yaml index 2fbe86971..0eb322699 100644 --- a/inst/pre-commit-config-pkg.yaml +++ b/inst/pre-commit-config-pkg.yaml @@ -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 diff --git a/inst/pre-commit-config-proj.yaml b/inst/pre-commit-config-proj.yaml index b4e239508..f14f004d9 100644 --- a/inst/pre-commit-config-proj.yaml +++ b/inst/pre-commit-config-proj.yaml @@ -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 diff --git a/inst/pre-commit-hooks.yaml b/inst/pre-commit-hooks.yaml index 6e69987bb..8ce87b73a 100644 --- a/inst/pre-commit-hooks.yaml +++ b/inst/pre-commit-hooks.yaml @@ -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" diff --git a/inst/spell-check-ordered-exclude.R b/inst/spell-check-ordered-exclude.R new file mode 100755 index 000000000..821a66adc --- /dev/null +++ b/inst/spell-check-ordered-exclude.R @@ -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) diff --git a/tests/testthat/test-hooks-regex-exclude.R b/tests/testthat/test-hooks-regex-exclude.R index 78c6d139e..3d8e0bc2b 100644 --- a/tests/testthat/test-hooks-regex-exclude.R +++ b/tests/testthat/test-hooks-regex-exclude.R @@ -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( @@ -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)^(", ")$")