Skip to content

Commit

Permalink
test tar_workspace_download()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Feb 4, 2025
1 parent 920beb2 commit 768c50a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 9 deletions.
3 changes: 1 addition & 2 deletions R/class_builder.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ builder_save_workspace <- function(target, pipeline, scheduler, meta) {
path_store = meta$store
)
scheduler$reporter$report_workspace(target)
meta$database$upload_workspace(target, meta)
scheduler$reporter$report_workspace_upload(target)
meta$database$upload_workspace(target, meta, scheduler$reporter)
}

builder_record_error_meta <- function(target, pipeline, meta) {
Expand Down
4 changes: 2 additions & 2 deletions R/class_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ database_class <- R6::R6Class(
}
"upload"
},
upload_workspace = function(target, meta) {
upload_workspace = function(target, meta, reporter) {
"upload_workspace"
},
download = function(verbose = TRUE) {
Expand All @@ -444,7 +444,7 @@ database_class <- R6::R6Class(
}
"download"
},
download_workspace = function(name, store) {
download_workspace = function(name, store, verbose = TRUE) {
"download_workspace"
},
head = function() {
Expand Down
13 changes: 11 additions & 2 deletions R/class_database_aws.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ database_aws_class <- R6::R6Class(
)
invisible()
},
download_workspace = function(name, store) {
download_workspace = function(name, store, verbose = TRUE) {
path <- path_workspace(store, name)
key <- path_workspace(dirname(self$key), name)
aws <- self$resources$aws
if (verbose) {
tar_print(
"Downloading AWS workspace file ",
key,
" to local file ",
path
)
}
dir_create(dirname(path))
aws_s3_download(
file = path,
Expand Down Expand Up @@ -112,7 +120,7 @@ database_aws_class <- R6::R6Class(
)
invisible()
},
upload_workspace = function(target, meta) {
upload_workspace = function(target, meta, reporter) {
name <- target_get_name(target)
path <- path_workspace(meta$store, name)
key <- path_workspace(dirname(self$key), name)
Expand All @@ -127,6 +135,7 @@ database_aws_class <- R6::R6Class(
args = aws$args,
max_tries = aws$max_tries %|||% 5L
)
reporter$report_workspace_upload(target)
invisible()
},
head = function() {
Expand Down
9 changes: 9 additions & 0 deletions R/class_database_local.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ database_local_class <- R6::R6Class(
}
invisible()
},
upload_workspace = function(target, meta, reporter) {
invisible()
},
download = function(verbose = TRUE) {
if (verbose) {
tar_print(self$path, " not configured to download from the cloud.")
}
invisible()
},
download_workspace = function(name, store, verbose = TRUE) {
if (verbose) {
tar_print(self$path, " not configured to download from the cloud.")
}
invisible()
},
delete_cloud = function(verbose = TRUE) {
if (verbose) {
tar_print("Not configured to delete cloud object ", self$key)
Expand Down
11 changes: 9 additions & 2 deletions R/tar_workspace_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' defining the pipeline. Must be configured with the right `aws`
#' and `repository_meta` options (in [tar_option_set()])
#' to support downloading workspaces from the cloud.
#' @param verbose `TRUE` to print an informative message describing the
#' download, `FALSE` to stay silent.
#' @examples
#' if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
#' tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
Expand Down Expand Up @@ -54,7 +56,8 @@
tar_workspace_download <- function(
name,
script = targets::tar_config_get("script"),
store = targets::tar_config_get("store")
store = targets::tar_config_get("store"),
verbose = TRUE
) {
name <- tar_deparse_language(substitute(name))
tar_assert_chr(name)
Expand All @@ -80,6 +83,10 @@ tar_workspace_download <- function(
"tar_option_get(\"repository_meta\") == \"local\"."
)
)
database_meta(path_store = store)$download_workspace(name, store)
# Tested in tests/aws/test-aws-workspaces.R.
# nocov start
database <- database_meta(path_store = store)
database$download_workspace(name, store = store, verbose = verbose)
invisible()
# nocov end
}
6 changes: 5 additions & 1 deletion man/tar_workspace_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-class_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ tar_test("compare_working_directories()", {
tar_test("local database cloud methods", {
database <- database_init(repository = "local")
expect_null(database$download())
expect_null(database$download_workspace(NULL, NULL, TRUE))
expect_null(database$upload())
expect_null(database$upload_workspace(NULL, NULL, TRUE))
expect_false(database$head()$exists)
expect_null(database$delete_cloud())
})
Expand All @@ -438,11 +440,13 @@ tar_test("database unknown repository", {
tar_test("mock download", {
x <- database_class$new(path = tempfile())
expect_equal(x$download(), "download")
expect_equal(x$download_workspace(), "download_workspace")
})

tar_test("mock upload", {
x <- database_class$new(path = tempfile())
expect_equal(x$upload(), "upload")
expect_equal(x$upload_workspace(), "upload_workspace")
})

tar_test("mock head non-existent file", {
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-class_reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tar_test("reporter abstract class", {
expect_null(x$report_outdated(NULL))
expect_null(x$report_outdated_end(NULL))
expect_null(x$report_workspace(NULL))
expect_null(x$report_workspace_upload(NULL))
expect_null(x$report_retry())
expect_null(x$report_finalize())
expect_null(x$flush_messages())
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-class_timestamp.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ tar_test("run timestamp reporter with a error and saved workspace", {
expect_error(expect_message(local$run()), class = "tar_condition_run")
})

tar_test("run timestamp reporter workspace upload", {
skip_on_os("windows")
pipeline <- pipeline_init(
list(
target_init("x", quote(123))
)
)
local <- local_init(pipeline, reporter = "timestamp")
local$run()
expect_message(
local$scheduler$reporter$report_workspace_upload(pipeline$targets$x)
)
})

tar_test("run timestamp reporter with a warning", {
pipeline <- pipeline_init(list(target_init("x", quote(warning(123)))))
local <- local_init(pipeline, reporter = "timestamp")
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-class_verbose.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ tar_test("run verbose reporter with a error and save workspace", {
expect_error(expect_message(local$run()), class = "tar_condition_run")
})

tar_test("run verbose reporter workspace upload", {
skip_on_os("windows")
pipeline <- pipeline_init(
list(
target_init("x", quote(123))
)
)
local <- local_init(pipeline, reporter = "verbose")
local$run()
expect_message(
local$scheduler$reporter$report_workspace_upload(pipeline$targets$x)
)
})

tar_test("run verbose reporter with a warning", {
pipeline <- pipeline_init(list(target_init("x", quote(warning(123)))))
local <- local_init(pipeline, reporter = "verbose")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-tar_workspace_download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tar_test("tar_workspace_download() on a local pipeline", {
tar_script(tar_target(x, 1))
tar_make(callr_function = NULL)
expect_error(
tar_workspace_download(x),
condition = "tar_condition_validate"
)
})
5 changes: 5 additions & 0 deletions tests/testthat/test-utils_cli.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ tar_test("cli_workspace()", {
expect_message(cli_workspace("x", time_stamp = TRUE))
})

tar_test("cli_workspace_upload()", {
skip_cran()
expect_message(cli_workspace_upload("x", time_stamp = TRUE))
})

tar_test("cli_blue_bullet()", {
skip_cran()
expect_message(cli_blue_bullet("x"))
Expand Down

0 comments on commit 768c50a

Please sign in to comment.