Skip to content

make codemeta hook language: r and write test #350

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
Dec 15, 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
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions inst/hooks/exported/codemeta-description-updated

This file was deleted.

16 changes: 16 additions & 0 deletions inst/hooks/exported/codemeta-description-updated.R
Original file line number Diff line number Diff line change
@@ -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().")
}
4 changes: 2 additions & 2 deletions inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added tests/testthat/in/codemeta.json
Empty file.
49 changes: 49 additions & 0 deletions tests/testthat/test-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down