From 0a1db576f2156d11b7e822a1ef60b30244915faf Mon Sep 17 00:00:00 2001 From: Paul Wildenhain Date: Tue, 30 Nov 2021 10:30:46 -0500 Subject: [PATCH 1/9] :see_no_evil: Ignore Rprofile --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e0392f267..5240de782 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .Rhistory .Rproj.user .Ruserdata +.Rprofile docs/ inst/doc scratch/ From d2091d79fab54d0f734a485c78279edd7d59a3f0 Mon Sep 17 00:00:00 2001 From: Paul Wildenhain Date: Tue, 30 Nov 2021 11:23:23 -0500 Subject: [PATCH 2/9] :white_check_mark: Add new tests + files --- tests/testthat/in/parsable-R-fail.Rmd | 8 ++++++++ tests/testthat/in/parsable-R-success.Rmd | 8 ++++++++ tests/testthat/test-all.R | 12 ++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/in/parsable-R-fail.Rmd create mode 100644 tests/testthat/in/parsable-R-success.Rmd diff --git a/tests/testthat/in/parsable-R-fail.Rmd b/tests/testthat/in/parsable-R-fail.Rmd new file mode 100644 index 000000000..ffcbbee33 --- /dev/null +++ b/tests/testthat/in/parsable-R-fail.Rmd @@ -0,0 +1,8 @@ +--- +title: "Parsable Rmd Fail" +output: rmarkdown::html_document +--- + +```{r} +1 1j1j öj1 +``` diff --git a/tests/testthat/in/parsable-R-success.Rmd b/tests/testthat/in/parsable-R-success.Rmd new file mode 100644 index 000000000..70ce092a7 --- /dev/null +++ b/tests/testthat/in/parsable-R-success.Rmd @@ -0,0 +1,8 @@ +--- +title: "Parsable Rmd Success" +output: rmarkdown::html_document +--- + +```{r} +call() +``` diff --git a/tests/testthat/test-all.R b/tests/testthat/test-all.R index 34ecf644d..6ad2609ce 100644 --- a/tests/testthat/test-all.R +++ b/tests/testthat/test-all.R @@ -104,13 +104,21 @@ run_test( # success run_test("parsable-R", - suffix = "-success.R", - error_msg = NULL + suffix = "-success.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 #### From f1cc91cade86df67c080e80658c4c008a1c97069 Mon Sep 17 00:00:00 2001 From: Paul Wildenhain Date: Tue, 30 Nov 2021 11:27:16 -0500 Subject: [PATCH 3/9] :recycle: Add Rmds to parsable-R hook --- .pre-commit-hooks.yaml | 2 +- inst/hooks/exported/parsable-R.R | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 7040f9b99..2df6fc02b 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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 diff --git a/inst/hooks/exported/parsable-R.R b/inst/hooks/exported/parsable-R.R index ae2c1a997..35a426262 100755 --- a/inst/hooks/exported/parsable-R.R +++ b/inst/hooks/exported/parsable-R.R @@ -2,6 +2,17 @@ files <- commandArgs(trailing = TRUE) out <- lapply(files, function(path) { + is_rmd <- grepl("\\.[rR]md$", path) + if (is_rmd) { + dir <- tempdir() + path <- knitr::purl( + input = path, + output = paste0(dir, "/file.R"), + quiet = TRUE, + documentation = FALSE + ) + } + tryCatch( parse(path), error = function(x) stop("File ", path, " is not parsable", call. = FALSE) From 92a25568285738fd04bba6c9b9efb9c1ba874d4a Mon Sep 17 00:00:00 2001 From: Paul Wildenhain Date: Tue, 30 Nov 2021 11:30:59 -0500 Subject: [PATCH 4/9] :lipstick: Style tests --- tests/testthat/test-all.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-all.R b/tests/testthat/test-all.R index 6ad2609ce..7230411b4 100644 --- a/tests/testthat/test-all.R +++ b/tests/testthat/test-all.R @@ -104,13 +104,13 @@ run_test( # success run_test("parsable-R", - suffix = "-success.R", - error_msg = NULL + suffix = "-success.R", + error_msg = NULL ) run_test("parsable-R", - suffix = "-success.Rmd", - error_msg = NULL + suffix = "-success.Rmd", + error_msg = NULL ) # failure From 22b3603a08a610fda20c397f178700f6f8a6d73c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 30 Nov 2021 16:32:32 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- inst/pre-commit-hooks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/pre-commit-hooks.yaml b/inst/pre-commit-hooks.yaml index 7040f9b99..2df6fc02b 100644 --- a/inst/pre-commit-hooks.yaml +++ b/inst/pre-commit-hooks.yaml @@ -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 From cbe089925f17b91714a9c055fa9f4a90c7d19932 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 30 Nov 2021 19:56:58 +0100 Subject: [PATCH 6/9] use tempfile directly --- inst/hooks/exported/parsable-R.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inst/hooks/exported/parsable-R.R b/inst/hooks/exported/parsable-R.R index 35a426262..ad8c4a4d5 100755 --- a/inst/hooks/exported/parsable-R.R +++ b/inst/hooks/exported/parsable-R.R @@ -4,10 +4,9 @@ files <- commandArgs(trailing = TRUE) out <- lapply(files, function(path) { is_rmd <- grepl("\\.[rR]md$", path) if (is_rmd) { - dir <- tempdir() path <- knitr::purl( input = path, - output = paste0(dir, "/file.R"), + output = tempfile(fileext = ".R"), quiet = TRUE, documentation = FALSE ) From 2f61295e80b1c8280dda7f81a177d74f986cb8b7 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 30 Nov 2021 19:58:20 +0100 Subject: [PATCH 7/9] also update docs --- vignettes/available-hooks.Rmd | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vignettes/available-hooks.Rmd b/vignettes/available-hooks.Rmd index a57758237..beab4c81f 100644 --- a/vignettes/available-hooks.Rmd +++ b/vignettes/available-hooks.Rmd @@ -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. From 4cdcb2204b43ce5b3af8f33d779e4e074d5368b5 Mon Sep 17 00:00:00 2001 From: Paul Wildenhain Date: Tue, 30 Nov 2021 14:13:51 -0500 Subject: [PATCH 8/9] :white_check_mark: Add tests to test-hooks.R --- tests/testthat/test-hooks.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/testthat/test-hooks.R b/tests/testthat/test-hooks.R index a9768d3ad..c7feeaea7 100644 --- a/tests/testthat/test-hooks.R +++ b/tests/testthat/test-hooks.R @@ -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 #### From 3eceb3ff51f4de40d4357540f4116b7bae3ad4be Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 30 Nov 2021 20:19:32 +0100 Subject: [PATCH 9/9] drop test in wrong file --- tests/testthat/test-all.R | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/testthat/test-all.R b/tests/testthat/test-all.R index 7230411b4..34ecf644d 100644 --- a/tests/testthat/test-all.R +++ b/tests/testthat/test-all.R @@ -108,17 +108,9 @@ 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 ####