Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .github/workflows/end-to-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
run: |
echo 'one' > README.Rmd
echo 'one' >> codemeta.json
echo 'one' >> CITATION.cff
echo 'one' > README.md
echo "#' some code\n#'\n#' @param here.\n#' @name somethings\nNULL" > R/test.R # overwrite if anything there
brew install pre-commit
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@
language: script
minimum_pre_commit_version: "2.13.0"
files: '^man/|_pkgdown\.yml'
- id: cff-description-updated
name: cff-description-updated
description: make sure `CITATION.cff` is in sync with `DESCRIPTION`. It should be run after use-tidy-description
entry: Rscript inst/hooks/exported/cff-description-updated.R
language: r
files: '^DESCRIPTION$'
minimum_pre_commit_version: "2.13.0"
32 changes: 32 additions & 0 deletions inst/hooks/exported/cff-description-updated.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env Rscript

"A hook to make sure DESCRIPTION hasn't been edited more recently than
CITATION.cff.

Usage:
cff-description-updated [--root=<root_>] <files>...

Options:
--root=<root_> Path relative to the git root that contains the R package
root [default: .].

" -> doc


arguments <- docopt::docopt(doc)
setwd(arguments$root)

# adapted from https://github.com/lorenzwalthert/precommit/blob/d2dcd45f30b6c52469665c89cb7cba53e716a62a/inst/hooks/exported/codemeta-description-updated.R
if (!file.exists("DESCRIPTION")) {
rlang::abort("No `DESCRIPTION` found in repository.")
}

if (!file.exists("CITATION.cff")) {
rlang::abort("No `CITATION.cff` found in repository.")
}


codemeta_outdated <- file.info("DESCRIPTION")$mtime > file.info("CITATION.cff")$mtime
if (codemeta_outdated) {
rlang::abort("CITATION.cff is out of date; please re-run cffr::cff_write().")
}
2 changes: 2 additions & 0 deletions inst/pre-commit-config-pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ repos:
args: [--style_pkg=styler, --style_fun=tidyverse_style]
- id: roxygenize
# codemeta must be above use-tidy-description when both are used
# same for cff-description-updated
# - id: codemeta-description-updated
# - id: cff-description-updated
- id: use-tidy-description
- id: spell-check
exclude: >
Expand Down
7 changes: 7 additions & 0 deletions inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@
language: script
minimum_pre_commit_version: "2.13.0"
files: '^man/|_pkgdown\.yml'
- id: cff-description-updated
name: cff-description-updated
description: make sure `CITATION.cff` is in sync with `DESCRIPTION`. It should be run after use-tidy-description
entry: Rscript inst/hooks/exported/cff-description-updated.R
language: r
files: '^DESCRIPTION$'
minimum_pre_commit_version: "2.13.0"
Empty file added tests/testthat/in/CITATION.cff
Empty file.
88 changes: 88 additions & 0 deletions tests/testthat/test-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,94 @@ run_test("codemeta-description-update",
}
)

### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
### codemeata ####
run_test("cff-description-updated",
file_name = c("CITATION.cff"),
suffix = "",
std_err = "No `DESCRIPTION` found in repository.",
std_out = NULL,
)

run_test("cff-description-updated",
file_name = c("DESCRIPTION"),
suffix = "",
std_err = "No `CITATION.cff` found in repository.",
std_out = NULL,
)

# outdated
run_test("cff-description-updated",
file_name = c("DESCRIPTION", "CITATION.cff"),
suffix = "",
std_err = "out of date",
std_out = NULL,
file_transformer = function(files) {
if (length(files) > 1) {
# transformer is called once on all files and once per file
content_2 <- readLines(files[1])
Sys.sleep(2)
writeLines(content_2, files[1])
}
files
}
)

# succeed
run_test("cff-description-updated",
file_name = c("DESCRIPTION", "CITATION.cff"),
suffix = "",
file_transformer = function(files) {
if (length(files) > 1) {
# transformer is called once on all files and once per file
content_2 <- readLines(files[2])
Sys.sleep(2)
writeLines(content_2, files[2])
}
files
}
)

# succeed in correct root
run_test("cff-description-updated",
file_name = c(
"rpkg/DESCRIPTION" = "DESCRIPTION",
"rpkg/CITATION.cff" = "CITATION.cff"
),
cmd_args = "--root=rpkg",
suffix = "",
file_transformer = function(files) {
if (length(files) > 1) {
# transformer is called once on all files and once per file
content_2 <- readLines(files[2])
Sys.sleep(2)
writeLines(content_2, files[2])
}
files
}
)

# # fail in wrong root
run_test("cff-description-updated",
file_name = c(
"rpkg/DESCRIPTION" = "DESCRIPTION",
"rpkg/CITATION.cff" = "CITATION.cff",
"rpkg2/CITATION.cff" = "README.md"
),
cmd_args = "--root=rpkg2",
std_err = "No `DESCRIPTION` found in repository.",
suffix = "",
file_transformer = function(files) {
if (length(files) > 1) {
# transformer is called once on all files and once per file
content_2 <- readLines(files[2])
Sys.sleep(2)
writeLines(content_2, files[2])
}
files
}
)

### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
### pgkdown check ####

Expand Down
20 changes: 20 additions & 0 deletions vignettes/available-hooks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,26 @@ This hook does not modify any file.
repos, the git and R package root coincide. Added in version
0.3.3.00000.

## `cff-description-updated`

Make sure `DESCRIPTION` hasn't been edited more recently than
`CITATION.cff`,

i.e. remind you to run `cffr::cff_write()` in order to keep
`CITATION.cff` in sync with `DESCRIPTION`.

This hook does not modify any file.

**Arguments**

id: cff-description-updated
args: [--root=<R package root>]

<!-- Add text about which version this was added after bumping version -->

- Argument `root` specifies the directory in the git repo that
contains the R package. Defaults to `.` since for most R package git
repos, the git and R package root coincide.

## `pkgdown`

Expand Down