From fe1414925f38c975f121cb78d4c004ce29b52170 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Jan 2024 15:25:56 +0100 Subject: [PATCH 1/6] Rewrite skip_if_(no)_quarto helpers to account for version --- tests/testthat/helper.R | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index d5fe0b57..8a871048 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -1,9 +1,22 @@ -skip_if_no_quarto <- function() { - skip_if(is.null(quarto_path())) +# Use to test quarto availability or version lower than +skip_if_no_quarto <- function(ver = NULL) { + skip_if(is.null(quarto_path()), message = "Quarto is not available") + skip_if( + quarto_version() < ver, + message = sprintf("Version of quarto is lower than %s.", ver) + ) } -skip_if_quarto <- function() { - skip_if(!is.null(quarto_path())) +# Use to test quarto greater than +skip_if_quarto <- function(ver = NULL) { + if (is.null(ver)) { + skip_if(!is.null(quarto_path()), message = "Quarto is available") + } else { + skip_if( + quarto_version() >= ver, + message = sprintf("Version of quarto is greater than or equal %s.", ver) + ) + } } local_qmd_file <- function(..., .env = parent.frame()) { From 49235a75c30da7caee00e2cde4d707a54a14ca6a Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Jan 2024 16:18:36 +0100 Subject: [PATCH 2/6] Add `quarto use binder` and tests --- R/use.R | 16 ++++++++++++ .../testthat/_snaps/quarto-before-1.4/use.md | 9 +++++++ tests/testthat/helper.R | 25 +++++++++++++++--- tests/testthat/test-quarto.R | 2 +- tests/testthat/test-use.R | 26 +++++++++++++++++++ 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/quarto-before-1.4/use.md diff --git a/R/use.R b/R/use.R index 86f07523..e6065f18 100644 --- a/R/use.R +++ b/R/use.R @@ -33,6 +33,22 @@ quarto_use_template <- function(template, no_prompt = FALSE) { invisible() } +quarto_use_binder <- function(no_prompt = FALSE) { + if (quarto_version() < 1.4) { + cli::cli_abort(c( + "{.code quarto use binder} has been added in Quarto 1.4. See {.url https://quarto.org/docs/projects/binder.html}.", + i = "You are using {.strong {quarto_version()}} from {.file {quarto_path()}}.") + ) + } + + quarto_bin <- find_quarto() + + args <- c("binder", if (no_prompt) "--no-prompt") + + quarto_use(args, quarto_bin = quarto_bin, echo = TRUE) + +} + quarto_use <- function(args = character(), ...) { quarto_run_what("use", args = args, ...) } diff --git a/tests/testthat/_snaps/quarto-before-1.4/use.md b/tests/testthat/_snaps/quarto-before-1.4/use.md new file mode 100644 index 00000000..9d02e3bc --- /dev/null +++ b/tests/testthat/_snaps/quarto-before-1.4/use.md @@ -0,0 +1,9 @@ +# quarto_use_binder errors < 1.4 + + Code + quarto_use_binder() + Condition + Error in `quarto_use_binder()`: + ! `quarto use binder` has been added in Quarto 1.4. See . + i You are using 1.3.450 from ''. + diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 8a871048..8baf86af 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -30,6 +30,17 @@ local_qmd_file <- function(..., .env = parent.frame()) { path } +local_quarto_project <- function(..., yml = list(project = list(type = "default")), .env = parent.frame()) { + skip_if_not_installed("withr") + skip_if_not_installed("yaml") + # Create a directory and a .qmd file + path <- local_qmd_file(..., .env = .env) + withr::local_dir(project <- dirname(path)) + write_yaml(yml, "_quarto.yml") + # return the project dir + project +} + .render <- function(input, output_file = NULL, ..., .env = parent.frame()) { skip_if_no_quarto() skip_if_not_installed("withr") @@ -70,7 +81,15 @@ expect_snapshot_qmd_output <- function(name, input, output_file = NULL, ...) { } -transform_quarto_cli_in_output <- function(lines) { - # it will be quarto.exe only on windows - gsub("quarto.exe", "quarto", lines) +transform_quarto_cli_in_output <- function(full_path = FALSE) { + return( + function(lines) { + if (full_path) { + gsub(find_quarto(), "", lines, fixed = TRUE) + } else { + # it will be quarto.exe only on windows + gsub("quarto.exe", "quarto", lines) + } + } + ) } diff --git a/tests/testthat/test-quarto.R b/tests/testthat/test-quarto.R index 45b8e9b8..b374462d 100644 --- a/tests/testthat/test-quarto.R +++ b/tests/testthat/test-quarto.R @@ -8,6 +8,6 @@ test_that("quarto_run gives guidance in error", { expect_snapshot( error = TRUE, quarto_run(c("rend", "--quiet")), - transform = transform_quarto_cli_in_output + transform = transform_quarto_cli_in_output() ) }) diff --git a/tests/testthat/test-use.R b/tests/testthat/test-use.R index 574df3af..d863ca3f 100644 --- a/tests/testthat/test-use.R +++ b/tests/testthat/test-use.R @@ -8,3 +8,29 @@ test_that("Installing an extension", { expect_true(dir.exists("_extensions/quarto-journals/jss")) expect_length(list.files(pattern = "[.]qmd$"), 1) }) + +test_that("quarto_use_binder errors < 1.4", { + skip_if_quarto(ver = 1.4) + expect_snapshot( + error = TRUE, + quarto_use_binder(), + transform = transform_quarto_cli_in_output(full_path = TRUE), + variant = "quarto-before-1.4" + ) +}) + +test_that("quarto_use_binder works", { + skip_on_cran() + skip_if_no_quarto(ver = 1.4) + project <- local_quarto_project( + c("---", + "title: Histogram", + "---", + "", + "```{r}", + "hist(rnorm(100))", + "```") + ) + withr::local_dir(project) + quarto_use_binder(no_prompt = TRUE) +}) From 68b6df5d3ff60c59399afb5643badb46084636d4 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Jan 2024 17:19:33 +0100 Subject: [PATCH 3/6] Handle prompt in quarto use binder This will work for future version of Quarto --- R/use.R | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/R/use.R b/R/use.R index e6065f18..f09721eb 100644 --- a/R/use.R +++ b/R/use.R @@ -23,7 +23,7 @@ quarto_use_template <- function(template, no_prompt = FALSE) { quarto_bin <- find_quarto() - # This will ask for approval or stop installation + # This will ask for approval or stop installation check_extension_approval(no_prompt, "Quarto templates", "https://quarto.org/docs/extensions/formats.html#distributing-formats") args <- c("template", template, "--no-prompt") @@ -41,9 +41,29 @@ quarto_use_binder <- function(no_prompt = FALSE) { ) } + if (!no_prompt) { + if (!rlang::is_interactive()) { + cli::cli_abort(c( + "{.code quarto use binder} requires explicit approval as it will write some configurations files to your project.", + '>' = "Set {.arg no_prompt = TRUE} if you agree.", + i = "See more at {.url https://quarto.org/docs/projects/binder.html}") + ) + } else { + cli::cli_inform(c( + "{.code quarto use binder} will write some configurations files to your project.", + '*' = "If you are not sure what it will do, we recommend consulting documentation at {.url https://quarto.org/docs/projects/binder.html}" + )) + prompt_value <- tolower(readline("Do you want to continue ?(Y/n)? ")) + if (!prompt_value %in% "y") { + cli::cli_inform("Aborting") + return(invisible(FALSE)) + } + } + } + quarto_bin <- find_quarto() - args <- c("binder", if (no_prompt) "--no-prompt") + args <- c("binder", "--no-prompt") quarto_use(args, quarto_bin = quarto_bin, echo = TRUE) From 0818256d57ac33a4da373183b3944de80fd2c87e Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Jan 2024 17:22:03 +0100 Subject: [PATCH 4/6] Add snapshot test for `no_prompt = FALSE` --- tests/testthat/_snaps/use.md | 10 ++++++++++ tests/testthat/test-use.R | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/use.md diff --git a/tests/testthat/_snaps/use.md b/tests/testthat/_snaps/use.md new file mode 100644 index 00000000..072eb5f9 --- /dev/null +++ b/tests/testthat/_snaps/use.md @@ -0,0 +1,10 @@ +# quarto_use_binder works + + Code + quarto_use_binder(no_prompt = FALSE) + Condition + Error in `quarto_use_binder()`: + ! `quarto use binder` requires explicit approval as it will write some configurations files to your project. + > Set `no_prompt = TRUE` if you agree. + i See more at + diff --git a/tests/testthat/test-use.R b/tests/testthat/test-use.R index d863ca3f..9becbbf5 100644 --- a/tests/testthat/test-use.R +++ b/tests/testthat/test-use.R @@ -10,7 +10,7 @@ test_that("Installing an extension", { }) test_that("quarto_use_binder errors < 1.4", { - skip_if_quarto(ver = 1.4) + skip_if_quarto(ver = "1.4") expect_snapshot( error = TRUE, quarto_use_binder(), @@ -21,7 +21,8 @@ test_that("quarto_use_binder errors < 1.4", { test_that("quarto_use_binder works", { skip_on_cran() - skip_if_no_quarto(ver = 1.4) + skip_if_no_quarto(ver = "1.4") + project <- local_quarto_project( c("---", "title: Histogram", @@ -31,6 +32,11 @@ test_that("quarto_use_binder works", { "hist(rnorm(100))", "```") ) + expect_snapshot( + error = TRUE, + quarto_use_binder(no_prompt = FALSE) + ) withr::local_dir(project) + skip_if_no_quarto(ver = "1.5") quarto_use_binder(no_prompt = TRUE) }) From 4ba3f57ba403383feaaae936fc7a434884566004 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 24 Jan 2024 17:25:07 +0100 Subject: [PATCH 5/6] Correct place for the local_dir --- tests/testthat/test-use.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-use.R b/tests/testthat/test-use.R index 9becbbf5..a75c29c1 100644 --- a/tests/testthat/test-use.R +++ b/tests/testthat/test-use.R @@ -32,11 +32,11 @@ test_that("quarto_use_binder works", { "hist(rnorm(100))", "```") ) + withr::local_dir(project) expect_snapshot( error = TRUE, quarto_use_binder(no_prompt = FALSE) ) - withr::local_dir(project) skip_if_no_quarto(ver = "1.5") quarto_use_binder(no_prompt = TRUE) }) From 563c3bf2d20a66775b3031bce692a6183b58ed9d Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 25 Jan 2024 10:04:15 +0100 Subject: [PATCH 6/6] Fix for quarto use binder no prompt is in 1.5 --- R/use.R | 4 ++-- tests/testthat/_snaps/quarto-before-1.4/use.md | 9 --------- tests/testthat/_snaps/quarto-before-1.5/use.md | 9 +++++++++ tests/testthat/test-use.R | 9 ++++----- 4 files changed, 15 insertions(+), 16 deletions(-) delete mode 100644 tests/testthat/_snaps/quarto-before-1.4/use.md create mode 100644 tests/testthat/_snaps/quarto-before-1.5/use.md diff --git a/R/use.R b/R/use.R index f09721eb..d6e1f614 100644 --- a/R/use.R +++ b/R/use.R @@ -34,9 +34,9 @@ quarto_use_template <- function(template, no_prompt = FALSE) { } quarto_use_binder <- function(no_prompt = FALSE) { - if (quarto_version() < 1.4) { + if (quarto_version() < "1.5") { cli::cli_abort(c( - "{.code quarto use binder} has been added in Quarto 1.4. See {.url https://quarto.org/docs/projects/binder.html}.", + "Binder feature for Quarto requires 1.5. See {.url https://quarto.org/docs/projects/binder.html}.", i = "You are using {.strong {quarto_version()}} from {.file {quarto_path()}}.") ) } diff --git a/tests/testthat/_snaps/quarto-before-1.4/use.md b/tests/testthat/_snaps/quarto-before-1.4/use.md deleted file mode 100644 index 9d02e3bc..00000000 --- a/tests/testthat/_snaps/quarto-before-1.4/use.md +++ /dev/null @@ -1,9 +0,0 @@ -# quarto_use_binder errors < 1.4 - - Code - quarto_use_binder() - Condition - Error in `quarto_use_binder()`: - ! `quarto use binder` has been added in Quarto 1.4. See . - i You are using 1.3.450 from ''. - diff --git a/tests/testthat/_snaps/quarto-before-1.5/use.md b/tests/testthat/_snaps/quarto-before-1.5/use.md new file mode 100644 index 00000000..f2b0c9f8 --- /dev/null +++ b/tests/testthat/_snaps/quarto-before-1.5/use.md @@ -0,0 +1,9 @@ +# quarto_use_binder errors < 1.5 + + Code + quarto_use_binder() + Condition + Error in `quarto_use_binder()`: + ! Binder feature for Quarto requires 1.5. See . + i You are using 1.4.549 from ''. + diff --git a/tests/testthat/test-use.R b/tests/testthat/test-use.R index a75c29c1..0dc1aa26 100644 --- a/tests/testthat/test-use.R +++ b/tests/testthat/test-use.R @@ -9,19 +9,19 @@ test_that("Installing an extension", { expect_length(list.files(pattern = "[.]qmd$"), 1) }) -test_that("quarto_use_binder errors < 1.4", { - skip_if_quarto(ver = "1.4") +test_that("quarto_use_binder errors < 1.5", { + skip_if_quarto(ver = "1.5") expect_snapshot( error = TRUE, quarto_use_binder(), transform = transform_quarto_cli_in_output(full_path = TRUE), - variant = "quarto-before-1.4" + variant = "quarto-before-1.5" ) }) test_that("quarto_use_binder works", { skip_on_cran() - skip_if_no_quarto(ver = "1.4") + skip_if_no_quarto(ver = "1.5") project <- local_quarto_project( c("---", @@ -37,6 +37,5 @@ test_that("quarto_use_binder works", { error = TRUE, quarto_use_binder(no_prompt = FALSE) ) - skip_if_no_quarto(ver = "1.5") quarto_use_binder(no_prompt = TRUE) })