From f238d68aa6228b21f042e755333d992a3184d23e Mon Sep 17 00:00:00 2001 From: Pawel Rucki <12943682+pawelru@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:23:13 +0200 Subject: [PATCH] move quarto render into cleanup script (#274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix https://github.com/insightsengineering/tlg-catalog/issues/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 <133694481+m7pr@users.noreply.github.com> --- .github/workflows/check.yml | 2 ++ book/_utils/envir_hook.qmd | 4 ++-- book/_utils/save_results.qmd | 6 +++++- package/cleanup | 20 ++++++++++++++++++++ package/tests/testthat/_data/.gitignore | 2 +- package/tests/testthat/_data/.rbuildkeep | 0 package/tests/testthat/_data/README.md | 3 +++ package/tests/testthat/helper.R | 2 +- package/tests/testthat/setup.R | 11 ++++++----- 9 files changed, 40 insertions(+), 10 deletions(-) create mode 100755 package/cleanup delete mode 100644 package/tests/testthat/_data/.rbuildkeep create mode 100644 package/tests/testthat/_data/README.md diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index bf47198a4c..f85af9bb73 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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' @@ -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' diff --git a/book/_utils/envir_hook.qmd b/book/_utils/envir_hook.qmd index bd162f3776..d3e384ec2b 100644 --- a/book/_utils/envir_hook.qmd +++ b/book/_utils/envir_hook.qmd @@ -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)) { @@ -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( diff --git a/book/_utils/save_results.qmd b/book/_utils/save_results.qmd index 3d28337a8d..80332c68d8 100644 --- a/book/_utils/save_results.qmd +++ b/book/_utils/save_results.qmd @@ -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) } diff --git a/package/cleanup b/package/cleanup new file mode 100755 index 0000000000..0c60970014 --- /dev/null +++ b/package/cleanup @@ -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 \ No newline at end of file diff --git a/package/tests/testthat/_data/.gitignore b/package/tests/testthat/_data/.gitignore index 63f2535de6..e5af87e9b5 100644 --- a/package/tests/testthat/_data/.gitignore +++ b/package/tests/testthat/_data/.gitignore @@ -1,3 +1,3 @@ * !.gitignore -!.rbuildkeep \ No newline at end of file +!README.md \ No newline at end of file diff --git a/package/tests/testthat/_data/.rbuildkeep b/package/tests/testthat/_data/.rbuildkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/package/tests/testthat/_data/README.md b/package/tests/testthat/_data/README.md new file mode 100644 index 0000000000..094be37c3f --- /dev/null +++ b/package/tests/testthat/_data/README.md @@ -0,0 +1,3 @@ +# README + +This is an empty file to keep parent dir not empty. diff --git a/package/tests/testthat/helper.R b/package/tests/testthat/helper.R index 3afbb520be..bfe5f93536 100644 --- a/package/tests/testthat/helper.R +++ b/package/tests/testthat/helper.R @@ -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, diff --git a/package/tests/testthat/setup.R b/package/tests/testthat/setup.R index 1248826f75..89969cc438 100644 --- a/package/tests/testthat/setup.R +++ b/package/tests/testthat/setup.R @@ -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 @@ -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 = "")` -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") @@ -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, @@ -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())