Skip to content

Commit 768c50a

Browse files
committed
test tar_workspace_download()
1 parent 920beb2 commit 768c50a

12 files changed

+83
-9
lines changed

Diff for: R/class_builder.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ builder_save_workspace <- function(target, pipeline, scheduler, meta) {
419419
path_store = meta$store
420420
)
421421
scheduler$reporter$report_workspace(target)
422-
meta$database$upload_workspace(target, meta)
423-
scheduler$reporter$report_workspace_upload(target)
422+
meta$database$upload_workspace(target, meta, scheduler$reporter)
424423
}
425424

426425
builder_record_error_meta <- function(target, pipeline, meta) {

Diff for: R/class_database.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ database_class <- R6::R6Class(
435435
}
436436
"upload"
437437
},
438-
upload_workspace = function(target, meta) {
438+
upload_workspace = function(target, meta, reporter) {
439439
"upload_workspace"
440440
},
441441
download = function(verbose = TRUE) {
@@ -444,7 +444,7 @@ database_class <- R6::R6Class(
444444
}
445445
"download"
446446
},
447-
download_workspace = function(name, store) {
447+
download_workspace = function(name, store, verbose = TRUE) {
448448
"download_workspace"
449449
},
450450
head = function() {

Diff for: R/class_database_aws.R

+11-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ database_aws_class <- R6::R6Class(
6767
)
6868
invisible()
6969
},
70-
download_workspace = function(name, store) {
70+
download_workspace = function(name, store, verbose = TRUE) {
7171
path <- path_workspace(store, name)
7272
key <- path_workspace(dirname(self$key), name)
7373
aws <- self$resources$aws
74+
if (verbose) {
75+
tar_print(
76+
"Downloading AWS workspace file ",
77+
key,
78+
" to local file ",
79+
path
80+
)
81+
}
7482
dir_create(dirname(path))
7583
aws_s3_download(
7684
file = path,
@@ -112,7 +120,7 @@ database_aws_class <- R6::R6Class(
112120
)
113121
invisible()
114122
},
115-
upload_workspace = function(target, meta) {
123+
upload_workspace = function(target, meta, reporter) {
116124
name <- target_get_name(target)
117125
path <- path_workspace(meta$store, name)
118126
key <- path_workspace(dirname(self$key), name)
@@ -127,6 +135,7 @@ database_aws_class <- R6::R6Class(
127135
args = aws$args,
128136
max_tries = aws$max_tries %|||% 5L
129137
)
138+
reporter$report_workspace_upload(target)
130139
invisible()
131140
},
132141
head = function() {

Diff for: R/class_database_local.R

+9
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ database_local_class <- R6::R6Class(
3737
}
3838
invisible()
3939
},
40+
upload_workspace = function(target, meta, reporter) {
41+
invisible()
42+
},
4043
download = function(verbose = TRUE) {
4144
if (verbose) {
4245
tar_print(self$path, " not configured to download from the cloud.")
4346
}
4447
invisible()
4548
},
49+
download_workspace = function(name, store, verbose = TRUE) {
50+
if (verbose) {
51+
tar_print(self$path, " not configured to download from the cloud.")
52+
}
53+
invisible()
54+
},
4655
delete_cloud = function(verbose = TRUE) {
4756
if (verbose) {
4857
tar_print("Not configured to delete cloud object ", self$key)

Diff for: R/tar_workspace_download.R

+9-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#' defining the pipeline. Must be configured with the right `aws`
1818
#' and `repository_meta` options (in [tar_option_set()])
1919
#' to support downloading workspaces from the cloud.
20+
#' @param verbose `TRUE` to print an informative message describing the
21+
#' download, `FALSE` to stay silent.
2022
#' @examples
2123
#' if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
2224
#' tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
@@ -54,7 +56,8 @@
5456
tar_workspace_download <- function(
5557
name,
5658
script = targets::tar_config_get("script"),
57-
store = targets::tar_config_get("store")
59+
store = targets::tar_config_get("store"),
60+
verbose = TRUE
5861
) {
5962
name <- tar_deparse_language(substitute(name))
6063
tar_assert_chr(name)
@@ -80,6 +83,10 @@ tar_workspace_download <- function(
8083
"tar_option_get(\"repository_meta\") == \"local\"."
8184
)
8285
)
83-
database_meta(path_store = store)$download_workspace(name, store)
86+
# Tested in tests/aws/test-aws-workspaces.R.
87+
# nocov start
88+
database <- database_meta(path_store = store)
89+
database$download_workspace(name, store = store, verbose = verbose)
8490
invisible()
91+
# nocov end
8592
}

Diff for: man/tar_workspace_download.Rd

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/testthat/test-class_database.R

+4
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ tar_test("compare_working_directories()", {
423423
tar_test("local database cloud methods", {
424424
database <- database_init(repository = "local")
425425
expect_null(database$download())
426+
expect_null(database$download_workspace(NULL, NULL, TRUE))
426427
expect_null(database$upload())
428+
expect_null(database$upload_workspace(NULL, NULL, TRUE))
427429
expect_false(database$head()$exists)
428430
expect_null(database$delete_cloud())
429431
})
@@ -438,11 +440,13 @@ tar_test("database unknown repository", {
438440
tar_test("mock download", {
439441
x <- database_class$new(path = tempfile())
440442
expect_equal(x$download(), "download")
443+
expect_equal(x$download_workspace(), "download_workspace")
441444
})
442445

443446
tar_test("mock upload", {
444447
x <- database_class$new(path = tempfile())
445448
expect_equal(x$upload(), "upload")
449+
expect_equal(x$upload_workspace(), "upload_workspace")
446450
})
447451

448452
tar_test("mock head non-existent file", {

Diff for: tests/testthat/test-class_reporter.R

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tar_test("reporter abstract class", {
1010
expect_null(x$report_outdated(NULL))
1111
expect_null(x$report_outdated_end(NULL))
1212
expect_null(x$report_workspace(NULL))
13+
expect_null(x$report_workspace_upload(NULL))
1314
expect_null(x$report_retry())
1415
expect_null(x$report_finalize())
1516
expect_null(x$flush_messages())

Diff for: tests/testthat/test-class_timestamp.R

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ tar_test("run timestamp reporter with a error and saved workspace", {
1717
expect_error(expect_message(local$run()), class = "tar_condition_run")
1818
})
1919

20+
tar_test("run timestamp reporter workspace upload", {
21+
skip_on_os("windows")
22+
pipeline <- pipeline_init(
23+
list(
24+
target_init("x", quote(123))
25+
)
26+
)
27+
local <- local_init(pipeline, reporter = "timestamp")
28+
local$run()
29+
expect_message(
30+
local$scheduler$reporter$report_workspace_upload(pipeline$targets$x)
31+
)
32+
})
33+
2034
tar_test("run timestamp reporter with a warning", {
2135
pipeline <- pipeline_init(list(target_init("x", quote(warning(123)))))
2236
local <- local_init(pipeline, reporter = "timestamp")

Diff for: tests/testthat/test-class_verbose.R

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ tar_test("run verbose reporter with a error and save workspace", {
1717
expect_error(expect_message(local$run()), class = "tar_condition_run")
1818
})
1919

20+
tar_test("run verbose reporter workspace upload", {
21+
skip_on_os("windows")
22+
pipeline <- pipeline_init(
23+
list(
24+
target_init("x", quote(123))
25+
)
26+
)
27+
local <- local_init(pipeline, reporter = "verbose")
28+
local$run()
29+
expect_message(
30+
local$scheduler$reporter$report_workspace_upload(pipeline$targets$x)
31+
)
32+
})
33+
2034
tar_test("run verbose reporter with a warning", {
2135
pipeline <- pipeline_init(list(target_init("x", quote(warning(123)))))
2236
local <- local_init(pipeline, reporter = "verbose")

Diff for: tests/testthat/test-tar_workspace_download.R

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tar_test("tar_workspace_download() on a local pipeline", {
2+
tar_script(tar_target(x, 1))
3+
tar_make(callr_function = NULL)
4+
expect_error(
5+
tar_workspace_download(x),
6+
condition = "tar_condition_validate"
7+
)
8+
})

Diff for: tests/testthat/test-utils_cli.R

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ tar_test("cli_workspace()", {
5454
expect_message(cli_workspace("x", time_stamp = TRUE))
5555
})
5656

57+
tar_test("cli_workspace_upload()", {
58+
skip_cran()
59+
expect_message(cli_workspace_upload("x", time_stamp = TRUE))
60+
})
61+
5762
tar_test("cli_blue_bullet()", {
5863
skip_cran()
5964
expect_message(cli_blue_bullet("x"))

0 commit comments

Comments
 (0)