Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make quarto_render(as_job = TRUE) wrapable #105

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
13 changes: 11 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@
if (as_job && rstudioapi::isAvailable()) {
message("Rendering project as background job (use as_job = FALSE to override)")
script <- tempfile(fileext = ".R")
render_args <- as.list(sys.call()[-1L])
render_args <- mapply(
function(arg, arg_name) paste0(
arg_name,
"="[nchar(arg_name) > 0L],
deparse1(eval(arg, envir = parent.frame(n = 3L)))
),
render_args,
names(render_args)
)

Check warning on line 119 in R/render.R

View check run for this annotation

Codecov / codecov/patch

R/render.R#L110-L119

Added lines #L110 - L119 were not covered by tests
Comment on lines +110 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle this a bit differently for complex wrapping calls that would use ... for example. In this case, it will not work. rlang maybe be useful for this. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, I didn't test with a wrapper function that uses dots, I only tested simple named args. Feel free to refactor and expand on this PR as you see fit!

writeLines(
c("library(quarto)", deparse(sys.call())),
paste0("quarto::quarto_render(", paste0(render_args, collapse = ", "), ")"),

Check warning on line 121 in R/render.R

View check run for this annotation

Codecov / codecov/patch

R/render.R#L121

Added line #L121 was not covered by tests
script
)
rstudioapi::jobRunScript(
Expand All @@ -120,7 +130,6 @@
return(invisible(NULL))
}


# build args
args <- c("render", input)
if (!missing(output_format)) {
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@ test_that("quarto_args in quarto_render", {
transform = transform_quarto_cli_in_output(full_path = TRUE)
)
})

test_that("`quarto_render(as_job = TRUE)` is wrapable", {
# this tests background jobs, a feature only available in interactive RStudio IDE sesssions
skip_on_cran()
skip_if_no_quarto()
skip_if_not(rstudioapi::isAvailable())
qmd <- local_qmd_file(c("content"))
withr::local_dir(dirname(qmd))
output <- basename(
withr::local_file(xfun::with_ext(qmd, ".native"))
)
wrapper <- function(path, out, format) {
quarto_render(
input = path,
output_file = out,
output_format = format,
quiet = TRUE,
as_job = TRUE
)
}
wrapper(basename(qmd), output, "native")
# wait for background job to finish (10s should be conservative enough)
Sys.sleep(10)
expect_true(file.exists(output))
})
Loading