From 4543c53f7523956d9c1488afb8cffc83c7f0b66b Mon Sep 17 00:00:00 2001 From: wlandau Date: Mon, 18 Mar 2024 13:26:54 -0400 Subject: [PATCH] Complete working_directory tests --- tests/testthat/test-tar_quarto_rep.R | 45 ++++++++++++++++++++++++++++ tests/testthat/test-tar_render.R | 2 +- tests/testthat/test-tar_render_rep.R | 45 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-tar_quarto_rep.R b/tests/testthat/test-tar_quarto_rep.R index 820f6b7..6b52d09 100644 --- a/tests/testthat/test-tar_quarto_rep.R +++ b/tests/testthat/test-tar_quarto_rep.R @@ -187,6 +187,51 @@ targets::tar_test("tar_quarto_rep() run", { expect_equal(out, status_completed()) }) +targets::tar_test("tar_quarto_rep() with custom working directory", { + skip_on_cran() + skip_rmarkdown() + lines <- c( + "---", + "title: report", + "output_format: html", + "params:", + " par: \"default value\"", + "---", + "", + "```{r}", + "targets::tar_read(upstream, store = '../_targets')", + "file.create(\"here\")", + "```" + ) + writeLines(lines, "report.qmd") + dir.create("out") + on.exit(unlink("out", recursive = TRUE)) + targets::tar_script({ + library(tarchetypes) + list( + tar_target(upstream, "UPSTREAM_SUCCEEDED"), + tar_quarto_rep( + report, + "report.qmd", + execute_params = data.frame( + par = "parval1", + stringsAsFactors = FALSE + ), + working_directory = "out", + batches = 1 + ) + ) + }) + expect_false(file.exists("here")) + expect_false(file.exists(file.path("out", "here"))) + suppressMessages(targets::tar_make(callr_function = NULL)) + expect_false(file.exists("here")) + expect_true(file.exists(file.path("out", "here"))) + lines <- suppressWarnings(readLines(list.files(pattern = "html$")[1L])) + expect_true(any(grepl("UPSTREAM_SUCCEEDED", lines))) + expect_equal(targets::tar_outdated(callr_function = NULL), character(0L)) +}) + targets::tar_test("tar_quarto_rep() run with output_file specified", { skip_on_cran() skip_quarto() diff --git a/tests/testthat/test-tar_render.R b/tests/testthat/test-tar_render.R index 936421e..e2b344c 100644 --- a/tests/testthat/test-tar_render.R +++ b/tests/testthat/test-tar_render.R @@ -79,7 +79,7 @@ targets::tar_test("tar_render() with custom output_file and working dir", { "---", "", "```{r}", - "tar_read(upstream, store = '../_targets')", + "targets::tar_read(upstream, store = '../_targets')", "file.create(\"here\")", "```" ) diff --git a/tests/testthat/test-tar_render_rep.R b/tests/testthat/test-tar_render_rep.R index c329e96..4763f10 100644 --- a/tests/testthat/test-tar_render_rep.R +++ b/tests/testthat/test-tar_render_rep.R @@ -269,6 +269,51 @@ targets::tar_test("tar_render_rep() with output_file and _files", { } }) +targets::tar_test("tar_render_rep() with custom working directory", { + skip_on_cran() + skip_rmarkdown() + lines <- c( + "---", + "title: report", + "output_format: html_document", + "params:", + " par: \"default value\"", + "---", + "", + "```{r}", + "tar_read(upstream, store = '../_targets')", + "file.create(\"here\")", + "```" + ) + writeLines(lines, "report.Rmd") + dir.create("out") + on.exit(unlink("out", recursive = TRUE)) + targets::tar_script({ + library(tarchetypes) + list( + tar_target(upstream, "UPSTREAM_SUCCEEDED"), + tar_render_rep( + report, + "report.Rmd", + params = data.frame( + par = "parval1", + stringsAsFactors = FALSE + ), + working_directory = "out", + batches = 1 + ) + ) + }) + expect_false(file.exists("here")) + expect_false(file.exists(file.path("out", "here"))) + suppressMessages(targets::tar_make(callr_function = NULL)) + expect_false(file.exists("here")) + expect_true(file.exists(file.path("out", "here"))) + lines <- readLines(list.files(pattern = "html$")[1L]) + expect_true(any(grepl("UPSTREAM_SUCCEEDED", lines))) + expect_equal(targets::tar_outdated(callr_function = NULL), character(0L)) +}) + targets::tar_test("tar_render_rep_run_params", { params <- tibble::tibble(param1 = letters[seq_len(4)]) out <- tar_render_rep_run_params(params, 1)