Skip to content

Commit

Permalink
Merge pull request #325 from pwildenhain/add-parsable-rmd
Browse files Browse the repository at this point in the history
- Add  Rmarkdown files to parsable-R hook (#325).
  • Loading branch information
lorenzwalthert authored Nov 30, 2021
2 parents 937542a + 3eceb3f commit 78ea6e8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.Rhistory
.Rproj.user
.Ruserdata
.Rprofile
docs/
inst/doc
scratch/
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
description: check if a .R file is parsable
entry: Rscript inst/hooks/exported/parsable-R.R
language: r
files: '\.[rR]$'
files: '\.[rR](md)?$'
minimum_pre_commit_version: "2.13.0"
- id: readme-rmd-rendered
name: readme-rmd-rendered
Expand Down
10 changes: 10 additions & 0 deletions inst/hooks/exported/parsable-R.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
files <- commandArgs(trailing = TRUE)

out <- lapply(files, function(path) {
is_rmd <- grepl("\\.[rR]md$", path)
if (is_rmd) {
path <- knitr::purl(
input = path,
output = tempfile(fileext = ".R"),
quiet = TRUE,
documentation = FALSE
)
}

tryCatch(
parse(path),
error = function(x) stop("File ", path, " is not parsable", call. = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion inst/pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
description: check if a .R file is parsable
entry: Rscript inst/hooks/exported/parsable-R.R
language: r
files: '\.[rR]$'
files: '\.[rR](md)?$'
minimum_pre_commit_version: "2.13.0"
- id: readme-rmd-rendered
name: readme-rmd-rendered
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/in/parsable-R-fail.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Parsable Rmd Fail"
output: rmarkdown::html_document
---

```{r}
1 1j1j öj1
```
8 changes: 8 additions & 0 deletions tests/testthat/in/parsable-R-success.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Parsable Rmd Success"
output: rmarkdown::html_document
---

```{r}
call()
```
6 changes: 6 additions & 0 deletions tests/testthat/test-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@ run_test("parsable-R",
error_msg = NULL
)

run_test("parsable-R",
suffix = "-success.Rmd",
error_msg = NULL
)

# failure
run_test("parsable-R", suffix = "-fail.R", error_msg = "not parsable")

run_test("parsable-R", suffix = "-fail.Rmd", error_msg = "not parsable")

### . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
### spell-check ####
Expand Down
5 changes: 3 additions & 2 deletions vignettes/available-hooks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ This hook does not modify files.

## `parsable-R`

Checks if your `.R` files are "valid" R code by checking if running `parse()` on
them returns an error.
Checks if your `.R` and `.Rmd` files are "valid" R code by checking if running
`parse()` on them (or their `knitr::purl()`ed output for `.Rmd`) returns an
error.

This hook does not modify files.

Expand Down

0 comments on commit 78ea6e8

Please sign in to comment.