From 24f2f8ae6c96206abb5682c8fbb4a8eaeeacb5ea Mon Sep 17 00:00:00 2001 From: Pawel Rucki <12943682+pawelru@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:45:23 +0200 Subject: [PATCH 1/3] move quarto render into cleanup script --- book/_utils/envir_hook.qmd | 4 ++-- book/_utils/save_results.qmd | 6 +++++- package/cleanup | 20 ++++++++++++++++++++ package/tests/testthat/helper.R | 2 +- package/tests/testthat/setup.R | 11 ++++++----- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100755 package/cleanup 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..19b89b32d2 --- /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. + +export TLG_CATALOG_PKG_BUILD_RENDER=TRUE +export TLG_CATALOG_PKG_TEST_DATA_PATH=$(realpath "./tests/testthat/_data") +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/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()) From f3ca63966783fbf5353751dba34f63616db1a5de Mon Sep 17 00:00:00 2001 From: Pawel Rucki <12943682+pawelru@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:03:18 +0200 Subject: [PATCH 2/3] fix notes --- package/cleanup | 3 ++- package/tests/testthat/_data/.gitignore | 2 +- package/tests/testthat/_data/.rbuildkeep | 0 package/tests/testthat/_data/README.md | 3 +++ 4 files changed, 6 insertions(+), 2 deletions(-) delete mode 100644 package/tests/testthat/_data/.rbuildkeep create mode 100644 package/tests/testthat/_data/README.md diff --git a/package/cleanup b/package/cleanup index 19b89b32d2..1a51c8d3d8 100755 --- a/package/cleanup +++ b/package/cleanup @@ -6,7 +6,8 @@ # Executed during the R CMD BUILD "running ‘cleanup’" step, it ensures a smaller package tarball without the symlink and eliminates R CMD CHECK notes. export TLG_CATALOG_PKG_BUILD_RENDER=TRUE -export TLG_CATALOG_PKG_TEST_DATA_PATH=$(realpath "./tests/testthat/_data") +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 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. From 582498a9e2438f3509f70340df7642d0fa156d63 Mon Sep 17 00:00:00 2001 From: Pawel Rucki <12943682+pawelru@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:38:40 +0200 Subject: [PATCH 3/3] move flag definition to the workflow --- .github/workflows/check.yml | 2 ++ package/cleanup | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) 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/package/cleanup b/package/cleanup index 1a51c8d3d8..0c60970014 100755 --- a/package/cleanup +++ b/package/cleanup @@ -5,7 +5,6 @@ # 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. -export TLG_CATALOG_PKG_BUILD_RENDER=TRUE 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