Skip to content

Commit

Permalink
move quarto render into cleanup script (#274)
Browse files Browse the repository at this point in the history
fix #272

move `quarto render` into R CMD BUILD:
```
❯ R CMD BUILD .
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘tlg.catalog.pkg’:
* checking DESCRIPTION meta-information ... OK
* running ‘cleanup’                                                            <<<<------ HERE!
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘tlg.catalog.pkg_0.1.0.9029.tar.gz’
```

After `quarto render` we remove the symlink to the book.
As a result, we have refreshed files for snapshot tests and also we
don't have a book files that R CMD CHECK complains about.

---------

Co-authored-by: Marcin <[email protected]>
  • Loading branch information
pawelru and m7pr authored Oct 25, 2024
1 parent 58a6ba0 commit f238d68
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
additional-env-vars: |
NOT_CRAN=true
QUARTO_PROFILE=development
TLG_CATALOG_PKG_BUILD_RENDER=TRUE
concurrency-group: development
r-cmd-stable:
if: github.event_name != 'push'
Expand All @@ -45,6 +46,7 @@ jobs:
additional-env-vars: |
NOT_CRAN=true
QUARTO_PROFILE=stable
TLG_CATALOG_PKG_BUILD_RENDER=TRUE
concurrency-group: stable
linter:
if: github.event_name != 'push'
Expand Down
4 changes: 2 additions & 2 deletions book/_utils/envir_hook.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ knitr::knit_hooks$set(test = function(options, envir, before = FALSE) {
if (isFALSE(options$eval)) {
return(NULL)
}
if (isFALSE(testthat::is_checking() || testthat::is_testing())) {
if (isFALSE(testthat::is_checking() || testthat::is_testing() || as.logical(Sys.getenv("TLG_CATALOG_PKG_BUILD_RENDER", "FALSE")))) {
return(NULL)
}
if (isFALSE(before)) {
Expand All @@ -28,7 +28,7 @@ knitr::knit_hooks$set(test = function(options, envir, before = FALSE) {
knitr::opts_template$set(
include_if_testing = list(
eval = isTRUE(testthat::is_checking() || testthat::is_testing()),
eval = isTRUE(testthat::is_checking() || testthat::is_testing() || as.logical(Sys.getenv("TLG_CATALOG_PKG_BUILD_RENDER", "FALSE"))),
cache = FALSE
),
skip_if_testing = list(
Expand Down
6 changes: 5 additions & 1 deletion book/_utils/save_results.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#| renv.ignore: TRUE
if (exists("tenv")) {
filename <- paste0(gsub("\\.rmarkdown$", "", knitr::current_input(dir = TRUE)), ".rds")
filepath <- gsub(normalizePath(Sys.getenv("QUARTO_PROJECT_DIR")), file.path(Sys.getenv("QUARTO_TESTTHAT_DATA_PATH"), Sys.getenv("QUARTO_PROFILE")), filename)
filepath <- gsub(
normalizePath(Sys.getenv("QUARTO_PROJECT_DIR")),
file.path(Sys.getenv("TLG_CATALOG_PKG_TEST_DATA_PATH"), Sys.getenv("QUARTO_PROFILE")),
filename
)
if (!dir.exists(dirname(filepath))) {
dir.create(dirname(filepath), recursive = TRUE)
}
Expand Down
20 changes: 20 additions & 0 deletions package/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# See https://github.com/insightsengineering/tlg-catalog/issues/272
# This script addresses R CMD CHECK notes caused by the tests/testthat/_book symlink.
# It renders the book (which saves the output for snapshot testing) and removes the symlink.
# Executed during the R CMD BUILD "running ‘cleanup’" step, it ensures a smaller package tarball without the symlink and eliminates R CMD CHECK notes.

TLG_CATALOG_PKG_TEST_DATA_PATH=$(realpath "./tests/testthat/_data")
export TLG_CATALOG_PKG_TEST_DATA_PATH
if [ "$TLG_CATALOG_PKG_BUILD_RENDER" = "TRUE" ]; then
echo "Rendering Quarto book..."
quarto render tests/testthat/_book/ --no-cache
echo "Quarto book rendered."
else
echo "Skipping Quarto book rendering."
fi

rm -rf tests/testthat/_book

exit 0
2 changes: 1 addition & 1 deletion package/tests/testthat/_data/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*
!.gitignore
!.rbuildkeep
!README.md
Empty file.
3 changes: 3 additions & 0 deletions package/tests/testthat/_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# README

This is an empty file to keep parent dir not empty.
2 changes: 1 addition & 1 deletion package/tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_article <- function(article_path) {
if (isTRUE(if_render_articles)) {
if (isTRUE(if_render_articles) && isTRUE(if_book_exists)) {
quarto::quarto_render(
file.path(test_book_path, article_path),
as_job = FALSE,
Expand Down
11 changes: 6 additions & 5 deletions package/tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_profile <- Sys.getenv("QUARTO_PROFILE", "stable")
test_book_path <- testthat::test_path("_book")
test_data_path <- testthat::test_path("_data")
Sys.setenv("QUARTO_TESTTHAT_DATA_PATH" = normalizePath(test_data_path))
Sys.setenv("TLG_CATALOG_PKG_TEST_DATA_PATH" = normalizePath(test_data_path))

# Flags for developers
# use it if you want to disable render of book or render individual articles instead
Expand All @@ -10,8 +10,9 @@ Sys.setenv("QUARTO_TESTTHAT_DATA_PATH" = normalizePath(test_data_path))
# (ii) set if_render_article to TRUE
# (iii) assure proper value of `test_profile` ("development" or "stable")
# (iv) run `devtools::test(filter = "<article_name>")`
if_render_book <- TRUE # Remember to change this to FALSE if you want to render articles (otherwise loop)
if_render_articles <- !if_render_book
if_book_exists <- file.exists(test_book_path)
if_render_book <- FALSE # Remember to change this to FALSE if you want to render articles (otherwise loop)
if_render_articles <- FALSE
if_test_plots <- FALSE # Additional option for optional skip of local plot tests
# Example for render articles (NOTE: KEEP THESE COMMENTED WHEN RUNNING!!):
# setwd("package")
Expand All @@ -26,7 +27,7 @@ if (isTRUE(if_render_book) && isTRUE(if_render_articles)) {
stop("Render both book and articles at the same time is not efficient! Please set one of them to FALSE.")
}

if (isTRUE(if_render_book)) {
if (isTRUE(if_render_book) && isTRUE(if_book_exists)) {
cat("Start rendering the book...\n")
quarto::quarto_render(
test_book_path,
Expand All @@ -41,4 +42,4 @@ if (isTRUE(if_render_book)) {

# clean up
withr::defer(Sys.unsetenv("QUARTO_PROFILE"), testthat::teardown_env())
withr::defer(Sys.unsetenv("QUARTO_TESTTHAT_DATA_PATH"), testthat::teardown_env())
withr::defer(Sys.unsetenv("TLG_CATALOG_PKG_TEST_DATA_PATH"), testthat::teardown_env())

0 comments on commit f238d68

Please sign in to comment.