diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2df6fc02b..4e8f422dc 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -45,8 +45,8 @@ - id: codemeta-description-updated name: codemeta-description-updated description: make sure codemeta.json is in sync with DESCRIPTION. It should be run after use-tidy-description. - entry: inst/hooks/exported/codemeta-description-updated - language: script + entry: Rscript inst/hooks/exported/codemeta-description-updated.R + language: r files: '^DESCRIPTION$' minimum_pre_commit_version: "2.13.0" - id: spell-check diff --git a/inst/hooks/exported/codemeta-description-updated b/inst/hooks/exported/codemeta-description-updated deleted file mode 100755 index fc692923e..000000000 --- a/inst/hooks/exported/codemeta-description-updated +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -# adapted from https://github.com/lorenzwalthert/precommit/blob/f4413cfe6282c84f7176160d06e1560860c8bd3d/inst/hooks/exported/readme-rmd-rendered - -if [[ DESCRIPTION -nt codemeta.json ]] -then - echo >&2 "codemeta.json is out of date; please re-run codemetar::write_codemeta()." - exit 1 -fi diff --git a/inst/hooks/exported/codemeta-description-updated.R b/inst/hooks/exported/codemeta-description-updated.R new file mode 100755 index 000000000..313ba738a --- /dev/null +++ b/inst/hooks/exported/codemeta-description-updated.R @@ -0,0 +1,16 @@ +#!/usr/bin/env Rscript + +# adapted from https://github.com/lorenzwalthert/precommit/blob/f4413cfe6282c84f7176160d06e1560860c8bd3d/inst/hooks/exported/readme-rmd-rendered +if (!file.exists("DESCRIPTION")) { + rlang::abort("No `DESCRIPTION` found in repository.") +} + +if (!file.exists("codemeta.json")) { + rlang::abort("No `codemeta.json` found in repository.") +} + + +codemeta_outdated <- file.info("DESCRIPTION")$mtime > file.info("codemeta.json")$mtime +if (codemeta_outdated) { + rlang::abort("codemeta.json is out of date; please re-run codemetar::write_codemeta().") +} diff --git a/inst/pre-commit-hooks.yaml b/inst/pre-commit-hooks.yaml index 2df6fc02b..4e8f422dc 100644 --- a/inst/pre-commit-hooks.yaml +++ b/inst/pre-commit-hooks.yaml @@ -45,8 +45,8 @@ - id: codemeta-description-updated name: codemeta-description-updated description: make sure codemeta.json is in sync with DESCRIPTION. It should be run after use-tidy-description. - entry: inst/hooks/exported/codemeta-description-updated - language: script + entry: Rscript inst/hooks/exported/codemeta-description-updated.R + language: r files: '^DESCRIPTION$' minimum_pre_commit_version: "2.13.0" - id: spell-check diff --git a/tests/testthat/in/codemeta.json b/tests/testthat/in/codemeta.json new file mode 100644 index 000000000..e69de29bb diff --git a/tests/testthat/test-hooks.R b/tests/testthat/test-hooks.R index 64a0dd880..9bc7d2c31 100644 --- a/tests/testthat/test-hooks.R +++ b/tests/testthat/test-hooks.R @@ -306,6 +306,55 @@ run_test("roxygenize", ) +### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. +### codemeata #### +run_test("codemeta-description-update", + file_name = c("codemeta.json"), + suffix = "", + error_msg = "No `DESCRIPTION` found in repository.", + msg = NULL, +) + +run_test("codemeta-description-update", + file_name = c("DESCRIPTION"), + suffix = "", + error_msg = "No `codemeta.json` found in repository.", + msg = NULL, +) + + +# outdated +run_test("codemeta-description-update", + file_name = c("DESCRIPTION", "codemeta.json"), + suffix = "", + error_msg = "out of date", + msg = 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("codemeta-description-update", + file_name = c("DESCRIPTION", "codemeta.json"), + 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 + } +) + ### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. ### readme-rmd-rendered #### if (has_git()) {